我对MS Access中使用SQL Server的getdate()
函数的链接表进行了查询。但是,当我尝试运行查询时出现此错误:
未定义函数GetDate in function
如何创建允许使用SQL Server T-SQL语法的链接表?我看到这称为 pass 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
答案 0 :(得分:1)
您收到错误的原因是MSAccess中GETDATE()
不是函数。您可能需要Now()
来获取日期和时间,或者您可以使用提供日期的Date()
答案 1 :(得分: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/
答案 2 :(得分: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查询(无论如何,从它中选择)
答案 3 :(得分:0)
将t-sql查询简单保存为传递
Select GetDate()
然后在VBA代码中,你可以去:
TheSqlDate = currentdb.QueryDefs("qPass").OpenRecordset()(0)
使用ADO和硬编码连接字符串,以及此处发布的其他代码的巨大打击只是一种方法,可以节省计费时间并创建世界级的漏洞。我发布的解决方案只有一行代码!