Programming in
Visual Basic
String Functions :
A string is a set of
characters treated as a single object. A
string can inlude letters, numbers and
special characters such as
-,£,&,*,etc. The following are
typical types of string:
A name
|
Freda Bloggs
|
A telephone number
|
0121-743-4471
|
A date
|
16-1-99
|
A time
|
22:05
|
A number
|
345
|
Anything you like
|
45$£%&ER(787***)
|
Everything
entered in a text box is treated as a
string and, as we saw in an earlier unit,
numeric strings needed to be changed to
values for use within calculations. The
val function we use to do this is a
typical example of a string function.
Some of the other typical string
functions are outlined below.
LCase or UCase
:
The LCase statement will
turn an entire string into lower case,
UCase turns a string into upper case. For
example:
txtItem
|
LCase(txtItem)
|
UCase(txtItem)
|
Radio
|
radio
|
RADIO
|
Disk 3.5
|
disk 3.5
|
DISK 3.5
|
By
checking everything as upper case or
lower case, we do not need to worry how
the end user enters the information. For
example, the first section of the Find
Item code within the Stock project would
change as follows:
Sub cmdFindItem_Click()
If UCase(txtItem) =
"RADIO" Then
txtPrice = 12.54
End If
End Sub
Left or Right :
The Left and Right
functions are used to look at a set
number of characters at the beginning or
end of a string. The format is
Left(String, Number of characters). For
example:
txtItem
|
Left(txtItem, 5)
|
Right(txtItem, 3)
|
Radio Set
|
Radio
|
Set
|
Visual Basic
|
Visua
|
sic
|
Mid
:
The Mid function return a
certain number of characters from inside
a string. The format is Mid(String,
Starting Point, Number of Characters).
For example:
txtItem
|
Mid(txtItem, 4, 5)
|
Mid(txtItem, 8, 1)
|
Radio Set
|
io Se
|
e
|
Visual Basic
|
ual B
|
B
|
InStr
:
The Instr function tests
to see whether on string occurs within
another string. If the string occurs, the
position of the start of the string is
returned, otherwise 0 is returned. A
start point for the search can be
included, but is not compulsory. The
format is InStr(Start Point, Large
string, Small String). For example:
txtItem
|
Instr(1, txtItem,
"Brown")
|
Instr(1, txtItem,
"Smith")
|
Paul Brown
|
6
|
0
|
The Instr
function is particulary useful for
complex fields when the user may not give
exactly what we expect. For example,
suppose the user enters a full namewithin
the Address project, we could use the
following code as part of the Find Name
code:
If Instr(txtName, "Brown") >
0 Then
txtAddress1 =
.........
...
...
...
End If
Mixing String
Functions :
Quite often we mix string
functions to ensure thorough checking.
For example, we might need to force upper
case for checking purposes and only look
at a certain number of characters. Here
are some examples:
If Right(UCase(txtItem), 5) =
"RADIO" Then ...
If InStr(1, UCase(txtName),
"BROWN") > 0 Then ...
Format :
The format function is
used to change the format of string,
particularly those containing values. The
general format is Format(String, format
string). The format string is a set of
characters that define how the string
will be displayed. Typical format
characters are
0
|
Display a digit ar a zero
|
#
|
Display a digit or nothing
(if zero)
|
%
|
Multiply by 100 and add the %
character
|
£
|
Insert a £ character
|
-() space
|
Display -,(,) or a space
|
For
example:
String
|
Format String
|
Result
|
23.4567
|
"0000.00"
|
0023.46
|
23.4567
|
"####.##"
|
23.46
|
23.4567
|
"£0000.00"
|
£0023.46
|
23.4567
|
"£####.##"
|
£23.46
|
01217434471
|
(0000) 000 0000
|
(0121) 743 4471
|
Exercise
Ten :
For all of the following,
test that your code works before
continuing.
1. Open the Stock project
2. Edit the coding of the Find Item
command button and use the Ucase or LCase
function to ensure correct matching.
3. Add the left function to only check
the amount of characters that should have
been entered i.e. for "Radio",
check Left(txtItem, 5).
4. Add an extra button that will allow
the user to search for part of a string
(e.g. "Set" within "Dinner
Set"). Copy the code from th Find
Item button and paste into the code for
the new button. Amend the code to use the
InStr function.
5. Edit the code for the Calculate button
to make use of the Format function to
display the Sub Total, VAT and Total
Price as currency.
6. Open the Address project.
7. Amend the code for the Find Name
button to make use of the InStr function
to find either the first name or last
name.
8. Use the format
function to display the telephone number
as follows:
0121-7434471
9. Amend the code for the Find Telephone
button to allow for the fact that the
user may enter the number without the
area code. (InStr or Right could be
used).
10. The Len function ( Len(string) )
returns the number of characters in a
string. Assume that a telephone number
cannot have more than 12 characters. Add
a test to the Find Telephone code to
check that the user's number is not too
big.
11. Find out about the Now function and
Date formats from th help menu. Add a
text box to all your forms that displays
today's date in a suitable format.
Page [<<
PREV] 1 2
3 4
5
6
Back to Tutorials
- Main
|