【问题标题】:view data of recordset in immediate window在即时窗口中查看记录集的数据
【发布时间】:2011-12-14 10:47:52
【问题描述】:

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

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

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

【问题讨论】:

  • 不是您问题的直接答案,但当我想浏览一组查询结果时,我发现 DoCmd.OpenQuery "someQueryDef" 在即时窗口中非常有用。

标签: ms-access vba


【解决方案1】:

利用来自@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

【讨论】:

    【解决方案2】:

    关于移动 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)
    

    【讨论】:

    • 谢谢,这很有帮助。
    【解决方案3】:

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

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

    【讨论】:

    • 感谢这项工作。但是除了移动到该行之外,是否有任何命令可以查看特定行和列的数据。因为我必须使用 movenext 100 次才能查看第 100 行的数据。
    • 在使用记录集时,并非所有数据都必须同时在内存中,所以我认为除了迭代之外别无他法。但是您可以将行存储在您自己的某种数据结构中,然后检查该数据结构
    • @user1075037 确实可以在不迭代的情况下移动记录集。我已经发布了对此答案的补充。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 2015-03-26
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多