Sams Teach Yourself Visual Basic 6 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- Introduction
- Who Should Read This Book
- What This Book Will Do for You
- Can This Book Really Teach Visual Basic in 24 Hours?
- What You Need
- Files on the Visual Basic Distribution CD-ROM
- Conventions Used in This Book
- Enough! Time Is Ticking!
- Part I: Introducing Visual Basic
- Hour 1. Visual Basic at Work
- Hour 2.Analyzing Visual Basic Programs
- Hour 3.Controls and Properties
- Hour 4.Examining Labels, Buttons, and Text Boxes
- Part II: Coding the Details
- Hour 5.Putting Code into Visual Basic
- Hour 6.Message and Input Boxes
- Hour 7.Making Decisions
- Hour 8.Visual Basic Looping
- Part III:Putting Code to Work
- Hour 9.Combining Code and Controls
- Hour 10.List Boxes and Data Lists
- Hour 11.Additional Controls
- Hour 12.Dialog Box Basics
- Part IV:Programming with Data
- Hour 13.Modular Programming
- Hour 14.Built-In Functions Save Time
- Hour 15.Visual Basic Database Basics
- Hour 16.Printing with Visual Basic
- Part V:Sprucing Up Programs
- Hour 17.Menus and Visual Basic
- Hour 18.The Graphic Image Controls
- Hour 19.Toolbars and More Graphics
- Hour 20.Writing Correct Applications
- Part VI:Advancing Visual Basic Applications
- Hour 21.Visual Basic and ActiveX
- Hour 22.Object Basics
- Hour 23.Distributing Your Applications
- Hour 24.Online Visual Basic
- Part VII:Appendixes
- Appendix A.Operator Precedence
- Appendix B.Answers
- Appendix C.Using the CD-ROM
Format Function
Visual Basic cannot read your mind, so it doesn't know how you want numbers displayed in your applications. Although Visual Basic sometimes displays none, one, or two decimal places for currency values, you'll almost always want those currency values displayed to two decimal places with a dollar sign and commas when appropriate.
As with the date and time functions, if you've set your computer's international settings to a country other than the United States, your formatted currency values may differ from those shown here. (This book uses U.S. settings.) Some countries use commas to indicate decimal places, whereas the United States uses the decimal point.
Format() returns a Variant (convertible to a String) datatype formatted to look the way you want. Format() doesn't change a value, but Format() changes the way a value looks. Here is the format of Format():
Format(Expression, strFormat)
Often, you'll assign the result of Format() to other variables and controls. Generally, you'll perform all needed calculations on numeric values before formatting those values. After you've performed the final calculations, you'll then format the values to String (or Variant) datatypes and display the resulting answers as needed.
Expression can be a variable, an expression, or a constant. strFormat must be a value from Table 14.8. Visual Basic contains many format strings in addition to the ones shown in Table 14.8. You can even develop your own programmer-defined format strings, although this book doesn't go into those.
Table 14.8. The strFormat values.
| strformat | Description |
| "Currency" | Ensures that a dollar sign ($) appears before the formatted value, followed by a thousands separator (a decimal point or comma for values over 999; your country setting determines whether the thousands separator is a comma or a decimal). Two decimal places will always show. Visual Basic displays negative values in parentheses. |
| "Fixed" | Displays at least one digit before and two digits following the decimal point, with no thousands separator. |
| "General Number" | Displays the number with no thousands separator. |
| "Medium Time" | Displays the time in 12-hour format and the a.m. or p.m. indicator. |
| "On/Off" | Displays On if the value contains a nonzero or True value and displays Off if the value contains zero or a False value. |
| "Percent" | Displays the number, multiplied by 100, and adds the percent sign to the right of the number. |
| "Scientific" | Displays numbers in scientific notation. |
| "Short Time" | Displays the time in 24-hour format. |
| "True/False" | Displays True if the value contains a nonzero or True value, and displays False if the value contains zero or a False value. |
| "Yes/No" | Displays Yes if the value contains a nonzero or True value and displays No if the value contains zero or a False value. |
Listing 14.2 contains a series of formatting function calls that convert numeric and logical values to formatted Variant datatypes that you can display.
Example 14.2. Formatting numeric and logical values.
1: Dim FormValue (8) As String 2: ' Change 12345.678 to $12,345.68 3: FormValue(1) = Format(12345.678, "Currency") 4: 5: ' Change 12345678 to 12345.68 6: FormValue(2) = Format(12345.678, "Fixed") 7: 8: ' Change .52 to 52.00% 9: FormValue(3) = Format(.52, "Percent") 10: 11: ' Change 1 to Yes 12: FormValue(4) = Format(1, "Yes/No") 13: 14: ' Change 0 to No 15: FormValue(5) = Format(0, "Yes/No") 16: 17: ' Change 1 to True 18: FormValue(6) = Format(1, "True/False") 19: 20: ' Change 0 to False 21: FormValue(7)= Format(0, "True/False")
An edit mask is a format string, such as "#,###.##", that specifies how you want numeric and string data to appear. For additional information on using special numeric formats, search the online documentation for User-Defined Numeric Formats (Format Function).
Summary | Next Section

Account Sign In
View your cart