How To Search A String With The VBA Instr Function

The VBA Instr Function is very useful for searching a string
for a particular piece of text.

If for example you want to loop a column or a field in
a table of multiple rows, and want to identify prize rows that are 81.51, you would write something
like this:

Sub VBAInstrFunction()
    Dim dblTemp As Double
    Dim rst As Object
    Dim strSQL As String
    
    Dim intThere As Integer
    
    strSQL = "SELECT p_PrizePrice FROM Prizes "
    
    Set rst = New ADODB.Recordset
    
    rst.Open strSQL, CurrentProject.Connection
    
    Do Until rst.EOF
        dblTemp = rst.Fields("p_PrizePrice")
        intThere = InStr(1, "$81.51", dblTemp)
        
        If intThere > 1 Then
            MsgBox "found it"
        End If
        
        rst.MoveNext
    Loop
    
    rst.Close
    Set rst = Nothing
    
End Sub

 

Here is the table I am using:

VBA_Instr_Function

As you can see the VBA Instr Function is very useful.

Let me know if you have any specific questions




By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How To Parse A Flat File In Excel VBA

In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]

How can I interact with other Office applications (Excel) using VBA in Access?

Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]

How To Generate A XML File With Access VBA

XML is used to structure your data. You can send a CSV file to another person, but using XML, you can structure your data in a more elegant format, and get very specific about your fields, and what data is contained within your tags (your field names). This is different than a basic CSV file […]

Mouse Click Counter On Access Form

This post will demonstrate how you can count the number of clicks on your button in a certain time frame. It will function like a game . Here is the database form all in one: Option Compare Database Public m_dteStartTime As Date Public m_dteStopTime As Date Private Sub btnClicker_Click() Dim intValue As Integer Dim rst […]

Previous Post

How To Use The VBA Input Box Cancel Button To Exit Sub

Next Post

How To Make An InputBox In VBA