Debugging Recordset by printing to immediate window

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
Does anyone have on hand a iterative method to print a recordset to the immediate window. Preferably all columns and rows in a table manner with the field names at the top.
 
Last edited:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Code:
    Dim rs As ADODB.recordset, i As Long

    For i = 0 To rs.Fields.Count - 1
        Debug.Print rs.Fields(i).Name,
    Next
    Debug.Print
    For i = 0 To rs.Fields.Count - 1
        Debug.Print rs.Fields(i).Value,
    Next
 
Upvote 0
Ooops, I forgot to loop through the records. Use this instead:
Code:
    Dim rs As ADODB.recordset, i As Long

    For i = 0 To rs.Fields.Count - 1
        Debug.Print rs.Fields(i).Name,
    Next
    rs.MoveFirst
    Do Until rs.EOF
        Debug.Print
        For i = 0 To rs.Fields.Count - 1
            Debug.Print rs.Fields(i).Value,
        Next
        rs.MoveNext
    Loop
 
Upvote 0
Awesome. Any creative way to keep the spacing just right so it looks left aligned and a clean looking table? Might be to much to ask so if not now worries. Some rows just have big numbers and columns names are short and long.
 
Upvote 0
I would try the Format function and string concatenation to construct the output lines in the required layout. The semi-colon character ';' can be used instead of a comma in the above code to give closer spacing.
 
Upvote 0

Forum statistics

Threads
1,214,837
Messages
6,121,883
Members
449,057
Latest member
Moo4247

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top