简体   繁体   English

如何在VBA / Access中从查询设计中调试查询结果

[英]How to debug.print a query result from the query design in VBA /Access

I want to execute a predefined query from access via VBA and print it to the debug output. 我想通过VBA从访问执行预定义查询并将其打印到调试输出。 The name of the query design is in a variable named report. 查询设计的名称位于名为report的变量中。

I was expecting that it would work with: 我期待它可以用于:

debug.print db.Execute report

But everytime vba autocorrects it to: 但每次vba自动更正它:

debug.print db.Execute; report

If I understand it correctly, the ; 如果我理解正确,那; stands for a new line and makes therefore no sense in my case. 代表一条新线,因此在我的情况下毫无意义。 But in both cases, with and without the new line I get an error. 但在这两种情况下,无论有没有新线路,我都会收到错误。 I assume that the simple problem is, that this is not the right syntax. 我认为简单的问题是,这不是正确的语法。

I could find a lot of information about how to debug print a query that is created as a string in VBA, but I can't find any hints how to output a query that is predefined in Access via a query design. 我可以找到很多关于如何调试打印在VBA中创建为字符串的查询的信息,但是我找不到任何提示如何通过查询设计输出在Access中预定义的查询。

Try either: 尝试:

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

or 要么

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

Debug.Print myResult

In VBA you can pass arguments to a procedure/method without using parentheses, as long as the result isn't being being assigned to anything. 在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)

In the same way, when you call Debug.Print it assumes that db.Execute and result are actually separate arguments (it has no way of knowing that you want result to be passed to db.Execute first because there's nothing in your syntax to state that). 以同样的方式,当你调用Debug.Print它假定db.Executeresult实际上是单独的参数(它无法知道你希望result首先传递给db.Execute因为你的语法中没有任何内容可以说明那)。 So you have to use parentheses to let it know. 所以你必须使用括号让它知道。

It seems as the problem was that it is only possible to call updates with db.Execute and not queries. 似乎问题是只能用db.Execute调用更新而不是查询。 There is no good solution to print a whole table from a query with debug.print but using a RecordSet as seen in the following code is a possible way. 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM