Using the Print # statement, you can print data to a file using the following form:
Print #filenumber[, outputlist] The information for this statement is basically the same as for Write #. The only difference is that Write # sends the data to the named file and Print # creates a new print file. To insert a space in outputlist, use the Spc(n) setting where n is the number of spaces to insert. Likewise, to insert a tab, use the Tab(n) setting, where n represents the absolute value of the column. The following procedure gleans the entire contents of a file and then creates a new print file:
Sub PrintPoundSign(filSource As String, filPrint As String) 'Print data from passed file 'in Immediate window. Dim hFileSource As Long Dim hFilePrint As Long Dim strLine As String hFileSource = FreeFile Open filSource For Input Access Read Shared As hFileSource hFilePrint = FreeFile Open filPrint For Output Access Write As hFilePrint Do Until EOF(hFileSource) Line Input #hFileSource, strLine Print #hFilePrint, strLine Loop Close hFileSource Close hFilePrint End Sub To run the procedure, enter the following statement in the Immediate window:
PrintPoundSign "path\employees.txt", "PrintFile" Using Word or a text editor, locate the new file named PrintFile (which should be in Access's default directory). The contents are the same as the employees.txt file, as shown in Figure 20.5. Figure 20.5. The contents of the print file are similar to what you've seen in the Immediate window. CASE STUDY: Using I/O to Number Lines in a Text File Your users want to export client information to a text file for sharing, but they also want to add a line number to each line of data in the text file. Users can open the newly created text file and do it manually, but let's automate the process using I/O functions. First, open the Switchboard form and add a new command button. Name that button cmdExport and enter the Caption setting "Export Client Text File." Then, add the following Click event procedure to the form's module:
Private Sub cmdExport_Click() 'Export Clients table to 'delimited text file. On Error GoTo HandleErr DoCmd.TransferText acExportDelim, , _ "Clients", "Clients.txt" Call NumberClientList Exit Sub HandleErr: MsgBox "Error " & Err.Number & ": " & _ Err.Description End Sub This procedure is simple in task. It exports the entire Clients table to a file named Clients.txt, saving the file in the current directory. You might want to include the entire path in the file's name. To create the NumberClientList function that's called by the Click event procedure, open a standard module (or use Chapter 20's example module) and enter the following procedure:
Public Sub NumberClientList() 'Add line numbers to 'client list in text file. Dim hFileSource As Long Dim hFilePrint As Long Dim strInput As String Dim i As Integer On Error GoTo HandleErr hFileSource = FreeFile Open "Clients.txt" For Input Access Read As hFileSource hFilePrint = FreeFile Open "ClientsPrint.txt" For Output Access Write As hFilePrint Do Until EOF(hFileSource) i = i + 1 Line Input #hFileSource, strInput Print #hFilePrint, i, strInput Loop Exit Sub ExitHere: Close hFileSource Close hFilePrint Exit Sub HandleErr: MsgBox "Error " & Err.Number & ": " & _ Err.Description Resume ExitHere End Sub Save the module and close it. Open the modified Switchboard form shown in Figure 20.6 and click the new command button. After exporting the Clients table to a new text file, the procedure calls the NumberClientList function. This procedure opens the new Clients.txt file. The Do Until loop does the following: - Uses the Line Input function to cycle through each line in the file.
- Evaluates the expression i = i + 1 to create the current loop's line number.
- Executes the Line Input function to retrieve the data and then to write the line number (i) and the retrieved data to the new print file.
Figure 20.6. Click the Export Client Text File command button to create a text file. Figure 20.7 shows the new text file with each line numbered. In this case, it's just coincidence that the line numbers equal the client identification values. Figure 20.7. We used I/O functions to add a line number to each line.
|
|