如何在VBA/Access中调试.打印查询设计中的查询结果

如何在VBA/Access中调试.打印查询设计中的查询结果,vba,ms-access,Vba,Ms Access,我想通过VBA从access执行预定义查询,并将其打印到调试输出。查询设计的名称位于名为report的变量中 我希望它能与以下各项配合使用: debug.print db.Execute report 但每次vba都会将其自动更正为: debug.print db.Execute; report 如果我理解正确,代表新行,因此在我的例子中没有意义。但是在这两种情况下,无论有没有新行,我都会得到一个错误。我假设一个简单的问题是,这不是正确的语法 我可以找到很多关于如何调试打印在VBA中创建为字

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

我希望它能与以下各项配合使用:

debug.print db.Execute report
但每次vba都会将其自动更正为:

debug.print db.Execute; report
如果我理解正确,
代表新行,因此在我的例子中没有意义。但是在这两种情况下,无论有没有新行,我都会得到一个错误。我假设一个简单的问题是,这不是正确的语法

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

请尝试:

'// 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)


在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.Execute
result
实际上是独立的参数(它无法知道您希望
result
传递到
db.Execute
首先,因为您的语法中没有说明这一点)。因此,您必须使用括号让它知道。

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

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

在这两种情况下,我都会得到错误“Function或variable expact”。错误出现在.Executeit seams上,因为db不存在,但两行之后没有调试.print。它可以正常工作。我需要查看所有代码来尝试猜测原因,但它也超出了这个问题的范围,因为它询问了其他问题(这是关于您语句中出现的“;”的问题)。我建议单独发布一个关于这个问题的新问题,并提供所需的所有代码
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