I need to write the contents of my recordset to a text file at the click of a button. I have everything ready but I don't know how to approach this as I am new to VB. Any help would be appreciated. Thanks, Jeremy
Printable View
I need to write the contents of my recordset to a text file at the click of a button. I have everything ready but I don't know how to approach this as I am new to VB. Any help would be appreciated. Thanks, Jeremy
You may want to try this:
This procedure assumes that connection is already open.VB Code:
Public Sub OpenRecordset(rstName As String) '============================================ Dim strSQL As String Dim i%, strData As String On Local Error Resume Next strSQL = "Select * From " & rstName & ";" Set adoRecordset = New ADODB.Recordset With adoRecordset .CursorType = adOpenKeyset .LockType = adLockOptimistic .Open rstName, adoConnection, , , adCmdTable Open App.Path & "\data.txt" For Output As #1 Do For i = 0 To .Fields.Count - 1 strData = strData & .Fields(i).Value & vbTab Next i Print #1, strData & vbNewLine .MoveNext Loop Until .EOF Close #1 End With End Sub
It doesn't seem to work. I get data.txt created but nothing is in it but blank lines. Any ideas? Thanks, Jeremy
Post your code so I can see what's wrong with it.
VB Code:
Private Sub Command1_Click() Open App.Path & "\data.txt" For Output As #1 OraRec.MoveFirst Do For i = 0 To OraRec.Fields.Count - 1 strData = strData & OraRec.Fields(i).Value & vbTab Next i Print #1, strData & vbNewLine OraRec.MoveNext Loop Until OraRec.EOF Close #1 End Sub
This code is your but modified to run with the recordset already open and to operate with out theThanks for your help, JeremyVB Code:
With
It might also be helpful to open the file in "Random" mode. This feature will (somewhat) mimic the recordset aspect of a database.
You don't really have use MoveFirst but that's ok. Place a breakpointer on this line:
strData = strData & OraRec.Fields(i).Value & vbTab
to see if strData has any value in it.
I had to do the .move first or it would hang up on the .movenext. I'll do that.