2011-12-14 50 views
2

我有一個記錄集rst,具有2列/字段IDValue。記錄集有多行。在調試過程中,我可以使用以下語句查看立即窗口中記錄集第一行的記錄。即時窗口中查看記錄集的數據

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

但是我無法查看第2或第100行的數據嗎?

+1

沒有直接回答你的問題,但我發現DoCmd.OpenQuery「someQueryDef」從即時窗口時非常有用,我想瀏覽過一組查詢結果。 – chip 2011-12-14 16:39:20

回答

6
關於

通過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) 
1

您必須遍歷行才能獲取其數據。你可以例如執行以下操作簡單的循環:

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

感謝這個作品。但有沒有任何命令可以查看特定行和列的數據,而不是移動到該行。因爲在我可以查看第100行數據之前,我必須使用100次movenext。 – Heman 2011-12-14 10:56:45

+0

使用記錄集時,並非所有數據都必須在內存中,所以我認爲沒有其他方法可以迭代。但是您可以將行存儲在您自己的某種數據結構中,然後檢查該數據結構 – codeling 2011-12-14 10:58:28

0

利用從@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