Microsoft Access VBA Macro Programming

(Tina Sui) #1

This will give the answer 0 because the string "shepherd" is not found due to the difference in
case. The following will give the answer 9:


MsgBox InStr(1, "Richard Shepherd", "shepherd", vbTextCompare)


The next example uses the two optional parameters. Notice the use of the start position:

MsgBox InStr(10, "Richard Shepherd", "shepherd", vbTextCompare)


This will give the result 0 (string not found) because the start search position is after where
the search string is found.
TheInstrRevfunction can be used to do a reverse search through a string. This means
that it begins the search from the end of the string instead of the beginning.


Functions


This section is intended to give an overview of the most commonly used functions in VBA.
Many others are available, but you will find that these are the major ones used.


Len


Lenreturns the number of characters in a string. The following will return the value of 3:


MsgBox Len("abc")


This example will return the value 8:

Msgbox Len("shepherd")


This function is useful in conjunction with the other string-handling functions. For
example, if you want the last four characters of a string that is variable in length, you would
need to know the string’s length.


Abs


Absstands forabsolute valueand returns a value of the unsigned magnitude. The following
examples both will give the value of 1:


MsgBox Abs( 1 )
MsgBox Abs(-1)


Int


Intis short forintegerand truncates a number to an integer. It does not round to the nearest
whole number. This will give the value of 1:


MsgBox Int(1.2)


Chapter 5: Strings, Functions, and Message Boxes 49

Free download pdf