0
votes

I'm very new to both Access and VBA. I've created a search button that looks for different items depending on what is selected on the different combo boxes. However, I would like to add another search criteria where if I type text into a texbox called "txtNotes" I can look for records that look like a match on a table field called "Notes" inside the "tbl_ContructionOrders" table. The matches have to be somewhat loose as everyone types notes differently and would like to use this box to find work orders where we had similar issues and maybe have an easier way finding a solution for said problems.

This is what I have so far and it is not working

Private Sub btnLookup_Click()
Dim strWhere As String


Me.Filter = True
strWhere = ""


    If IsNull(Me.txtNotes) Then
    
        If Not IsNull(Me.txtNotes) Then
        
            If strWhere <> "" Then
                strWhere = strWhere & " like [Notes] = """ & Me.txtNotes & """"
            
        Else
                strWhere = strWhere & "[Notes] = """ & Me.txtNotes & """"
             End If
         End If
         
         If Len(strWhere) <= 0 Then
        MsgBox "No Criteria", vbInformation, "No Input."
    Else
        Me.Filter = strWhere
        Me.FilterOn = True
        Me.Requery
    End If
    
    
    If Me.FilterOn Then
        If Me.Recordset.RecordCount = 0 Then
            MsgBox "Nothing Found."
        End If
        
    End If
    End Sub
2
Do you want search or filter? - Harun24HR
I suppose I want to filter the records that somewhat match what I type in the text box - Christian Cruz

2 Answers

0
votes

Try this:

Private Sub Command0_Click()

    'empty notes
    If IsNull(txtNotes.Value) Then
        MsgBox "No Criteria", vbInformation, "No Input."
        Exit Sub
    End If
    
    'search
    Filter = "[Notes] Like '*" & txtNotes.Value & "*'"
    FilterOn = True
    
    'count records
    If RecordsetClone.RecordCount = 0 Then
        MsgBox "Nothing Found."
        FilterOn = False
    End If
    
End Sub

If you want to search (filter) as you type, use this:

Private Sub txtNotes_Change()

    'search after x number of chars
    If Len(txtNotes.Text) <= 3 Then
        FilterOn = False
        Filter = vbNullString
    Else
        Filter = "[Notes] Like '*" & txtNotes.Text & "*'"
        FilterOn = True
    End If
    
End Sub
0
votes

It sounds like you want an auto-complete object. Jut the words that are to be auto-filled in a table. Use a combo box control on the form to select the word. Here is an instructional video that shows you how to set this up.

https://www.youtube.com/watch?v=ptRb8ffv4f0

If you need something else, post back.