1

我对使用getdate()SQL Server 功能的 MS Access 中的链接表进行了查询。但是,当我尝试运行查询时出现此错误:

函数中未定义的函数 GetDate

如何创建允许使用 SQL Server T-SQL 语法的链接表?我看到这被称为 apass through query但我不知道如何设置它以使用链接表上的连接作为传递查询。

目前使用Access 2010。查询是:

select getdate()

如果有帮助,我使用以下 vba 代码生成指向 SQL Server 的表链接:

Function LinkTable(LinkedTableAlias As String, Server As String, Database As String, SourceTableName As String, OverwriteIfExists As Boolean, Username As String, Password As String)
    'This method will also update the link if the underlying table definition has been modified.
    If (InStr(1, LinkedTableAlias, "MSys") > 0) Then
        Log "Skipping " & LinkedTableAlias
        Exit Function
    End If
    'The overwrite parameter will cause it to re-map/refresh the link for LinktedTable Alias, but only if it was already a linked table.
    ' it will not overwrite an existing query or local table with the name specified in LinkedTableAlias.

    'Links to a SQL Server table without the need to set up a DSN in the ODBC Console.
    Dim tdfLinked As DAO.TableDef

    ' Open a database to which a linked table can be appended.
    Dim dbsCurrent As Database
    Set dbsCurrent = CurrentDb()

    'Check for and deal with the scenario ofthe table alias already existing
    If TableNameInUse(LinkedTableAlias) Then
        'If InStr(dbsCurrent.TableDefs(LinkedTableAlias).Connect, "AccessBackup") Then
        '    Exit Function
        'End If

        If (Not OverwriteIfExists) Then
            Log "Can't use name '" + LinkedTableAlias + "' because it would overwrite existing table."
            Exit Function
        End If
        'delete existing table, but only if it is a linked table
        'If IsLinkedTable(LinkedTableAlias) Then
            dbsCurrent.TableDefs.Delete LinkedTableAlias
            dbsCurrent.TableDefs.Refresh
        'Else
        '    Log "Can't use name '" + LinkedTableAlias + "' because it would overwrite an existing query or local table."
        '    Exit Function
        'End If
    End If

    'Create a linked table
    Set tdfLinked = dbsCurrent.CreateTableDef(LinkedTableAlias)
    tdfLinked.SourceTableName = SourceTableName

    tdfLinked.Connect = "ODBC;DRIVER={SQL Server};SERVER=" & Server & ";DATABASE=" & Database & ";UID=" & Username & ";PWD=" & Password & ";"

    On Error Resume Next
    dbsCurrent.TableDefs.Append tdfLinked
    If (err.Number = 3626) Then 'too many indexes on source table for Access
            err.Clear
            On Error GoTo 0

            If LinkTable(LinkedTableAlias, Server, Database, "vw" & SourceTableName, OverwriteIfExists, Username, Password) Then
                Log "Can't link directly to table '" + SourceTableName + "' because it contains too many indexes for Access to handle. Linked to view '" & "vw" & SourceTableName & "' instead."
                LinkTable = True
            Else
                Log "Can't link table '" + SourceTableName + "' because it contains too many indexes for Access to handle. Create a view named '" & "vw" & SourceTableName & "' that selects all rows/columns from '" & SourceTableName & "' and try again to circumvent this."
                LinkTable = False
            End If
            Exit Function
    End If
    On Error GoTo 0

    '** Turn on error handling
  On Error GoTo ErrorHandler:
    tdfLinked.RefreshLink


    LinkTable = True

    Exit Function
ErrorHandler:
    Log "refreshlink failed for " & tdfLinked.Name
    LinkTable = True
4

4 回答 4

1

您收到错误的原因是它GETDATE()不是MSAccess 中的函数。您可能需要Now()获取日期和时间,或者您可以使用Date()提供日期的

于 2014-08-31T19:51:33.830 回答
1

我不太明白这个说法:

如何创建允许使用 SQL Server T-SQL 语法的链接表?

但这是将现有 MS Access 转换querydef为传递查询的方式:

在查询中进入设计模式,按Query菜单命令,SQL Specific然后Pass Through

请参阅此屏幕截图。

http://www.mssqltips.com/sqlservertip/1482/microsoft-access-pass-through-queries-to-sql-server/

于 2014-09-01T02:13:13.897 回答
0

Simple save your t-sql query as a pass-though

Select GetDate()

Then in VBA code, you can go:

TheSqlDate =  currentdb.QueryDefs("qPass").OpenRecordset()(0)

Using ADO, and hardcoding connection strings, and the HUGE whacks of other code posted here is just a way to rack up billable hours and create world poveity. My posted solution IS ONLY ONE LINE OF CODE!

于 2014-09-01T20:41:43.203 回答
0

这是创建传递查询的一种快速而肮脏的 VBA 方法:

Set qdf = CurrentDb.CreateQueryDef("testqry")
' this is just your connection string
qdf.Connect = "ODBC;Driver={SQL Server};Server=MSSQL1; Database=MyDB;Trusted_Connection=Yes"
'anything here gets passed directly to and executed on the SQL Server  
qdf.SQL = "select getdate()"
Set qdf = Nothing

现在您可以使用“testqry”,就好像它是任何其他 Access 查询一样(就 SELECTing 而言,无论如何)

于 2014-09-01T03:08:14.217 回答