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
String Functions
Unlike the numeric functions, Visual Basic's string functions return a string and often work with one or more string arguments. Table 14.2 lists several of the more common string functions you'll work with. The string functions accept controls as well as variables, literals, and expressions.
Table 14.2. Common built-in string functions.
| Function | Description |
| Chr(int) | Returns the ASCII character of the argument. |
| InStr([start,] str1, str2) | Returns the starting position of str2 within str1, beginning at start if specified. |
| InstrRev(str1, str2 [,start]) | Returns the starting position of str2 within str1 from the end of the string, beginning with start if specified. |
| Join(list [,delimeter]) | Merges strings in the list, each separated by one space unless you specify a delimeter character. |
| LCase(str) | Returns the argument in all lowercase letters. |
| Left(str, int) | Returns the far-left int characters from the argument. |
| Len(str) | Returns the number of characters in the string. (Notice that Len() works on numeric arguments as well.) |
| LTrim(str) | Returns the string argument, without leading spaces. |
| Mid(str, intStart [, intLen]) | Returns a substring of the argument, starting with the character at intStart and continuing until the entire the string is extracted or until the optional intLen characters have been extracted. Mid() is called the midstring function because it can return the middle portion of a string. |
| MonthName(month [,abbre]) | With a month number, the month name is returned, abbreviated to 3 characters if you specify True for abbrev. |
| Right(str, int) | Returns the far-right int characters from the argument. |
| RTrim(str) | Returns the string argument, without trailing spaces. |
| Str() | Converts its numeric argument to a string with the numeric digits in the string. |
| StrReverse(string) | Returns the string argument that is completely reversed. |
| UCase(str) | Returns the argument in all uppercase letters. |
| WeekdayName(weekday [, abbrev]) | Returns the day of the week as a string given the numeric weekday argument, and abbreviated if abbrev is True |
A substring is part of a string.
Suppose you want to determine whether a string variable's value will fit inside a text box before you attempt to assign the string to the Text Box control (assume that the text box doesn't have AutoSize set to True). If the text box is large enough to hold 20 characters, the following If statement fragment will be True if the string fits in the text box:
If (Len(strVar) <= 20) Then 'String fits
Suppose you need to compare two password string values. Given that the user may have entered the password in all uppercase or a case mixture, the following code tests the stored password against one entered in a string variable, and the code uses UCase() to ensure that they compare with the same case matches:
If UCase(strUser) = UCase(strPassword) Then ' This If leg is true if the passwords match
The LTrim() function is often useful for trimming the leading blank from strings you make from numbers. For example, Str(123) returns the string literal " 123" (notice the leading blank). Sometimes, when writing certain kinds of files, you need to write strings of data instead of numbers and Str() comes in handy. If, however, you need to strip off the leading blank, you can embed Str() within LTrim() to return the string digits without the leading blank, like this: LTrim(Str(123)).
Left() returns the left part of a string or control value that Visual Basic converts to a string. Therefore, the following stores only the first five characters from the string argument into strAns:
strAns = Left(txtUser.Text, 5)
Whereas Left() returns the left part, Right() returns the right part of a string. Mid() can return the middle part of a string. Therefore, the following expression becomes "der" when Mid() returns the middle three letters: Mid("Federal", 3, 3). Because of the optional third argument, Mid() works like the Right() function if you omit the third argument because Mid() returns all characters from the starting position to the end of the string if you don't put the third argument inside Mid()'s argument list.
Date and Time Functions | Next Section

Account Sign In
View your cart