【问题标题】:Pass Through Query With Parameters带参数传递查询
【发布时间】:2017-08-07 16:08:27
【问题描述】:

我正在尝试运行Access 2013 传递查询,该查询从我的 VBA 语法中获取两个参数。当我运行它时,我得到一个编译错误

预计语句结束
"和"

应该如何修改这个 sub 才能使它成为有效的 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定义为字符串怎么样?
  • 我将 sql 添加为字符串,但一旦我移动光标,它就会再次抛出错误。
  • 转到即时窗口并输入?"Select fname, lname, address from einfo where startdate between "&d1&" and "&d2&""

标签: ms-access vba ms-access-2013 pass-through


【解决方案1】:

SQL Server 的日期应该用引号引起来,不要忘记空格:

SQL = "Select fname, lname, address from einfo where startdate between '" & d1 & "' and '" & d2 & "'"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 2022-01-23
    • 2014-08-23
    • 2014-11-16
    相关资源
    最近更新 更多