Results 1 to 7 of 7
  1. #1
    excellenthelp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Mar 2014
    Posts
    93

    Search in a Form with Wildcards

    Hello Accessers,

    I am wondering what I am not writing properly in this code. It is for a user to type in a textbox where it will search for whatever is being typed in there- like a search box.

    It will continue to say an error: "Run-Time error '13': Type Mismatch".

    Code:
    Private Sub btnFind_Click()
    
        If (TxtFind & vbNullString) = vbNullString Then Exit Sub
        Dim rs As DAO.Recordset
        Set rs = Me.RecordsetClone
        rs.FindFirst "[ACA Name2] = " * " & TxtFind & " * ""
        If rs.NoMatch Then
            MsgBox "Sorry, no such record '" & TxtFind & "' was found.", _
                   vbOKOnly + vbInformation
        Else
            Me.Recordset.Bookmark = rs.Bookmark
        End If
        rs.Close
        TxtFind = Null
    End Sub
    The line that is red highlighted is where the debugger leads me to after attempting to execute the code.

    Help....?

    Thanks!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    Its a string , you need quotes surrounding it...

    rs.FindFirst "[ACA Name2] =' * " & TxtFind & " *' "

  3. #3
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Try

    rs.FindFirst "[ACA Name2] = '*" & TxtFind & "*'"

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Wildcard usually used with LIKE operator. But I have not tried with the FindFirst method.

    Use LIKE instead of = operator and apostrophe delimiters and no spaces around the * :

    rs.FindFirst "[ACA Name2] LIKE '*" & TxtFind & "*'"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    excellenthelp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Mar 2014
    Posts
    93
    Ranman256,

    Thank you. It still could not find the record in the form. But this time, there is no error. =) I had double checked the spelling in the textbox (it is only three letters!)
    Hmm...

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    hah completely missed that june, was just going to comment that you have spaces before and after your text string so it's looking for that.

    For instance if you had

    ABCDEFG

    ABC DEF G

    and txtFind was DEF you would only find the second record, not the first that's why I stripped out the spaces

  7. #7
    excellenthelp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Mar 2014
    Posts
    93
    It now works!! The code is:

    Code:
    Private Sub btnFind_Click()
    
        If (TxtFind & vbNullString) = vbNullString Then Exit Sub
        Dim rs As DAO.Recordset
        Set rs = Me.RecordsetClone
        Dim strSearch As String
        
        strSearch = "[ACA Name2] LIKE '*" & TxtFind & "*'"
        rs.FindFirst strSearch
        If rs.NoMatch Then
            MsgBox "Sorry, no such record '" & TxtFind & "' was found.", _
                   vbOKOnly + vbInformation
        Else
            Me.Recordset.Bookmark = rs.Bookmark
        End If
        rs.Close
        TxtFind = Null
    
        End Sub
    Thanks for the help!!!

    Marking this as SOLVED.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 07-22-2013, 12:42 PM
  2. Replies: 3
    Last Post: 07-02-2013, 01:39 PM
  3. Replies: 1
    Last Post: 04-20-2012, 03:16 AM
  4. VBA Wildcards
    By dssrun in forum Programming
    Replies: 11
    Last Post: 03-31-2011, 08:44 AM
  5. Using wildcards (*) in SQL
    By SIGMA248 in forum Queries
    Replies: 1
    Last Post: 07-22-2010, 08:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums