2011-12-14 45 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