简体   繁体   English

Debug.打印表格中的所有数据

[英]Debug.Print all data in a table

I am trying to use recordset code to loop through all the fields in a table and debug.print their values and field names in an order you would naturally read the table ie from left to right across columns then onto the row below我正在尝试使用记录集代码循环遍历表中的所有字段并调试。打印它们的值和字段名称的顺序你自然会读取表,即从左到右跨列然后到下面的行

这是一个示例表

I have accomplished what I'm trying to do but only for the first row.我已经完成了我想做的事情,但只针对第一行。 This is the code:这是代码:

Sub RecordSets()
Dim db As Database
Dim rs As Recordset
Dim i As Long
Set db = CurrentDb
Set rs = db.OpenRecordset("tbl1")

        For i = 0 To rs.Fields.Count - 1
                Debug.Print rs.Fields(i).Name
                Debug.Print rs.Fields(i).Value
        Next

rs.Close
db.Close
End Sub

Immediate window produces following result:立即 window 产生以下结果:

    Category
    Clothing
    Item
    Shirt
    Price
    5

This is the top row and is exactly as I want.这是顶行,完全符合我的要求。 But I cannot get any code to work accomplish this exact same thing for the other rows.但是我无法让任何代码为其他行完成完全相同的事情。 I am 99% sure I need to use a Do Until.EOF loop in conjunction with the For...Next loop but I can't get the results whatever I try or I lock access up in an infinite query.我 99% 确定我需要将 Do Until.EOF 循环与 For...Next 循环结合使用,但无论我尝试什么都无法获得结果,或者我在无限查询中锁定了访问权限。

Thanks for your help谢谢你的帮助

Untested:未经测试:

Sub RecordSets()
    Const SEP as String = vbTab
    Dim db As Database
    Dim rs As Recordset, numFlds As Long
    Dim i As Long, s As String, sp as string
    Set db = CurrentDb
    Set rs = db.OpenRecordset("tbl1")

    numFlds = rs.Fields.Count

    'print the headers (field names)
    For i = 0 To numFlds  - 1
        s = s & sp & rs.Fields(i).Name
        sp = SEP '<< add separator for subsequent items
    Next
    Debug.Print s

    'print the data
    sp = ""  '<< clear the separator
    Do While Not rs.EOF
        For i = 0 To numFlds - 1
            s = s & sp & rs.Fields(i).Name
            sp = SEP
        Next
        Debug.Print s
        rs.MoveNext
    Loop

    rs.Close
    db.Close
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM