2014-08-31 72 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方式。我發佈的解決方案只有一行代碼!