Results 1 to 5 of 5

Thread: Creating a text file with Tab-Separated fields

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    325

    Creating a text file with Tab-Separated fields

    I have a customer database and a table called accountInfo which contains addressing fields. I need to create an ascii text file that has 3 fields on each line. Those are email_address, first_name, and last_name. In order for this file to work with an email program, I must have the fields separated by tabs, not commas. Can anyone show me how to do this? Thanks.

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Creating a text file with Tab-Separated fields

    Populate an ADO recordset and dump it to the file using the GetString method.


    Code:
    Private Sub Command1_Click()
        Dim db As ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim lngFile As Long
        
        Set db = New ADODB.Connection
        db.Open "provider=microsoft.jet.oledb.4.0;data source=m:\testing\nwind2.mdb"
        
        Set rs = db.Execute("Select CustomerId, CompanyName, Country From Customers")
        
        lngFile = FreeFile
        
        Open "C:\Customers.dat" For Binary As #lngFile
        Put #lngFile, , rs.GetString(adClipString, , vbTab, vbNewLine)
        Close #lngFile
        
        rs.Close
        db.Close
    End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    325

    Re: Creating a text file with Tab-Separated fields

    Thanks so much. This program uses DAO instead of ADO. What would change?

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Creating a text file with Tab-Separated fields

    You need to iterate through the entire recordset.
    Code:
    Private Sub Command2_Click()
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim lngFile As Long
        Dim aData As Variant
        
        Set db = DAO.DBEngine.OpenDatabase("m:\testing\nwind2.mdb")
            
        Set rs = db.OpenRecordset("Select CustomerId, CompanyName, Country From Customers", dbOpenForwardOnly, dbReadOnly)
        
        lngFile = FreeFile
        
        Open "C:\Customers.dat" For Output As #lngFile
        
        Do Until rs.EOF
            Print #lngFile, rs.Fields(0); vbTab; rs.Fields(1).Value; vbTab; rs.Fields(2).Value
            rs.MoveNext
        Loop
    
        Close #lngFile
        
        rs.Close
        db.Close
    End Sub

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    325

    Re: Creating a text file with Tab-Separated fields

    This is perfect, thanks.

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