Article ID: 148444
Article Last Modified on 5/7/2003
Query: Right Justify Orders
------------------------------------------------------------------
Field: Customer ID
Field: Expr1: Space(12-Len(Format([Order Date],"Short Date"))) & _
Format([Order Date],"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 are used again in step 5 below.
Field Name Data Type Start Width
---------- --------- ----- -----
Customer ID Text 1 10
Expr1 Text 11 12
Expr2 Text 23 15
Macro Action
------------
TransferText
TransferText Actions
--------------------
Transfer Type: Export Fixed Width
Specification Name: Orders Spec
Table Name: Right Justify Orders
File Name: C:\Orders.txt
Has Field Names: No
Option Explicit
Function CreateTextFile()
'This function creates a fixed-width text file using the
'Orders table in NWIND.MDB. The Customer ID field will
'be left-aligned and the Order Date and Freight fields
'will be right-aligned.
'Create a Dim statement for each field to export to the text
'file. For the data type of each field, use "String *" followed
'by the width of the field.
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.
'Create a recordset based on the Orders table.
Dim mydb As Database
Dim mytable As Table
Set mydb = CurrentDB()
Set mytable = mydb.OpenTable("Orders")
'Determine the index to sort the table by.
mytable.Index = "PrimaryKey" 'A primary key must exist in the
'Orders table.
'Create the text file. Note the use of the RSet statement
'to right-align a field. You can use the LSet statement if you
'want to left-align a field (or simply assign the variable
'without using RSet or LSet.)
Dim intFile As Integer
intFile = FreeFile
Open "C:\Orders.txt" For Output As intFile
'The following section is optional. It puts the field names in the
'first row of the text file. Remove the comment mark (') 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.
mytable.MoveFirst
Do Until mytable.EOF
LSet strCustomerId = mytable![Customer ID]
RSet strOrderDate = Format(mytable![Order Date], "Short Date")
RSet strFreight = Format(mytable![Freight], "Currency")
'Concatenate all of the variables together as in the following:
Print #intFile, strCustomerId & strOrderDate & strFreight
'The following optional line can be used to create a blank row
'after each record. Remove the comment mark (') from the
'following line if you want to create a blank row after each
'line.
'Print #intFile,
mytable.MoveNext
Loop
Close intFile
mytable.Close
mydb.Close
MsgBox "Text file has been created!"
End Function
Additional query words: justify
Keywords: kbhowto kbinterop kbprogramming KB148444