Printing a Recordset to a Text Output File:
Return to List
Option Base 1
Public Sub Main()
On Error GoTo BYE
' Declare Locals Dim rs As ADODB.Recordset
Dim strSQL As String
Dim aryCustomerInfo()
Dim lngCount As Long
lngCount = 0
' Fill aryCustomerInfo() array with data strSQL = "SELECT * FROM tblCustomerAddress
Set rs = New ADODB.Recordset
rs.Open strSQL, CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly
Do Until rs.EOF
lngCount = lngCount + 1
ReDim Preserve aryCustomerInfo(3, lngCount)
aryCustomerInfo(1, lngCount) = rs![CustomerName]
aryCustomerInfo(2, lngCount) = rs![CustomerPhone]
aryCustomerInfo(3, lngCount) = rs![CustomerAddress]
rs.MoveNext
Loop
rs.Close
CurrentProject.Connection.Close
Set rs = Nothing
' Output array info to text file aFileNum = FreeFile
Open "C:\Report.txt" For Output As #aFileNum
For i = 1 to UBound(aryCustomerInfo(), 2)
Print #aFileNum, "Customer name: " & aryCustomerInfo(1, i)
Print #aFileNum, "Customer phone: " & aryCustomerInfo(2, i)
Print #aFileNum, "Customer address: " & aryCustomerInfo(3, i)
Print #aFileNum, "****************************"
Next
Close #aFileNum
Btn = MsgBox("Report Saved!" & vbCrLf & vbCrLf & _
"Do you wish to Open in Notepad?", vbYesNo, _
"Report Output Results")
Shell "Notepad.exe C:\Report.txt", vbMaximizedFocus
End If
BYE:
End Sub