How it works
The code in the module creates a user-defined data type or record called
typQuote. This type has two strings -- quoteText and quoteAuthor -- both of which are
fixed length strings. These are needed where you're storing the data in a file which you
will randomly read and write. If a quote or author takes up less than the allotted space,
Visual Basic pads each string with spaces so that each record will be exactly the same
size as every other record.In the Declarations
section of the form a number of variables are declared, including two which aid in
navigation of the quotes.txt file -- currentQuote, which is the record number of the
current record in the file, and lastQuote, which is the number of quotes in the file.
The form's load event contains the code to centre the form on
the screen and to open the quotes file for random access. If the file does not exist, it
will be created and two sample quotes will be added to it. The first quote in the file
will be displayed in the text boxes on the screen by a call to the procedure readQuote,
which appears in the module. This procedure reads the quote number it receives from the
calling statement and displays it in the appropriate text box or label on the form that
called the procedure. Next month's form will also use this procedure, so this is why it
appears in the module and why the name of the calling form is passed to it.
The displayAlertMessage procedure also appears in the module,
rather than in the form code, as it will be called by event procedures on both this form
and the second form, which will be added next month. The procedure displayAlertMessage
takes a message from the calling statement and displays it on the screen.
The procedure toggleButtons handles the changes required to
the buttons on the form when the user selects Edit or New. When the Add and Exit buttons
become visible, the New and Quit buttons must become invisible, the Next and Back buttons
must become disabled and the two text boxes must be unlocked. The procedure toggleButtons
both sets and resets various button and text box properties in one simple procedure. Those
that were on are turned off and those that were off are turned on.
When the user selects New or Edit to add or change a quote
the text boxes are unlocked so that changes can be made to them and, if the user chooses
to add a new quote, the contents of the text boxes are cleared. The editFlag keeps track
of whether the user chooses to add a new quote or alter one allowing the code in the
cmdAdd_click event to be used in either instance. The text which displays in the label
lblQuoteNumber is used at this point to give the user brief instructions.
When the user has completed their changes and they select the
Add button, the text is checked to ensure that a quote has been typed (ie, the text is one
character or more in length), that it is not longer than 600 characters and that the
author's name is not more than 50 characters. If the text entered meets all these tests
and it is a new quote, then the number of quotes is incremented and the currentQuote
variable is made equal to the new total number of quotes. The quote is then written to the
quotes.txt file.
If there is something wrong with the text entered, a message
indicating the problem and how the user can go about fixing it is displayed.
The code for the navigation buttons Back and Next simply
steps one quote at a time through the file, making sure that the user is not trying to
proceed past the beginning or end of the file and alerting them to the problem if they try
to do it.
Next month
In next month's column the form and code for displaying a randomly
selected quote on the screen whenever the user starts their computer will be added and
linked to this part of the project. |