Dim fnum As Integer Dim file_name As String Dim database_name As String Dim db As Database Dim rs As Recordset Dim num_fields As Integer Dim field_width() As Integer Dim field_value As String Dim i As Integer Dim num_processed As Integer On Error GoTo MiscError ' Open the output file. fnum = FreeFile file_name = txtFileName.Text Open file_name For Output As fnum ' Open the database. Set db = OpenDatabase(txtDatabaseName.Text) ' Open the recordset. Set rs = db.OpenRecordset( _ "SELECT * FROM Books ORDER BY Title") ' Start with the names of the fields. num_fields = rs.Fields.Count ReDim field_width(0 To num_fields - 1) For i = 0 To num_fields - 1 ' We're only working with Text here. Other ' types are different. For example, an ' integer may take 2 bytes to store but 6 ' characters to display. field_width(i) = rs.Fields(i).Size If field_width(i) < Len(rs.Fields(i).Name) Then field_width(i) = Len(rs.Fields(i).Name) End If field_width(i) = field_width(i) + 1 Print #fnum, rs.Fields(i).Name; Print #fnum, Space$(field_width(i) - _ Len(rs.Fields(i).Name)); Next i Print #fnum, "" ' Process the records. Do While Not rs.EOF num_processed = num_processed + 1 For i = 0 To num_fields - 1 field_value = rs.Fields(i).Value Print #fnum, field_value & _ Space$(field_width(i) - _ Len(field_value)); Next i Print #fnum, "" rs.MoveNext Loop ' Close the file and database. rs.Close db.Close Close fnum MsgBox "Processed " & _ Format$(num_processed) & " records." Exit SubMiscError: MsgBox "Error " & Err.Number & _ vbCrLf & Err.Description End Sub Private Sub Form_Load() txtDatabaseName.Text = App.Path & "\books.mdb" txtFileName.Text = App.Path & "\books.txt" End Sub
Dim file_name As String
Dim database_name As String
Dim db As Database
Dim rs As Recordset
Dim num_fields As Integer
Dim field_width() As Integer
Dim field_value As String
Dim i As Integer
Dim num_processed As Integer On Error GoTo MiscError ' Open the output file.
fnum = FreeFile
file_name = txtFileName.Text
Open file_name For Output As fnum ' Open the database.
Set db = OpenDatabase(txtDatabaseName.Text) ' Open the recordset.
Set rs = db.OpenRecordset( _
"SELECT * FROM Books ORDER BY Title") ' Start with the names of the fields.
num_fields = rs.Fields.Count
ReDim field_width(0 To num_fields - 1)
For i = 0 To num_fields - 1
' We're only working with Text here. Other
' types are different. For example, an
' integer may take 2 bytes to store but 6
' characters to display.
field_width(i) = rs.Fields(i).Size
If field_width(i) < Len(rs.Fields(i).Name) Then
field_width(i) = Len(rs.Fields(i).Name)
End If
field_width(i) = field_width(i) + 1
Print #fnum, rs.Fields(i).Name;
Print #fnum, Space$(field_width(i) - _
Len(rs.Fields(i).Name));
Next i
Print #fnum, "" ' Process the records.
Do While Not rs.EOF
num_processed = num_processed + 1
For i = 0 To num_fields - 1
field_value = rs.Fields(i).Value
Print #fnum, field_value & _
Space$(field_width(i) - _
Len(field_value));
Next i
Print #fnum, ""
rs.MoveNext
Loop ' Close the file and database.
rs.Close
db.Close
Close fnum
MsgBox "Processed " & _
Format$(num_processed) & " records." Exit SubMiscError:
MsgBox "Error " & Err.Number & _
vbCrLf & Err.Description
End Sub
Private Sub Form_Load()
txtDatabaseName.Text = App.Path & "\books.mdb"
txtFileName.Text = App.Path & "\books.txt"
End Sub