This lesson taught you how Visual Basic stores data values in several different formats. The different data types enable
you to categorize data. For example, when you are working with currency amounts, use the currency data type so that Visual Basic
ensures accuracy to two decimal places.
The key to manipulating data in Visual Basic programs is the variable. A
variable is a named storage location in memory that you define by using the Dim statement. The Dim statement requests that Visual Basic set aside the memory, attach one data type
to the variable, and name the variable. Once you define variables, the
assignment statement stores values in the variables. Until you store values in variables, the numeric variables hold zeros and the string variables hold null strings.
Once the variables receive values, the If and Select Case statements determine
the appropriate program paths to take. The If and Select Case analyze the data in variables and make runtime decisions based on the values there.
In this lesson, you saw the following:
Figure P4.1 shows how the PROJECT4.MAK application looks as soon as you load and run the program. The project's form contains several controls. Notice the following about the
controls on the form:
The user must enter values in three text boxes that correspond to the number of inventory items sold, the price per item, and a special discount code. The user must type either a 1, 2, 3, or 4 to indicate which
discount percentage is desired for the
total price.
The Calculate Inventory command button computes the value of the inventory, given the user's entries in the text boxes, and displays the amount of the inventory in the
lower label marked Extended Amount.
An Exit command button terminates the program.
Figure P4.1. The project's opening screen.
The program acts like a smart
but simple cash register that extends the total inventory sold price and adds a discount based on a discount code. There are several ways to support such a discount list in Visual Basic, and this program demonstrates only
one method. In future lessons,
you will learn how to add option button controls and scrolling list boxes to support such discount percentage codes.
Note: Assume that the company supports only the four discount codes described. If the user leaves the discount code blank, the program does not compute a discount code. If the user had to type a discount code, it would be too easy for the user for forget the decimal point if you required the user to enter a decimal discount like .15. The user might also enter the percent sign, which could cause program confusion as well. Therefore, for percentage values, the table of discount codes works very well and eliminates much userand programmerfrustration.
Enter the following values in the text box controls:
Units Sold: 20
Price Per Unit: 2.75
Discount Code: 3
Press the Calculate Inventory command button (Alt+C). Visual Basic
calculates the full inventory price and displays the answer 46.75 to the right of the Extended Amount label.
Even though this project works with currency data types, the displayed answer does not always appear with two decimal places. For
example, entering a Price Per Unit amount of 2.00 produces the Extended Amount of 34 with no decimal places showing. Visual
Basic never makes assumptions about how you want numeric data displayed. Even with currency values, Visual Basic will not display
two decimal places unless you specifically request two decimal places. Lesson 7 shows you how to format numeric output to look
exactly as you want it. Until then, be lenient with your own programs and the programs in this book, because the decimal places
will not always work out the way you would prefer. It is more important, until Lesson 7, to concentrate on how the results are
produced, not how the results are formatted to look.
One of the most powerful features of the PROJECT4.MAK application is its use of the If statement in the discount code's text box LostFocus event procedure. The
LostFocus event occurs when the user moves the focus from the discount code text box to
another control. Therefore, the txtDisc_LostFocus() event procedure, shown in Listing P4.1, executes immediately after the user enters a value for the discount code.
Note: Line 5 uses a Visual Basic element called a method, which you have not seen yet. A method works almost like a built-in function, such as Val(). However, instead of converting data, a method performs an action for a particular control. You must request a method by specifying a control, followed by a period and the name of the method.
Listing P4.1. Ensure that the user enters a proper discount code.
1: Sub txtDisc_LostFocus () 2: If (Val(txtDisc.Text) < 0) Or (Val(txtDisc.Text) > 4) Then 3: Beep 4: txtDisc.Text = "" 5: txtDisc.SetFocus 6: End If 7: End Sub
3: Audibly warns the user of an error.
When the user clicks the Calculate Inventory command button, the command
button's Click event procedure, shown in Listing P4.2, executes. The event procedure uses a combination of variables and a Select Case statement to compute the proper inventory
amount based on the user's text box values at the discount specified.
Listing P4.2. Computing the inventory amount.
1: Sub cmdInven_Click () 2: Dim Discount As Single 3: Dim ExtAmount As Currency 4: ExtAmount = Val(txtUnits.Text) * Val(txtPrice.Text) 5: Select Case Val(txtDisc.Text) 6: Case 0: Discount = 0 7: Case 1: Discount = .05 8: Case 2: Discount = .1 9: Case 3: Discount = .15 10: Case 4: Discount = .2 11: End Select 12: lblExt.Caption = ExtAmount - (Discount * ExtAmount) 13: End Sub
4: Always convert control values to numbers by using Val() before you calculate with the values.
11: No Case Else is required because the txtDisc LostFocus() procedure ensures that only valid values appear in txtDisc.
This project helped solidify your understanding of how and when to use variables for data. You can now exit the application and exit Visual Basic. The next lesson adds additional programming skills to your repertoire and increases the power of the programs that you can write and control.