Create Recordset and Export to Text File (1 Viewer)

jketcher

Registered User.
Local time
Today, 06:15
Joined
Apr 15, 2009
Messages
77
I recently got some help with filtering data in a table using multiple combo boxes. Now I need to export that filtered data to a text file. The data is displayed in a subform [Marketing subform]. How do I do this? Do I need to create a recordset from the data that is displaying in the subform? If yes, then I need help with the syntax. Someone previously sent me instructions for exporting a form's recordset to excel. How do I export to a tab delimited text file? I am not selecting all of the fields on the subform. I only want a few of them.
 

DCrake

Remembered
Local time
Today, 14:15
Joined
Jun 8, 2005
Messages
8,632
You need to be using the Open filename for Output As #1 and enumerating through the recordset building up a row of data to push into the text file.

David
 

jketcher

Registered User.
Local time
Today, 06:15
Joined
Apr 15, 2009
Messages
77
Sorry -- I need an example with explanation.

Thanks, jketcher
 

DCrake

Remembered
Local time
Today, 14:15
Joined
Jun 8, 2005
Messages
8,632
Firstly what are you skills on vba from a scale of 1 to 10. This will determing the level of feedback required.

David
 

jketcher

Registered User.
Local time
Today, 06:15
Joined
Apr 15, 2009
Messages
77
I would say I am a 4, but am a touch critic since I used to be a COBOL programmer. I did get this to work though. Found some other feedback before I got your response. VBA is making more sense but some of the syntax seems like gibberish.
 

DCrake

Remembered
Local time
Today, 14:15
Joined
Jun 8, 2005
Messages
8,632
Ok here goes

Code:
Dim fFile As Long
Dim strFile As String
Dim strString As String
Dim Rs As Recordset
Dim RsSql As String


fFile = Freefile
'Nominate the output text file path and name
strFile = "C:\FullPath\TextFileName.Txt"

'***You may want to put a check in here to test if the file
'***already exists, and as the user if they want to overwrite it first
'***Use the Kill strFile to delete it first

'Create a snapshot of data from your table with the desired fields and data
RsSql = "Select Field1, Field2, Field5, Etc From TblTableName Where Condition = '" & Criteria & "'"

'Open the recordset and test for EOF
Set Rs = CurrentDb.OpenRecordset(RsSql)

If Not Rs.EOF and Not Rs.BOF Then
   Open strFile For Output As #fFile
      Do Until Rs.EOF
          'Loop across all fields in recordset delimiting them with a tab key
           For i = 0 To Rs.Fields.Count -1
               strString = StrString & Rs(i) & vbTab
           Next
           'Output the resulting string to the text file
           Print #ffile, strString
           'Reset the string to empty for the next record
           strString = ""
           'Move to the next record in the recordset
           Rs.MoveNext
      Loop
      'Close the recordset and the text file
      Rs.Close
      Close #fFile
End If
'Destroy the instance of the recordset from memory
Set Rs = Nothing


You need to place this code in a module as a function or a sub procedure with a name that is not the same as the module itself.

Remember to change the field and table names in the RsSql to match yours.

All above is aircode and untested but I should be correct.


David
 

Users who are viewing this thread

Top Bottom