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

    Search Button to search entire records VBA

    Hello Accessers!

    I would like to know why the code below is not functioning properly. When I type in an acronym in the textbox, it keeps saying there is an error "Run-time error '3345': Unkown or invalid field reference 'ABO'." I do have ABO in the field.

    The dysfunctional code:


    Code:
    Private Sub btnFind_Click()
    
        If (TxtFind & vbNullString) = vbNullString Then Exit Sub
        Dim rs As DAO.Recordset
        Set rs = Me.RecordsetClone
        rs.FindFirst "[Acronym] = " & 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 red highlighted line is where the debugger leads me to. Something with identifying the field? I would like to enable the search procedure to search throughout the entire records rather than just a specific field. How may I write such a line or two?

    Thanks!

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

    Ah, the line works now. The highlighted line was updated from:
    Code:
    rs.FindFirst "[Acronym] = " & TxtFind
    to
    Code:
    rs.FindFirst "[Acronym] = """ & TxtFind & """
    However, I want to have the search perform the search in all of the fields rather than just a specific field. What is the parameter for this to replace [Acronym]?

    Awaiting for an excellent help.

    Thanks!

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    Text fields require criteria to be delimited with apostrophes. Date/time fields use #. Numbers don't need anything.

    rs.FindFirst "[Acronym] = '" & TxtFind & "'"

    EDIT: If you want to search all fields then have to build filter criteria for all fields. How many fields?
    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.

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

    Thanks for a quick response and short and clear explanation. There are 7 fields on the form that I would like to search from. They vary from Text, Number and Hyperlink.

    Hope this helps.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    I have a form with combobox for user to select field to search and then another combobox to enter search parameter. Code changes the second combobox RowSource based on first combobox selection. If it's a text field, they can select from list or enter any text. Then the code builds appropriate filter string for the selected field. If it's a text field then the string will be a LIKE and wildcard filter.

    Alternative is 7 unbound controls to input parameters then the search can include any or all. Review: http://allenbrowne.com/ser-62code.html
    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.

  6. #6
    excellenthelp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Mar 2014
    Posts
    93
    Thanks for the tip. I will play around with different ways that you suggested.

    Marking this as SOLVED.

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

Similar Threads

  1. Replies: 7
    Last Post: 08-08-2012, 03:28 PM
  2. Replies: 5
    Last Post: 07-13-2012, 01:15 AM
  3. Replies: 12
    Last Post: 03-22-2012, 02:48 AM
  4. Search Button
    By bomber72 in forum Forms
    Replies: 6
    Last Post: 08-03-2011, 11:44 AM
  5. QBF (search entire field)
    By 161 in forum Queries
    Replies: 9
    Last Post: 02-09-2011, 03:01 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