MS Access: Instr Function
In Microsoft Access, the Instr function returns the position of the first occurrence of a string in another string.
Syntax
The syntax for the Instr function is:
Instr ( [start], string_being_searched, string2, [compare] )
start is optional. It is the starting position for the search. If this parameter is omitted, the search will begin at position 1.
string_being_searched is the string that will be searched.
string2 is the string to search for.
compare is optional. This is the type of comparison to perform. The valid choices are:
| VBA Constant | Value | Explanation |
|---|---|---|
| vbUseCompareOption | -1 | Uses option compare |
| vbBinaryCompare | 0 | Binary comparison |
| vbTextCompare | 1 | Textual comparison |
| vbDatabaseCompare | 2 | Comparison based on your database |
Note
If string2 is not found within string_being_searched, the Instr function will return 0.
If string_being_searched is zero-length, the Instr function will return 0.
If string_being_searched is null, the Instr function will return null.
If string2 is zero-length, the Instr function will return the value used in the start parameter. If the start parameter is omitted, the Instr function will return 1.
If start is greater than string2, the Instr function will return 0.
Applies To
- Access 2013, Access 2010, Access 2007, Access 2003, Access XP, Access 2000
For Example
| Instr (1, "Tech on the Net", "the") | would return 9 |
| Instr ("Tech on the Net", "the") | would return 9 |
| Instr (10, "Tech on the Net", "t") | would return 15 |
VBA Code
The Instr function can be used in VBA code. For example:
Dim LPosition As Integer LPosition = Instr (10, "Tech on the Net", "t")
In this example, the variable called LPosition would now contain the value 15.
SQL/Queries
You can also use the Instr function in a query.
