我有一個記錄集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