Results 1 to 6 of 6
  1. #1
    pradeep.sands is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2013
    Posts
    49

    Question writing to text file (array set)

    Hi guys,
    I need some help. Consider I have a table, something like this:


    Click image for larger version. 

Name:	Unbenannt.JPG 
Views:	21 
Size:	13.5 KB 
ID:	13300

    Now I need to write a code from a form, in vba, to a text file such that my text file has this:
    {ab1,cd1,ef1,gh1}
    {ab2,cd2,ef2,gh2}
    {ab3,cd3,ef3,gh3}
    I am guessing, maybe I read number of records and columns from table (opened as recordset) and store them in two integer variables..
    introws = .recordcount
    intcolumns = .fields.count

    and now I move to first record (.movefirst), and then write all the columns of that record into {.......}
    and then move to next record and write all the columns of that record into a new {.....}
    and so on till .eof is reached..

    will this work???
    I have this idea, but I m unable to write the code to implement this....can someone help?
    if there is another easy way, you are welcome to help me

  2. #2
    TG_W is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2010
    Location
    Manvel, TX
    Posts
    299
    Have you tried DoCmd.OutputTo to a text file (acFormatTXT)?

  3. #3
    pradeep.sands is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2013
    Posts
    49
    Actually the table is a filtered table...I mean to say I filtered the actual table and opened as a record set.
    and now I will have to write data from this recordset (filtered table) and Not from the actual/original table

  4. #4
    TG_W is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2010
    Location
    Manvel, TX
    Posts
    299
    What about writing the recordset to a temporary table then outputting from there? You could have a delete SQL to clear the temp table at the start of the operation.

    EDIT

  5. #5
    pradeep.sands is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jun 2013
    Posts
    49
    Ya I can do that. But, if I use DoCmd.outputto then my output file doesnt look the way I want it to..
    I want my txt file to look exactly like this:

    {ab1,cd1,ef1,gh1}
    {ab2,cd2,ef2,gh2}
    {ab3,cd3,ef3,gh3}
    When I use DoCmd.outputto then my output txt file also looks like the table (with all borders/boxes shown)

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Try this:
    (NOTE: requires a reference to DAO)
    Code:
    Public Sub Export2Txt()
       Dim r As DAO.Recordset
       Dim k As Integer
       Dim sSQL As String
       Dim tmpString As String
       Dim path As String
    
       'open the text file
       k = FreeFile
       '  Change the text file name to whatever you want
       Open "Output.txt" For Output As #k
    
       'open the recordset
       'Change the SQL to your SQL
       sSQL = "SELECT [Field1], [Field2], [Field3], [Field4]  FROM [YourTable]"
       Set r = CurrentDb.OpenRecordset(sSQL)
    
       ' or if you have a saved query, use
       '   Set r = CurrentDb.OpenRecordset("YourFilteredQueryName")
    
       If Not r.BOF And Not r.EOF Then
          r.MoveLast
          r.MoveFirst
    
          'loop through the recordset
          Do While Not r.EOF
             tmpString = ""
             tmpString = "{" & r.Fields(0) & "," & r.Fields(1) & "," & r.Fields(2) & "," & r.Fields(3) & "}"
             Print #k, tmpString
             r.MoveNext
          Loop
       End If
    
       'clean up
       Close #k
       r.Close
       Set r = Nothing
    
       'just to let you know when it is over!!  :-)
       MsgBox "done"
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. File I/O - Writing page headers.
    By Robeen in forum Access
    Replies: 13
    Last Post: 05-10-2013, 03:31 PM
  2. Replies: 2
    Last Post: 12-27-2012, 09:37 AM
  3. Replies: 3
    Last Post: 07-30-2012, 02:16 PM
  4. Writing text to log file.
    By winsonlee in forum Programming
    Replies: 2
    Last Post: 08-05-2011, 12:52 PM
  5. ComboBox writing to text
    By tmcrouse in forum Forms
    Replies: 1
    Last Post: 11-18-2010, 09:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums