TASK 4: Write from the Database to the Text Source File
Finally we are ready to have the database write our source file. Our source (.src) file is an ASCII text file that will be used by MiniHelp Plus to compile the WinHelp .rtf topics file and, ultimately the WinHelp (.hlp) file itself.The example Access Basic code for this is in Function rptPbookWhelp().
rptPbookWhelp()
There are several items in this procedure that should be explained:
- Const QU = """"
In order for Access to print a single quotation character (") to a file (or to an SQL statement), it requires a series of four quotation marks in the Access Basic code. To accommodate this, I assign the text string of four quotation marks to a constant, QU, and then use the constant QU as needed.
- Filename = gconPBOOKHLP_A_PATH & "depersn1.src"
I use this statement to define the filename for the text file I'm about to write. The gconPBOOKHLP_A_PATH is a global constant (defined in a separate module) to define the path where MiniHelp Plus resides on the hard drive, and thus, where I want the .src file to be written. Therefore, the complete filename consists of the path, represented by the global constant, and the concatenated filename.
- FN = FreeFile
This is a statement that assigns to my variable "FN" a sort of random-number that's generated by the Access system function FreeFile(). FreeFile (the parentheses are optional if the function is called from an Access module) returns the next valid unused file number. According to the Access Basic help, "Use FreeFile when you need to supply a file number, and you want to ensure that the file number is not already in use."
- Print #FN,
For every line that you want to write to the text file, you must issue the above command. There is a space between the "Print" and the "#FN,", and there must be a space between the "Print #FN," and whatever follows. When nothing follows the "Print #FN," command, only an empty line is printed to the file, with the line-feed and carriage-return characters. That which follows the "Print #FN," command must be on one unbroken line (i.e., it cannot be continued to the next line). It can consist of a 'string literal', enclosed by quotation marks, or anything representing a string, such as variable or constant that holds a string value. Whenever you need to combine string literals and variables or constants, use a semi-colon (;) character. Look at the example rptPbookWhelp() function, and you'll see the constant QU combined with quotation mark-enclosed (") string literals, made possible by the use of the concatenating semi-colon (;) character. This is especially true where the startup macro statements for the .hpj file are written.
Two Source Files are Made in the Example
Recall that my example WinHelp Daniel phone book is derived from two source files, depersn1.src and depersn2.src. So, halfway through the function rptPbookWhelp(), look for the statement "Close FN". This is where I've finished writing the first source file, depersn1.src, and I'm ready to begin writing the second source file, depersn2.src.After I close depersn1.src, I redefine the variable Filename with the new filename, depersn2.src, let the Access function FreeFile give me a new file number ("FN = FreeFile"), return to the top of the open recordset ("PbookSet.MoveFirst"), and issue the command "Open Filename For Output As FN". Afterwards, I can begin writing lines to the newly opened source file.