2017-03-03 88 views
1

我想通过VBA执行预定义的查询访问并将其打印到调试输出中。查询设计的名称位于名为report的变量中。如何在VBA/Access中查询设计的查询结果debug.print

我期待,它将工作与:

debug.print db.Execute report 

但每次VBA它autocorrects到:

debug.print db.Execute; report 

如果我理解正确的话,该;代表一个新的生产线,因此使得对我而言没有意义。但在这两种情况下,无论是否有新行,我都会遇到错误。我认为简单的问题是,这不是正确的语法。

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

回答

0

看起来问题在于它只能用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 
3

尝试之一:

'// 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第一,因为有没有在你的语法是对的方式说明)。所以你必须使用圆括号让它知道。

+0

在两种情况下,我都收到错误“Function or variable expanded”。错误点在。执行 – Florian

+0

它接缝,因为数据库不存在,但两行后没有debug.print它的工作没有任何问题 – Florian

+0

我需要看到所有的代码来尝试猜测为什么,但它也将失去这个问题的范围在于询问别的东西(这是关于出现在你的陈述中的“;”)。我建议单独发布有关该问题的新问题并提供所需的所有代码 –