Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182

    Whats Wrong with My Code --- Searching For Text in a Field

    I have a single form with 300 records. One the form I have a textbox called FISNumber where I record an identification number. I also have a textbox field called txtSearchFISNum and a button called btnSearchFISNum.



    I want to be able to type in a number into txtSearchFISNum and press btnSearchFISNum, and then be taken to the exact record on my form where FISNumber = txtSearchFISNum.

    This is what I am using:

    Public Sub GoToSearchFISNumb(FISNumber As Long)
    With Me.RecordsetClone
    .FindFirst "[FISNumber] = '" & FISNumber & "'"
    If Not .NoMatch Then Me.Bookmark = .Bookmark
    End With
    End Sub


    Private Sub btnSearchFISNum_Click()
    Me.GoToSearchFISNum Me.txtSearchFISNum
    End Sub

    However, the code doesnt work and only brings up the debug menu which highlights the second code.

  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,474
    If numeric, maybe:

    .FindFirst "[FISNumber] = " & FISNumber

  3. #3
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    I tried that as well. But I still get the same error.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You would normally call a sub/function like:

    GoToSearchFISNum Me.txtSearchFISNum
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Try it this way:

    Code:
    Public Function GoToSearchFISNumb(argFISNumber As Long)
        With Me.RecordsetClone
            .FindFirst "[FISNumber] = '" & argFISNumber & "'"
            If Not .NoMatch Then Me.Bookmark = .Bookmark
        End With
    End Function
    
    Private Sub btnSearchFISNum_Click()
        Call GoToSearchFISNumb(FISNumber)
    End Sub

  6. #6
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Thank you Davegri. I tried the code, but the debug menu came up and highlight
    .FindFirst "[FISNumber] = '" & argFISNumber & "'"

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    What is the exact error message?

    Could put the function code directly in the Click event. Use a separate function if you need to call the same procedure from somewhere else.

    Instead of textbox, why not a combobox so users can enter/select only valid data?
    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.

  8. #8
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    What is the recordsource of your form? If the form is not bound to a query or table, there is no RecordsetClone.

  9. #9
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    The Error:

    Run-time error '3464' Data mismatch in criteria expression.

    And I am basically using the textfield [txtSearchFISNum] and the button, btnSearchFISNum, to search through the 200+ records in my form so that txtSearchFISNum=FISNumber. FISNumber is the form bound to my table. txtSearchFISNum is unbound.

    And yes. The form is bound to a table

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    What is actually saved into FISNumber field? Is it a number - perhaps the primary key of lookup table? If that is the case then of course there is a data type conflict because you are entering a text value in the textbox. Using a multi-column combobox instead would eliminate this issue.
    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.

  11. #11
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Ah. Then... that is likely the issue. I was unaware. The FISNumber field is my primary key and an autonumber.

    And if I use a combobox, wont I end up with a dropdown list of 200+ numbers?

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Yes, but 200+ is not very many. Users can still type in the text part of the combobox and as they type will find match in the dropdown if there is one.
    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.

  13. #13
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Is there a way to do it without a combobox?

    And additionally, if I do use the combobox, I would need to set its record source to the FISNumber field in my table. Right?

    Also, the 200 will quickly evolve into several thousand

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    The search would have to compare to the text field that has the descriptive text.

    Combobox has a ControlSource and RowSource, not a RecordSource.

    Controls used to input search criteria should be UNBOUND. Review http://allenbrowne.com/ser-62.html


    Did you create lookups in table? I NEVER do that. Most experienced developers here also recommend not to.
    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.

  15. #15
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    June7,

    Sorry about that. I meant RowSource.

    And the control for my search criteria is unbound.

    I have created lookups in this table. But none of those lookups would apply to what Im trying to accomplish here (I think)

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Text field wrong value
    By xcheshirecat in forum Forms
    Replies: 13
    Last Post: 06-14-2014, 10:14 AM
  2. Whats wrong with my query
    By Kirtap in forum Queries
    Replies: 5
    Last Post: 10-01-2013, 10:31 AM
  3. Replies: 1
    Last Post: 05-17-2012, 02:41 PM
  4. Whats wrong with this code?
    By shabbaranks in forum Programming
    Replies: 2
    Last Post: 03-20-2012, 08:01 AM
  5. Replies: 20
    Last Post: 09-18-2010, 02:31 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