Follow us:
access functions

MS Access: Instr Function

This MSAccess tutorial explains how to use the Access Instr function with syntax and examples.

Description

The Microsoft Access Instr function returns the position of the first occurrence of a string in another string.

Syntax

The syntax for the Instr function in MS Access is:

Instr ( [start], string_being_searched, string2, [compare] )

Parameters or Arguments

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

The Instr function can be used in the following versions of Microsoft Access:

  • Access 2013, Access 2010, Access 2007, Access 2003, Access XP, Access 2000

Example

Let's look at how to use the Instr function in MS Access:

Instr (1, "Tech on the Net", "the")
Result: 9

Instr ("Tech on the Net", "the")
Result: 9

Instr (10, "Tech on the Net", "t")
Result: 15

Example in VBA Code

The Instr function can be used in VBA code in Microsoft Access.

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.

Example in SQL/Queries

You can also use the Instr function in a query in Microsoft Access.

For example:

Microsoft Access

In this query, we have used the Instr function as follows:

Expr1: Instr(1,[CategoryName],"t")

This query will return the position of the first "t" in the CategoryName 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: Instr(1,[CategoryName],"t")

The results would now be displayed in a column called Position.

Share: