Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 4: Data, Operations, and Built-In Functions
Last change: February 13 2016 18:47:36.
<<PreviousNext>>

Computing Net Sale: An Application Example

You have learned a lot about numeric data in this chapter. These are in addition to those VB controls you learned in Chapter 3, “Some Visual Basic Controls and Events.” Have you been wondering how all these can be put together into an application? The purpose of this example is to show you how VB controls and numeric data can be used together for a simple but practical application. Please work along as you read.

The Net Sale Project

A retailer’s gross receipts consist of cash and credit cards. Both cash and credit card receipts are deposited daily in the bank. Before making the deposit, the owner needs to figure out her net receipts (which will be her actual amount of deposit). Cash is deposited on a one-for-one basis, meaning there is no discount or fee; however, she has to pay the bank a processing fee of 2.5% for all credit card receipts. You are to develop a project to compute the net receipts that she can use to prepare her deposit slips.

Input, Process, and Output

One way to analyze the project is to examine the input, process, and output of the project. It is usually easier to consider the output first. What the retailer needs to know is the net receipts that include both cash and net credit card receipts. It would also be nice to show the amount of credit card processing fees. The input should include the amount of cash and gross credit card receipts.
The process (computation) should be fairly simple. There will be no change in cash receipts between the gross and net amount. The credit card processing fee can be computed from the gross credit card receipts. The formula should be as follows:

ProcessingFees = GrossCreditCardReceipts x .025

This analysis can be shown as in the following table.

Input Processing Output
Cash receipts None Cash receipts
Gross credit card receipts (GCCR) GCCR × .025 Credit card processing fees (CCPF)
  GCCR – CCPF Net credit card receipts

In addition, it would be nice to show total gross and net receipts. The retailer can use the total gross amount to verify the entered data. The total net receipt amount can be used for the preparation of deposit slips.

The Visual Interface

What VB controls should you use to obtain input from the user? Both cash receipts and credit card receipts are numeric. The exact amounts vary from day to day. It would appear that text boxes are the most suitable VB controls to use. Output can also be displayed in text boxes; however, you do not want the user to accidentally change the results. One possible solution is to use labels and set their properties to make them appear like text boxes. You can make the labels look similar to text boxes by properly setting their BackColor and BorderStyle properties. Alternatively, you can use texboxes and set their ReadOnly properties to True. Labels use less computer resources. However, they do not allow the user to copy and paste their text contents, while texboxes do. Assume a need for the copy and paste operation. Text boxes will be used here. You can also set text boxes to display amounts right-justified so that they will feel and appear right. For code clarity, you can use a named constant for the processing fee rate. You will also use variables to store computational data before the values are displayed in the output labels.

When should the computation be carried out and displayed? For simplicity, the computation will be done when the user clicks on a button with a text, Compute. It would also be nice to provide the user a button to quit. A sketch of the interface design is presented in Figure 4-3.

Figure 4-3
A sketch of input, process, and output of the receipts project
Computing net sale

Of course, when you translate this sketch into a VB form layout, you must also add proper labels. The resulting visual interface appears as seen in Figure 4-4. The settings of selected properties of the VB objects used are summarized in the following table. Notice the property settings for the text boxes which are used to display the results of computations. To prevent these fields from accidental changes and preserve the accuracy of the results, these text boxes’ ReadOnly properties are set to True. Once this property is set to True, the control’s BackColor is automatically set to Control by the IDE. To make the appearance of these controls consistent with other text boxes, you can set their BackColor properties back to White through the Properties window.

Named Object Description Property Setting
txtCash For cash input TextAlign Right
txtCreditCards For credit card input TextAlign Right
txtGrossReceipts To display gross receipts ReadOnly True
BackColor White
TextAlign Right
txtProcessingFees To display processing fees ReadOnly True
BackColor White
TextAlign Right
txtNetCash To display cash receipts ReadOnly True
BackColor White
TextAlign Right
txtNetCreditCards To display net credit cards ReadOnly True
BackColor White
TextAlign Right
txtNetReceipts To display net receipts ReadOnly True
BackColor White
TextAlign Right
btnCompute To perform computations Text Compute
btnQuit To end the program Text Quit

Figure 4-4
Visual interface for the net sale project

Declaring the Variables and Constant

Before writing any code, try to identify the variables to use and their data types. In addition, you should also consider whether to name any constant to be used in the project. From your analysis, you know that:

Tip
If you need to refer to a property several times in a procedure, assign it to a variable first; then use the variable from then on. The code executes faster with a variable than with a property of an object. Of course, if the property setting changes, the variable will need to be reassigned with the new setting.
The next question is: What data type should each variable be? In the previous discussion, it was suggested that the same data type be used for all the variables in a statement to avoid the problem with conversion. Because all these variables will be used to handle amounts, they will involve decimal points. This excludes the Integer and Long types from consideration. The floating point (Single and Double) and Decimal types are the potential candidates. Recall that the Single type can handle only about six or seven significant digits. If the retailer has a gross sale in excess of $100,000.00, the Single type will not have enough precision (field width) to handle the amount. The choice should be between Double and Decimal. The Decimal type has the advantage of being accurate, but is slower in speed than the Double type. Assume that the processing speed is the most important criterion for this project. Your choice will then be the Double type for all variables. With all these considerations, you will now place the following code in the btnCompute Click event procedure:

Dim Cash As Double Dim CreditCards As Double Dim ProcessingFees As Double Dim NetCreditCards As Double

Because a variable should be declared before it is used, it makes sense to place all the preceding lines at the beginning of the procedure. Doing so also has another advantage. When you need to check the declaration for a particular variable, it is much easier to locate at the predetermined location.

How do you handle the credit card processing rate? Using a named constant for the rate appears to have some advantages. The name itself can give a clear purpose for the computation. More importantly, if the rate literal is used in several places in the project, when there is a change in the rate, you may have to search over the entire project to make the corrections. On the other hand, if you use a named constant, you define it at only one place and, therefore, revise the rate only once at the same place. With this consideration, you will place the following line in the general declaration area in the code window:

Const CreditCardRate As Double = 0.025

Again, the constant is declared as the Double type to be consistent with all the variables used to avoid data type conversion. Placing the declaration in the general declaration area will make the constant recognizable in all procedures.

Computations

Before considering any code, please note that you will not deal with strict type conversion at this point. After you have the variables declared, you can assign the content of the text boxes to them:

‘Store user entered data in variables Cash = txtCash.Text CreditCards = txtCreditCards.Text

These variables can then be used in the computations. The credit card processing fees are computed by multiplying the (gross) credit card receipts by the processing fee rate:

‘ Perform fees computations ProcessingFees = CreditCards * CreditCardRate

The net credit card receipt is the difference between the gross credit card receipts and the processing fees.

‘ Compute net credit card deposit NetCreditCards = CreditCards – ProcessingFees

Displaying the Results

You are now ready to display the results. Net cash will be the same as its original amount. Both the computational results will be displayed in their corresponding textboxes.

‘ Display results txtNetCash.Text = Cash txtProcessingFees.Text = ProcessingFees txtNetCreditCards.Text = NetCreditCards

The totals can be displayed by adding cash and gross/net credit card receipts.

‘ Display totals txtNetReceipts.Text = Cash + NetCreditCards txtGrossReceipts.Text = Cash + CreditCards

You also need to provide code to end the program execution. This is done in the btnQuit Click event procedure. As explained in Chapter 2, “Introduction to Visual Basic Programming,” a good way to handle this is to close the form:

Me.Close

The Complete Code

Putting everything together, the complete code appears as follows:

‘Place the following constant declaration in the general declaration area Const CreditCardRate As Double = 0.025 Private Sub btnCompute_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim Cash As Double Dim CreditCards As Double Dim ProcessingFees As Double Dim NetCreditCards As Double ‘Store user entered data in variables Cash = txtCash.Text CreditCards = txtCreditCards.Text ‘ Perform fees computations ProcessingFees = CreditCards * CreditCardRate ‘ Find net credit card deposit NetCreditCards = CreditCards - ProcessingFees ‘ Display results txtNetCash.Text = Cash txtProcessingFees.Text = ProcessingFees txtNetCreditCards.Text = NetCreditCards ‘Display totals txtNetReceipts.Text = Cash + NetCreditCards txtGrossReceipts.Text = Cash + CreditCards End Sub Private Sub btnQuit_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click Me.Close() End Sub

An Issue of Presentation

Have you been working on our example? If you have (and you should), it is time to test run the program to see if it gives the correct and desired results. The program should work. You might, however, discover one minor problem. When the credit card receipts have a fractional number, the resulting amounts on the form do not appear to be neatly presented; some may have no decimal points, and others may have a decimal point with several digits to the right. Is there a way to force the computer to display two decimal places for all amounts so that they appear neatly aligned by the decimal point? The answer lies in the Format function.

Last change: February 13 2016 18:47:38.
<<PreviousNext>>