MS Access: InstrRev Function
This MSAccess tutorial explains how to use the Access InstrRev function with syntax and examples.
Description
The Microsoft Access InstrRev function returns the position of the first occurrence of a string in another string, starting from the end of the string. This is similar to the Instr function which returns the position of the first occurrence, starting from the beginning of the string.
Syntax
The syntax for the InstrRev function is:
InstrRev ( string_being_searched, string2 [, start [ , compare] ] )
Parameters or Arguments
- string_being_searched
- The string that will be searched.
- string2
- The string to search for.
- start
- Optional. It is the starting position for the search. If this parameter is omitted, the search will begin at position -1 which is the last character position.
- compare
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 InstrRev function will return 0.
- If string_being_searched is zero-length, the InstrRev function will return 0.
- If string_being_searched is null, the InstrRev function will return null.
- If start is null, the InstrRev function will return #Error.
Applies To
The InstrRev function can be used in the following versions of Microsoft Access:
- Access 2019, Access 2016, Access 2013, Access 2010, Access 2007, Access 2003, Access XP, Access 2000
Example
Let's look at how to use the InstrRev function in MS Access:
InstrRev ("alphabet", "a") Result: 5 InstrRev ("alphabet", "a", -1) Result: 5 InstrRev ("alphabet", "a", 1) Result: 1 InstrRev ("alphabet", "a", 2) Result: 1 InstrRev ("alphabet", "a", 3) Result: 1 InstrRev ("alphabet", "a", 4) Result: 1 InstrRev ("alphabet", "a", 5) Result: 5 InstrRev ("alphabet", "a", 6) Result: 5 InstrRev ("alphabet", "a", 7) Result: 5 InstrRev ("alphabet", "a", 8) Result: 5 InstrRev ("alphabet", "a", 9) Result: 0
Example in VBA Code
The InstrRev function can be used in VBA code in Microsoft Access.
For example:
Dim LPosition As Integer LPosition = InstrRev ("alphabet", "a")
In this example, the variable called LPosition would now contain the value 5.
Example in SQL/Queries
You can also use the InstrRev function in a query in Microsoft Access.
For example:
In this query, we have used the InstrRev function as follows:
Expr1: InstrRev([LastName],"v")
This query will return the position of the last "v" in the LastName field. The results will be displayed in a column called Expr1. You can replace Expr1 with a column name that is more meaningful.
For example:
Position: InstrRev([LastName],"v")
The results would now be displayed in a column called Position.
Frequently Asked Questions
Question: In Microsoft Access, all values in a column have a file extension suffix. How can I remove the suffix and the "." from all values in this column?
Answer: You can use the InstrRev function to remove a file extension. For example, if we had a column called Filename that contained values as follows:
And we wanted to remove the extension from each Filename, we could use the InstrRev function as follows:
Left([Filename],InStrRev([Filename],".")-1)
This would return the following query results:
Advertisements