Article ID: 150401
Article Last Modified on 1/19/2007
Field: CustomerID
Field: Expr1: Space(12-Len(Format([OrderDate],"Short Date"))) &
Format([OrderDate],"Short Date")
Field: Expr2: Space(15-Len(Format([Freight],"Currency"))) &
Format([Freight],"Currency")
NOTE: The Space() function is used to pad spaces at the beginning of
the field. The numbers 12 and 15 at the start of the Space() function
indicate the desired total width of that field in the text file. These
numbers will be used again in Step 7.
Field Name Start Width
---------- ----- -----
CustomerID 1 10
Expr1 11 12
Expr2 23 15
Public Function CreateTextFile()
'This function creates a fixed-width text file using the Orders table
'in Northwind.mdb. The CustomerID field will be left-justified
'while the OrderDate and Freight fields will be right-justified.
Dim strCustomerId As String * 10 'specifies width of 10 characters
Dim strOrderDate As String * 12 'specifies width of 12 characters
Dim strFreight As String * 15 'specifies width of 15 characters
Dim mydb As Database, myset As Recordset
Dim intFile As Integer
Set mydb = CurrentDb()
Set myset = mydb.OpenRecordset("Orders", dbOpenTable)
myset.Index = "PrimaryKey" 'Orders table must have primary key.
intFile = FreeFile
Open "C:\My Documents\Orders.txt" For Output As intFile
'The following section is optional. Remove the comment (') from
'these lines if you want to put field names in the first row of
'the text file.
'LSet strCustomerId = "CustomerID"
'RSet strOrderDate = "OrderDate"
'RSet strFreight = "Freight"
'Print #intFile, strCustomerId & strOrderDate & strFreight
'This section puts the records from the Orders table in the text
'file.
myset.MoveFirst
Do Until myset.EOF
LSet strCustomerId = myset![CustomerID] 'Field name in brackets
RSet strOrderDate = Format(myset![OrderDate], "Short Date")
RSet strFreight = Format(myset![Freight], "Currency")
'Concatenate all of the variables together as in the following:
Print #intFile, strCustomerId & strOrderDate & strFreight
myset.MoveNext
Loop
Close intFile
myset.Close
mydb.Close
MsgBox "Text file has been created!"
End Function148444 Exporting Right-Aligned Fields to a Text File (1.x, 2.0)
98663 Exporting to Fixed-Width Text File Left-Aligns Numbers
Additional query words: align right justify open statement
Keywords: kbhowto kbprogramming KB150401