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
Comparison Operators
All comparison operators produce true or false results. In other words, the comparison is either true or the comparison is false. The mathematical operators produce numeric values, whereas the comparison operators produce only true or false values. The rest of the program can use the true or false comparison operator result to make decisions. If a comparison operator returns False when comparing whether an employee worked the last pay period, the rest of the program knows not to print a paycheck for that employee.
Comparison operators are operators that compare data values against each other and produce true or false results.
Table 7.1 describes VB's six comparison operators. The comparison operators always compare data, and that comparison is either true or false because data either compares as expected or does not.
Table 7.1. The comparison operators determine how data compares.
| Operator | Use | Description |
| > |
lblSales. Caption > Goal |
The greater than operator returns True if the value on the left side of > is numerically or alphabetically greater than the value on the right. |
| < | Pay < 2000.00 | The less than operator returns True if the value on the left side of < is numerically or alphabetically less than the value on the right. |
| = | Age = Limit | The equal to operator (or equal operator) returns True if the values on both sides of = are equal to each other. |
| >= | FirstName >= "Mike" | The greater than or equal to operator returns True if the value on the left side of >= is numerically or alphabetically greater than or equal to the value on the right. |
| <= | Num <= lblAmt.Caption | The less than or equal to operator returns True if the value on the left side of <= is numerically or alphabetically less than or equal to the value on the right. |
| <> | txtAns.Text <> "Yes" | The not equal to operator returns True if the value on the left side of <> is numerically or alphabetically unequal to the value on the right. |
As you can see from Table 7.1, the comparison operators compare either variables, literals, control values, or combinations of all those data sources. The comparison operators work on both numeric and alphabetic values. You can compare any kind of number against another number, or any kind of string against another string.
The Comparison's Nature
When you compare strings, Visual Basic uses the ASCII table to determine how to compare the characters. For example, the ASCII table says that the uppercase letter A—with an ASCII numeric value of 65—is less than the uppercase letter B, which has an ASCII numeric value of 66. Notice that all uppercase letters are less than lowercase letters. Therefore, the abbreviation ST is less than St.
An ASCII table contains a list of characters with corresponding unique numeric representations.
To understand how comparison operators work, you must understand how to use their true or false results. The If statement, introduced in the next section, explains how you can use true and false results to make decisions in your program. Before you read the next section, make sure that you understand how these operators compare values. Make sure that you understand the Result column of Table 7.2 before you go any further.
Table 7.2. Relationship results.
| Relation | Result |
| 4 > 2 | True |
| 4 < 1 | False |
| 4 < 8 | True |
| "Apple" <= "Orange" | True |
| "Macmillan" < "Mc millan" | True |
| 0 >= 0 | True |
| 0 <= 0 | True |
| 1 <> 2 | True |
| 2 >= 3 | False |
Keep Each Side's Datatype Consistent
Take extra care that the expressions on both sides of a comparison operator conform to the same datatype or at least compatible datatypes. In other words, you cannot compare a string to a numeric datatype. If you try, you will receive a type mismatch error because the datatypes don't match. You can compare any numeric datatype against any other numeric datatype most of the time. In other words, you can test whether a single-precision value is less than or greater than an integer value.
The If Statement | Next Section

Account Sign In
View your cart