Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Finishing Touches

Run your application and enter some sample values. Figure 9.4 shows the application with some sample data entered for a five-year investment. As shown in Figure 9.4, if you invested $1,000 today at 11% interest, in five years you will have approximately $1,685.

09fig04.gif

Figure 9.4 The running application computes the final investment amount.

The application is not really complete and ready for distribution. Although you've mastered the mechanics of this simple application, more is needed to make the application professional. Obviously, the ending investment's decimal place precision is far too high.

You need to format the value shown in the Ending Investment text box. When you format a value, you don't change the value, but you change the value's look. Visual Basic includes a built-in function called Format() that formats numeric and string values so you can display such values as dollars and cents, area code formats, or whatever formats you want to use. Although Hour 14, "Built-In Functions Save Time," explains Format() in detail, you can use the Format() function now to spruce up your application.

To format is to change the way a value appears to the user.

At the end of the cmdCompute_Click() event procedure, change the ending investment's assignment to this:

txtEnding.Text = Format(curInitInv * sngInterest, "$###,##0.00")

The Format() function's basic format is this:

Format(expression, strFormat)

Visual Basic changes the look of expression to match that of the format string you supply. Therefore, the format string "$###,##0.00" instructs Visual Basic to display the value with a dollar sign, floating numbers with the # (if the number is less than $100,000.00 the numbers will move left to touch the dollar sign instead of leaving a gap for the missing digits), commas in the amount if the amount is over $1,000, and a decimal point with two decimal places showing. If the value happens to be $0.00, the zeros ensure that the value prints, whereas if you used a # in place of each 0, the # would show nothing if the result were zero.

After you format the value and rerun the application with the numbers used earlier, you'll see a result that looks better, as Figure 9.5 shows.

09fig05.gif

Figure 9.5 The Format() function improved the output.

Share ThisShare This

Informit Network