2014-08-31 122 views
0

我有一个使用SQL Server的getdate()函数的MS Access中的链接表查询。不过,我得到这个错误,当我试图运行查询:在功能在MS Access SQL Server链接表传递查询

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

未定义功能GETDATE?我看到这被称为pass through query但我不知道如何设置它来使用链接表上的连接作为传递查询。

目前使用Access 2010中的查询是:

select getdate() 

如果有帮助,我用生成的表链接到SQL Server以下VBA代码:

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 

回答

1

你之所以得到的错误是,GETDATE()不是函数里面的MSAccess。你可能需要Now()得到的日期和时间,或者您可以使用Date()提供的日期

+0

'getdate()'是数据源中的函数,它是ms-sql服务器。当有链接表时,访问是否会自动转换为t-sql? – SumGuy 2014-08-31 19:57:54

+0

@SumGuy: - getdate()是MSSQL中的一个函数,而不是MSAccess,这就是您遇到错误的原因。 – 2014-08-31 19:58:58

+0

我明白,但这不是我的问题,我如何才能使用链接的数据源并在源数据库上执行查询?我更新了原来的问题,感谢您的帮助至今 – SumGuy 2014-08-31 23:40:04

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查询(只要选择由它去,反正)

0

简单的T-SQL查询保存为一个通不过

Select GetDate() 

然后在VBA代码,你可以去:

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

使用ADO和硬编码的连接字符串,并张贴在这里的其他代码的庞大是敲敲罢了只是一个架起来计费时间,创造世界poveity方式。我发布的解决方案只有一行代码!