Working with Files – Part 4
Writing Sequential Files Using the Write # and Print # Statements
The Write # statement
The Write # statement is used to write records with comma-delimited fields to a sequential file. The basic format is:
Write#<filenumber>, <variable list>
· filenumber refers to the file that was Opened As that number (for Output or Append) in the Open statement
· variable list is a list of variables, separated by commas, which will be written to the file as the fields of each record
The Write # statement will automatically enclose string fields in quotes and date fields in pound signs.
Example – Writing a comma-delimited record:
Consider the following program segment, which repeatedly prompts the user to enter employee data via the InputBox function in a loop until the user enters nothing for the employee name. The InputBox function automatically converts the user's entries into the target data types. On each pass through the loop, a record containing the data that the user entered is appended to the employee file.
Dim strEmpFileName As String
Dim strBackSlash As String
Dim intEmpFileNbr As Integer
Dim strEmpName As String
Dim intDeptNbr As Integer
Dim strJobTitle As String
Dim dtmHireDate As Date
Dim sngHrlyRate As Single
strBackSlash = IIf(Right$(App.Path, 1) = "\", "", "\")
strEmpFileName = App.Path & strBackSlash & "EMPLOYEE.DAT"
intEmpFileNbr = FreeFile
Open strEmpFileName For Append As #intEmpFileNbr
' Set up an "input loop" to prompt the user for data.
' "Priming" input:
strEmpName = InputBox("Enter name (or click Cancel to stop):")
' The loop will continue until the user clicks "Cancel" on the InputBox (which
' will cause the result variable of InputBox (strEmpName) to be set to a zero-
' length string ("") ...
Do Until strEmpName = ""
' Prompt the user for each field:
intDeptNbr = InputBox("Enter dept:")
strJobTitle = InputBox("Enter job title:")
dtmHireDate = InputBox("Enter hire date:")
sngHrlyRate = InputBox("Enter hourly rate:")
' Write out the record to the file ...
Write #intEmpFileNbr, strEmpName, intDeptNbr, strJobTitle, dtmHireDate, sngHrlyRate
' Display the contents of the record that was written on the form ...
Print strEmpName; _
Tab(25); Format$(intDeptNbr, "@@@@"); _
Tab(35); strJobTitle; _
Tab(60); Format$(dtmHireDate, "mm/dd/yyyy"); _
Tab(71); Format$(Format$(sngHrlyRate, "Standard"), "@@@@@@@")
' Start a new record by prompting for the employee name ...
strEmpName = InputBox("Enter name (or click Cancel to stop):")
Loop
Close #intEmpFileNbr
In the example above, suppose the user responded to the prompts as follows:
Prompt Response
Enter name FREDDIE FUDRUCKER
Enter dept 450
Enter job title DATA ENTRY CLERK
Enter hire date April 22, 1999
Enter hourly rate 9.00
The record would be written to the file as:
"FREDDIE FUDRUCKER",450,"DATA ENTRY CLERK",#1999-04-22#,9
Note that the Write statement writes Date fields to the file in the format #YYYY-MM-DD#. This is the standard format for dates stored on comma-delimited files, although VB can handle dates stored as #MM/DD/YYYY# as was used earlier. In any event, be sure to always use four digits for the year; otherwise, you will be relying on VB's internal algorithm to "figure out" what date you want, and the result may not always be what you expect.
The code above is the code behind the sample program's "Try It" button. When you run the sample program, The intial prompt for the employee name is displayed:
This is followed by subsequent prompts for the rest of the fields of the record:
Once the field entries for a record have been made, the record is written to the output file (which you can later verify by looking at it in the Windows Notepad program), the contents of the new record are displayed on the form, and the prompting for a new record starts again with a prompt for the employee name. (If you do not want to enter more records, click Cancel on the prompt for employee name.)
Download the VB project code for the example above here.
The Print # statement
The Print # statement is used to write formatted strings of data to a sequential file. The basic format is:
Print #<filenumber>, [expression list]
· filenumber refers to the file that was Opened As that number (for Output or Append) in the Open statement
· expression list is a list of one or more expressions, variables, or constants, separated by semicolons or commas, and which may include the TAB(n) and SPC(n) functions.
To create records in the fixed-width format ("print" format) shown earlier, you could substitute the following Print # statement for the Write # statement in the previous example:
Print #intEmpFileNbr, strEmpName; Tab(21); Format$(intDeptNbr, "@@@@"); _
Tab(30); strJobTitle; _
Tab(51); Format$(dtmHireDate, "m/d/yyyy"); _
Tab(61); Format$(Format$(sngHrlyRate, "#0.00"), "@@@@@")
Given the same sample input as in the previous example, the record would be written to the file as follows:
1 1 2 2 3 3 4 4 5 5 6 6
1...5....0....5....0....5...0....5....0....5....0....5....0....5.
FREDDIE FUDRUCKER 450 DATA ENTRY CLERK 4/22/1999 9.00
Download the VB project code for the example above here.
To create records in the fixed-width format ("COBOL-style" format) shown earlier, you could substitute the following Print # statement for the Write # statement in the previous example:
Print #intEmpFileNbr, strEmpName; Tab(21); Format$(intDeptNbr, "0000"); _
strJobTitle; Tab(43); Format$(dtmHireDate, "mm"); _
Format$(dtmHireDate, "dd"); Format$(dtmHireDate, "yyyy"); _
Format$(sngHrlyRate * 100, "0000")
Given the same sample input as in the previous example, the record would be written to the file as follows:
1 1 2 2 3 3 4 4 5 5 6 6
1...5....0....5....0....5...0....5....0....5....0....5....0....5.
FREDDIE FUDRUCKER 0450DATA ENTRY CLERK 042219990900
Download the VB project code for the example above here.
To demonstrate any of the Write/Print examples above: