Results 1 to 8 of 8

Thread: Writing to a text file

  1. #1

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677

    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

  2. #2
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    You may want to try this:
    VB Code:
    1. Public Sub OpenRecordset(rstName As String)
    2. '============================================
    3. Dim strSQL As String
    4. Dim i%, strData As String
    5.  
    6. On Local Error Resume Next
    7.  
    8.     strSQL = "Select * From " & rstName & ";"
    9.     Set adoRecordset = New ADODB.Recordset
    10.     With adoRecordset
    11.         .CursorType = adOpenKeyset
    12.         .LockType = adLockOptimistic
    13.         .Open rstName, adoConnection, , , adCmdTable
    14.         Open App.Path & "\data.txt" For Output As #1
    15.             Do
    16.                 For i = 0 To .Fields.Count - 1
    17.                     strData = strData & .Fields(i).Value & vbTab
    18.                 Next i
    19.                 Print #1, strData & vbNewLine
    20.                 .MoveNext
    21.             Loop Until .EOF
    22.         Close #1
    23.     End With
    24.  
    25. End Sub
    This procedure assumes that connection is already open.
    Roy

  3. #3

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677

    Help?

    It doesn't seem to work. I get data.txt created but nothing is in it but blank lines. Any ideas? Thanks, Jeremy

  4. #4
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    Post your code so I can see what's wrong with it.
    Roy

  5. #5

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677

    lol

    VB Code:
    1. Private Sub Command1_Click()
    2.     Open App.Path & "\data.txt" For Output As #1
    3.             OraRec.MoveFirst
    4.             Do
    5.                 For i = 0 To OraRec.Fields.Count - 1
    6.                     strData = strData & OraRec.Fields(i).Value & vbTab
    7.                 Next i
    8.                 Print #1, strData & vbNewLine
    9.                 OraRec.MoveNext
    10.             Loop Until OraRec.EOF
    11.         Close #1
    12. End Sub

    This code is your but modified to run with the recordset already open and to operate with out the
    VB Code:
    1. With
    Thanks for your help, Jeremy

  6. #6
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286
    It might also be helpful to open the file in "Random" mode. This feature will (somewhat) mimic the recordset aspect of a database.

  7. #7
    PowerPoster
    Join Date
    Aug 2002
    Location
    NY, NY
    Posts
    2,139
    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.
    Roy

  8. #8

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677

    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
  •  



Click Here to Expand Forum to Full Width