vba 在立即窗口中查看记录集的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8503190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:17:35  来源:igfitidea点击:

view data of recordset in immediate window

ms-accessvbaaccess-vba

提问by Heman

I have a recordset rstwith 2 columns/fields IDand Value. The recordset has multiple rows. While debugging, I am able to view records in the first row of the recordset in the immediate window using the following statements.

我有一个rst包含 2 列/字段IDValue. 记录集有多行。在调试时,我可以使用以下语句在即时窗口中查看记录集第一行中的记录。

?rst.fields(0)
?rst.fields(1)

But I am not able to view data in 2nd or 100th row?

但我无法查看第 2 行或第 100 行的数据?

回答by Fionnuala

Regarding moving through a DAO recordset and the comment by @nyarlathotep:

关于通过 DAO 记录集和@nyarlathotep 的评论:

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM tbljournaltitles")

Debug.Print rs(0).Name
Debug.Print rs(0)
Debug.Print rs("MyText")

rs.Move 10
Debug.Print rs(0)

rs.Move -4
Debug.Print rs(0)

''Find does not work with all recordset types
rs.FindFirst "MyText Like 'Text*'"
Debug.Print rs(0)

回答by codeling

You'd have to iterate over the rows to get their data. You could e.g. do the following simple loop:

您必须遍历行以获取其数据。例如,您可以执行以下简单循环:

Do While Not rst.EOF
    'Do something with the data
    rst.MoveNext
Loop

回答by Daniel Neumann

Leveraging answers from @Fionnualla and @codeling (and adding closing & cleanup for the recordset), also adding help from VBA: Debug.Print without newline?to make this look more like a table (still need to work on making the columns the actual width of the max size of the col).

利用@Finnualla 和@codeling 的答案(并为记录集添加关闭和清理),还添加了来自VBA 的帮助:Debug.Print without newline?使它看起来更像一个表格(仍然需要使列成为 col 最大大小的实际宽度)。

This procedure will print out any query you drop onto it.

此过程将打印出您放置在其上的任何查询。

Public Sub debugPrintQuery(ByVal myQuery As String)
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset(myQuery)

' print column names
Dim i As Integer
For i = 0 To rs.Fields.Count - 2
    Debug.Print rs(i).Name & vbTab; 'print col names separated with a tab one from each other
Next i
Debug.Print rs(rs.Fields.Count - 1).Name 'last one without ; so it adds the newline

Do While Not rs.EOF
    For i = 0 To rs.Fields.Count - 2
        Debug.Print rs(i) & vbTab; 'print values separated with a tab one from each other
    Next i
    Debug.Print rs(rs.Fields.Count - 1) 'last one without ; so it adds the newline
    rs.MoveNext
Loop

rs.Close 'Close the recordset
Set rs = Nothing 'Clean up

End Sub