totn 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
Optional. It is the starting position for the search. If this parameter is omitted, the search will begin at position 1.
string_being_searched
The string that will be searched.
string2
The string to search for.
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

  • The first position in string_being_searched is 1.
  • When finding the location of string2 in a string_being_searched, the InStr function does not perform a case-sensitive search.
  • 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 2019, Access 2016, 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("Tech on the Net", "T")
Result: 1    'Shows how start is defaulted to 1 if omitted

InStr(1, "Tech on the Net", "T")
Result: 1

InStr(1, "Tech on the Net", "t")
Result: 1    'Shows that search is not case-sensitive

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

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

InStr(1, "Tech on the Net", "M")
Result: 0    'Shows what is returned if string2 is not found

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.