|
|
|
|
When you want to communicate with the user, you have to use a natural language such as English. VB refers to text as strings. There are string variables, string functions, and just plain strings. String variables When you collect verbal information from the user, such as the user's name or address, that information is stored as a string variable. Typically that information is gathered using a text box: Dim USERNAME as String USERNAME = Text1.Text String functions Suppose you want to use a shorter version of the user's name, keeping only the leftmost three letters. Then you can create a new variable: Dim NICKNAME as String NICKNAME = Left(USERNAME,3) As you might imagine, there's a similar function for Right, and a slightly trickier one for the middle of a string called Mid. Often it is useful to know how long a name is. There's a function for that: NAMELENGTH = Len(USERNAME) The Len function is handy for checking if the user has filled in a required text box. If the length of the string is zero, or if it is less than it should be, you can send a friendly message box requesting the user to take corrective action. Plain Strings The .Caption of a control or form is a string; so is the .Text of a text box. They can be combined with other strings. The combining character is the ampersand (&). The plus sign works too, but is confusing because it suggests addition. I recommend you use the & to join strings. Label1.Caption = "Hi there, " & USERNAME If you have a long caption for a label and want it to print neatly, you can use spaces and carriage returns (Chr$13) to achieve that. Label1.Caption = "Welcome to my program," Label1.Caption = Label1.Caption & Chr$(13) Conversion You will sometimes need to convert from strings to numbers and vice versa. The conversion functions are Val and Str. These conversions are often the solution to VB's annoying "Type Mismatch" errors. Dim USERAGE as Integer USERAGE = Val(Text1.Text) Label2.Caption = "Are you really " &
|
|
|