## Visual Basic in 12 Easy Lessons velp04.htm

- Stop & Type
  - The Program's Description
  - The Program's Action
  - Checking the Discount Code
  - Computing the Inventory
  - Close the Application

#
Project 4

##
Data Basics

###
Stop & Type

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:

- How to distinguish the data types
- Why you must define and name variables
- How to use Visual Basic's mathematical operators
- When the operator precedence table becomes crucial
- What to do when you want to override the operator precedence
- How to code If-Else and Select Case statements to make decisions
- Why the various Case options exist

####
The Program's Description

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 user—and programmer—frustration.

####
The Program's Action

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.

####
Checking the Discount Code

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

1. The discount text box's Name property contains txtDisc. The name of the LostFocus event procedure is txtDisc_LostFocus().
2. Use Val() to convert the text box value to a number while it tests to make sure that the user entered a number from 0 to 4.
3. The body of the If executes only if the user enters a bad discount code. The Beep statement beeps the computer's speaker to get the user's attention.
4. This line erases whatever value the user entered in the text box.
5. This line returns the focus to the text box so that the user is forced to enter a good value before doing anything else.
6. This line ends the body of the If statement.
7. This line terminates the event procedure.

> > 3: Audibly warns the user of an error.

####
Computing the Inventory

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

1. The command button's Name property contains cmdInven, so the name of the Click event procedure is cmdInven_Click().
2. A single-precision variable named Discount is defined to hold an intermediate calculation.
3. A currency variable named ExtAmount is defined to hold an intermediate calculation.
4. The first part of the extended inventory amount is computed by multiplying the user's number of units sold by the price per unit.
5. Select Case makes a decision based on one of four values stored in the txtDisc text box.
6. If the discount code is 0, zero is used for the discount percentage.
7. If the discount code is 1, 5% is used for the discount percentage.
8. If the discount code is 2, 10% is used for the discount percentage.
9. If the discount code is 3, 15% is used for the discount percentage.
10. If the discount code is 4, 20% is used for the discount percentage.
11. The body of the Select Case comes to an end.
12. This line finishes computing the discount amount by applying the discount to the extended price, and it displays the final amount on the form.

> > 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.

####
Close the Application

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.