
[英]MS Access report Pass through query to SQL Server with parameter
[英]In MS Access SQL Server Linked Table pass through query
我有一个针对使用SQL Server的getdate()
函数的MS Access中的链接表的查询。 但是,当我尝试运行查询时出现此错误:
函数中未定义的函数GetDate
如何创建允许使用SQL Server T-SQL语法的链接表? 我看到这称为pass through query
但是我不知道如何设置它以将链接表上的连接用作传递查询。
当前使用Access2010。查询是:
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
出现错误的原因是GETDATE()
不是 MSAccess中的函数 。 您可能需要Now()
来获取日期和时间,或者可以使用Date()
来提供日期
我不太明白这句话:
如何创建允许使用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/
这是创建传递查询的快速而肮脏的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
现在,您可以像使用其他任何Access查询一样使用“ testqry”(无论如何,从SELECT进行)
简单地将您的t-sql查询保存为传递
Select GetDate()
然后在VBA代码中,您可以执行以下操作:
TheSqlDate = currentdb.QueryDefs("qPass").OpenRecordset()(0)
使用ADO和对连接字符串进行硬编码,以及此处发布的其他代码的巨大冲击,只是增加可计费时间并创造世界平衡的一种方法。 我发布的解决方案只有一行代码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.