Results 1 to 12 of 12
  1. #1
    pbDudley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2017
    Location
    New Orleans
    Posts
    50

    Create A Search Field on Form

    Hello


    I have one form created using a query. Then I dragged some tables onto the form, creating subforms, that are all tied together via one unique record.

    I can't figure out how to add one text field and one button where a user can enter a specific plan number (is unique), then click the button to populate the form.

    I'm assuming it must be something with:

    On the button, go to on click in properties, enter the VB code, and enter the code referring to the value in the text box but I don't know how right this.

    Any help is appreciated.

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2019
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by pbDudley View Post
    Hello
    I have one form created using a query. Then I dragged some tables onto the form, creating subforms, that are all tied together via one unique record.

    I can't figure out how to add one text field and one button where a user can enter a specific plan number (is unique), then click the button to populate the form.

    I'm assuming it must be something with:

    On the button, go to on click in properties, enter the VB code, and enter the code referring to the value in the text box but I don't know how right this.

    Any help is appreciated.
    Can you post a copy of the db
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    pbDudley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2017
    Location
    New Orleans
    Posts
    50
    I don't think I can, it has too much proprietary data in it.
    I do see this example:
    MS Access search for record by textbox instead of dropdown - Stack Overflow

    It doesn't involve a button, entering code into a text box, it looks fairly straight forward, but I'm getting an error I believe at the code that is red below (error states Compile error: user-defined type not defined. The only part I changed is the rs.FindFirst "[PLAN]=" & txtGoTo & """" I changed ID to PLAN
    Private Sub txtGoTo_AfterUpdate()
    If (txtGoTo & vbNullString) = vbNullString Then Exit Sub
    Dim rs As DAO.RecordSet
    Set rs = Me.RecordsetClone
    rs.FindFirst "[PLAN]=" & txtGoTo
    If rs.NoMatch Then
    MsgBox "Sorry, no such record '" & txtGoTo & "' was found.", _
    vbOKOnly + vbInformation
    Else
    Me.RecordSet.Bookmark = rs.Bookmark
    End If
    rs.Close
    txtGoTo = Null
    End Sub

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2019
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by pbDudley View Post
    Hello
    I have one form created using a query. Then I dragged some tables onto the form, creating subforms, that are all tied together via one unique record.

    I can't figure out how to add one text field and one button where a user can enter a specific plan number (is unique), then click the button to populate the form.

    I'm assuming it must be something with:

    On the button, go to on click in properties, enter the VB code, and enter the code referring to the value in the text box but I don't know how right this.

    Any help is appreciated.
    Why not use the wizard to create a combo box that will find the record selected in that combo. The sub forms should update automatically.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    dblife's Avatar
    dblife is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2011
    Location
    South Shields, UK
    Posts
    104
    If you go to the underlying query and make it ask for the value you want to search for.
    Say your search field on the form (fMain) is called txtSearch
    If the key field is called employeeID go to the underlying query and in the 'criteria' field for employeeID, ask it to look in txtSearch on the form fMain by typing =forms!fMain!txtSearch
    As long as the form is based on the query, it should filter the form record you see.
    If you need to change the value once the form is opened, add a 'refresh' button to requery the form fMain (you can google the code for that)

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,946
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Code:
    rs.FindFirst "[PLAN]=" & txtGoTo
    If Plan is a text datatype it needs to be delimited.

    Code:
    rs.FindFirst "[PLAN]=""" &  txtGoTo & """"
    also
    Code:
    Me.RecordSet.Bookmark = rs.Bookmark
    should be
    Code:
    Me.Bookmark = rs.Bookmark
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  8. #8
    pbDudley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2017
    Location
    New Orleans
    Posts
    50
    Im still getting a compile error:

    Compile Error:


    User-defined type not defined

    'Code to filter the form for PLANS
    Private Sub txtGoTo_AfterUpdate()
    If (txtGoTo & vbNullString) = vbNullString Then Exit Sub
    Dim rs As DAO.Recordset
    Set rs = Me.RecordsetClone
    rs.FindFirst "[PLAN]=""" & txtGoTo & """"
    If rs.NoMatch Then
    MsgBox "Sorry, no such record '" & txtGoTo & "' was found.", _
    vbOKOnly + vbInformation
    Else
    Me.Bookmark = rs.Bookmark
    End If
    rs.Close
    txtGoTo = Null
    End Sub

    It seems to not like this
    Dim rs As DAO.Recordset

    But I could be wrong on this.

    I can try the macro method and adding the criiteria to the query as some others mentioned, I just thoght this would be an easy snippet of code for this, Ill keep testing though, thank you for the help

  9. #9
    pbDudley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2017
    Location
    New Orleans
    Posts
    50
    Or I'm trying to filter all records on a form and this code is only made for a text box. I can try the macro and query options

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,946
    What version of Access are you using?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Here's a simple example.

    The first combobox filters by number datatype.

    the 2nd combobox filters by text datatype

    the textbox filters by number(autonumber)
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  12. #12
    pbDudley is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2017
    Location
    New Orleans
    Posts
    50
    Hello
    I got the search code to work:
    Private Sub txtGoTo_AfterUpdate()
    If (txtGoTo & vbNullString) = vbNullString Then Exit Sub
    Dim rs As DAO.Recordset
    Set rs = Me.RecordsetClone
    rs.FindFirst "[PLAN]=""" & txtGoTo & """"
    If rs.NoMatch Then
    MsgBox "Sorry, no such record '" & txtGoTo & "' was found.", _
    vbOKOnly + vbInformation
    Else
    Me.Recordset.Bookmark = rs.Bookmark
    End If
    rs.Close
    txtGoTo = Null
    End Sub

    I think the problem was I didnt have certain reference libraries checked in my VB. Another user mentioned this to me and it worked on another issue and now this code works. It fills out the form when I enter the PLAN number, than the whole form populates with the correct data.
    Thank you all for the help
    Last edited by pbDudley; 04-25-2024 at 10:19 AM.

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

Similar Threads

  1. Replies: 2
    Last Post: 05-31-2023, 04:25 AM
  2. Replies: 6
    Last Post: 09-02-2016, 02:12 PM
  3. Replies: 1
    Last Post: 06-25-2014, 01:05 PM
  4. Replies: 5
    Last Post: 07-13-2012, 01:15 AM
  5. Replies: 4
    Last Post: 05-29-2012, 01:32 PM

Tags for this Thread

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