|
-
Oct 18th, 2002, 11:15 AM
#1
Thread Starter
Fanatic Member
Writing to a text file
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
-
Oct 18th, 2002, 11:36 AM
#2
PowerPoster
You may want to try this:
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
This procedure assumes that connection is already open.
-
Oct 18th, 2002, 02:53 PM
#3
Thread Starter
Fanatic Member
Help?
It doesn't seem to work. I get data.txt created but nothing is in it but blank lines. Any ideas? Thanks, Jeremy
-
Oct 18th, 2002, 03:01 PM
#4
PowerPoster
Post your code so I can see what's wrong with it.
-
Oct 18th, 2002, 03:04 PM
#5
Thread Starter
Fanatic Member
lol
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 the Thanks for your help, Jeremy
-
Oct 18th, 2002, 03:10 PM
#6
Software Eng.
It might also be helpful to open the file in "Random" mode. This feature will (somewhat) mimic the recordset aspect of a database.
-
Oct 18th, 2002, 03:26 PM
#7
PowerPoster
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.
-
Oct 18th, 2002, 03:28 PM
#8
Thread Starter
Fanatic Member
I had to.
I had to do the .move first or it would hang up on the .movenext. I'll do that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|