【问题标题】:How to debug.print a query result from the query design in VBA /Access如何从 VBA / Access 中的查询设计中 debug.print 查询结果
【发布时间】:2017-03-06 08:21:22
【问题描述】:

我想通过 VBA 访问执行预定义查询并将其打印到调试输出。查询设计的名称在名为 report 的变量中。

我期待它可以与:

debug.print db.Execute report

但每次 vba 都会自动更正为:

debug.print db.Execute; report

如果我理解正确,; 代表一个新行,因此对我来说毫无意义。但在这两种情况下,无论有没有新行,我都会收到错误消息。我认为简单的问题是,这不是正确的语法。

我可以找到很多关于如何调试打印在 VBA 中创建为字符串的查询的信息,但我找不到任何提示如何通过查询设计输出在 Access 中预定义的查询。

【问题讨论】:

    标签: vba ms-access


    【解决方案1】:

    试试:

    '// You need the parentheses because the result has to be evaluated before it can be passed to the .Print() method
    Debug.Print db.Execute(result)
    

    Dim myResult As String
    myResult = db.Execute(result)
    
    Debug.Print myResult
    

    在 VBA 中,您可以在不使用括号的情况下将参数传递给过程/方法,只要结果没有被分配给任何东西。

    '// Not assigning anything
    MyExampleFunction arg1, arg2, arg3
    
    '// assigning the result, which needs to be evaluated before it can be passed to a variable.
    MyResult = MyExampleFunction(arg1, arg2, arg3)
    

    同样,当您调用Debug.Print 时,它假定db.Executeresult 实际上是单独的参数(它无法知道您希望首先将result 传递给db.Execute,因为您的语法中没有任何内容可以说明)。所以你必须用括号让它知道。

    【讨论】:

    • 在这两种情况下,我都会收到错误“函数或变量已扩展”。 .Execute 上的错误点
    • 它接缝,因为 db 不存在,但两行之后没有 debug.print 它可以正常工作
    • 我需要查看所有代码来尝试猜测原因,但这也超出了这个问题的范围,因为它正在询问其他内容(这是关于“;”出现在你的陈述)。我建议单独发布一个关于该问题的新问题并提供所需的所有代码
    【解决方案2】:

    问题似乎在于只能使用db.Execute 而不是查询来调用更新。 使用debug.print 从查询中打印整个表没有很好的解决方案,但使用RecordSet 是一种可能的方法,如下面的代码所示。

    Dim rs As DAO.RecordSet
    Set rs = db.OpenRecordset(Bericht)
    
    Do Until rs.EOF = True
        Debug.Print rs("Matrikelnummer"), rs("Bewertung"), rs("Termin (Datum)"), rs("Maximale Bewertung"), rs("Bestehensgrenze"), rs("Versuch"), rs("Äquivalenzleistung")
        rs.MoveNext
    Loop
    
    rs.Close
    Set rs = Nothing
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      相关资源
      最近更新 更多