Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.
NOTE: The sample code in this article uses Microsoft Data Access Objects. For this code to run properly, you must reference the Microsoft DAO 3.6 Object Library. To do so, click
References on the
Tools menu in the Visual Basic Editor, and make sure that the
Microsoft DAO 3.6 Object Library check box is selected.
The sample code contained in this article works with separate recordset objects, and uses sequential file statements to export all the data. This example uses two queries to split the Customers table in Northwind.mdb. To preserve the integrity of the data, both queries should have the same number of records, and they should be sorted in the same order.
- Create the first query as follows:
Query: qryCustomers1
--------------------
Type: Select Query
Field: CustomerID
Table: Customers
Field: CompanyName
Table: Customers
Field: ContactName
Table: Customers
Field: ContactTitle
Table: Customers
- Create the second query as follows:
Query: qryCustomers2
--------------------
Type: Select Query
Field: Address
Table: Customers
Field: City
Table: Customers
Field: Region
Table: Customers
Field: PostalCode
Table: Customers
Field: Country
Table: Customers
Field: Phone
Table: Customers
Field: Fax
Table: Customers
- Create a new module, and enter the following line in the Declarations
section if it is not already there:
- Enter the following procedure in the module:
Sub WriteFlatFile(sFileName As String, sDelimiter As String)
On Error GoTo WriteFileErrors
Dim rst1 As DAO.Recordset
Dim rst2 As DAO.Recordset
Dim fld1 As DAO.Field
Dim fld2 As DAO.Field
'Counter variable used to print the delimiter except after
'the last field for rst2
Dim intCount As Integer
'Delete the specified file if it already exists
If Dir(sFileName) <> "" Then
If MsgBox("The file you entered already exists. Would you " _
& "like to delete it?", vbExclamation + vbYesNo) = vbYes Then
Kill sFileName
Else
Exit Sub
End If
End If
'Create a separate recordset for each query
Set rst1 = CurrentDb().OpenRecordset("qryCustomers1")
Set rst2 = CurrentDb().OpenRecordset("qryCustomers2")
'Open the file using the Open statement
Open sFileName For Output As #1
'Write the header row from both recordsets using the Print statement
For intCount = 0 To rst1.Fields.Count - 1
Print #1, rst1(intCount).Name & sDelimiter;
Next
'This uses a counter variable to see determine if we are writing the
'last field. If we are, do not print the delimiter. Only check this
'for the end of the second recordset, to make sure there is a delimiter
'between the 2 recordsets
For intCount = 0 To rst2.Fields.Count - 1
If intCount < rst2.Fields.Count - 1 Then
Print #1, rst2(intCount).Name & sDelimiter;
Else
Print #1, rst2(intCount).Name;
End If
Next
'Write new line
Print #1,
'Write the data from each recordset
Do While Not rst1.EOF And Not rst2.EOF
For intCount = 0 To rst1.Fields.Count - 1
Print #1, rst1(intCount).Value & sDelimiter;
Next
rst1.MoveNext
For intCount = 0 To rst2.Fields.Count - 1
If intCount < rst2.Fields.Count - 1 Then
Print #1, rst2(intCount).Value & sDelimiter;
Else
Print #1, rst2(intCount).Value;
End If
Next
rst2.MoveNext
'write to new line
Print #1,
Loop
MsgBox "File has been written to " & sFileName
WriteFileExit:
'Close objects and destroy DAO object variables
rst1.Close
rst2.Close
Set rst1 = Nothing
Set rst2 = Nothing
'close the sequential file opened earlier
Close #1
Exit Sub
WriteFileErrors:
MsgBox "An error has occurred: " & vbCrLf & Err.Number & " " & Err.Description
Resume WriteFileExit
End Sub
- To test this procedure, type the following line in the Immediate window, and then press ENTER:
WriteFlatFile "C:\TestFile.txt",","
Note that a comma delimited file, C:\TestFile.txt, has been created, and that the entire Customers table has been exported to this file.