我有一个记录集rst
,具有2列/字段ID
和Value
。记录集有多行。在调试过程中,我可以使用以下语句查看立即窗口中记录集第一行的记录。即时窗口中查看记录集的数据
?rst.fields(0)
?rst.fields(1)
但是我无法查看第2或第100行的数据吗?
我有一个记录集rst
,具有2列/字段ID
和Value
。记录集有多行。在调试过程中,我可以使用以下语句查看立即窗口中记录集第一行的记录。即时窗口中查看记录集的数据
?rst.fields(0)
?rst.fields(1)
但是我无法查看第2或第100行的数据吗?
通过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)
您必须遍历行才能获取其数据。你可以例如执行以下操作简单的循环:
Do While Not rst.EOF
'Do something with the data
rst.MoveNext
Loop
利用从@Fionnualla和@codeling(答案和添加闭&清理用于记录),还可以从VBA: Debug.Print without newline?中添加帮助,使其看起来更像一张表格(仍然需要使列成为col的最大大小的实际宽度)。
此过程将打印出您放入它的任何查询。
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
没有直接回答你的问题,但我发现DoCmd.OpenQuery“someQueryDef”从即时窗口时非常有用,我想浏览过一组查询结果。 – chip 2011-12-14 16:39:20