使用参数传递查询
Pass Through Query With Parameters
我正在尝试 运行 Access 2013
通过查询,该查询从我的 VBA 语法中获取两个参数。当我 运行 这个时,我得到了
的编译错误
expected end of statement
" and "
如何修改此子才能使其成为有效的 sql 字符串?
Public Sub GeneratePassThroughForJob()
Dim qdfPassThrough As DAO.QueryDef, MyDB As Database
Dim strConnect As String, d1 As String, d2 As String, Dim SQL As String
d1 = Format(Forms!DataPull!txtd1, "YYYY-MM-DD")
d2 = Format(Forms!DataPull!txtd2, "YYYY-MM-DD")
If Not IsNull(CurrentDb.QueryDefs("qrySQLPass").SQL) Then
CurrentDb.QueryDefs.Delete "qrySQLPass"
End If
Set MyDB = CurrentDb()
Set qdfPassThrough = MyDB.CreateQueryDef("qrySQLPass")
strConnect = "ValidSQLServerConnectionString"
qdfPassThrough.Connect = "ODBC;" & strConnect
SQL = "Select fname, lname, address from einfo where startdate between "&d1&" and "&d2&""
qdfPassThrough.SQL = "Select fname, lname, address from einfo where startdate between "&d1&" and "&d2&""
qdfPassThrough.ReturnsRecords = False
qdfPassThrough.Close
Application.RefreshDatabaseWindow
DoCmd.OpenQuery "qrySQLPass", acViewNormal, acReadOnly
DoCmd.Maximize
End Sub
SQL 服务器的日期应该被引用,并且不要忘记空格:
SQL = "Select fname, lname, address from einfo where startdate between '" & d1 & "' and '" & d2 & "'"
我正在尝试 运行 Access 2013
通过查询,该查询从我的 VBA 语法中获取两个参数。当我 运行 这个时,我得到了
expected end of statement
" and "
如何修改此子才能使其成为有效的 sql 字符串?
Public Sub GeneratePassThroughForJob()
Dim qdfPassThrough As DAO.QueryDef, MyDB As Database
Dim strConnect As String, d1 As String, d2 As String, Dim SQL As String
d1 = Format(Forms!DataPull!txtd1, "YYYY-MM-DD")
d2 = Format(Forms!DataPull!txtd2, "YYYY-MM-DD")
If Not IsNull(CurrentDb.QueryDefs("qrySQLPass").SQL) Then
CurrentDb.QueryDefs.Delete "qrySQLPass"
End If
Set MyDB = CurrentDb()
Set qdfPassThrough = MyDB.CreateQueryDef("qrySQLPass")
strConnect = "ValidSQLServerConnectionString"
qdfPassThrough.Connect = "ODBC;" & strConnect
SQL = "Select fname, lname, address from einfo where startdate between "&d1&" and "&d2&""
qdfPassThrough.SQL = "Select fname, lname, address from einfo where startdate between "&d1&" and "&d2&""
qdfPassThrough.ReturnsRecords = False
qdfPassThrough.Close
Application.RefreshDatabaseWindow
DoCmd.OpenQuery "qrySQLPass", acViewNormal, acReadOnly
DoCmd.Maximize
End Sub
SQL 服务器的日期应该被引用,并且不要忘记空格:
SQL = "Select fname, lname, address from einfo where startdate between '" & d1 & "' and '" & d2 & "'"