Search for subform record based on text box

carmens 6 Reputation points
2022-12-12T18:31:50.943+00:00

I am trying to bring up a form record based on text entered in a text box.

The form "Furnace + Header Data Form" is a parent form, with 2 subforms called "Header Data Form" and "Surrogate Data Form". This question is in relation to the "Header Data Form" subform.
The parent form is used to enter data on different furnace runs, which is connected to the table "Furnace Run Data". Each furnace run will have several headers in it, and each header has a serial number. I added a subform called "Header Data Form", which will allow the user to enter information specific to each header serial number. Data that is entered into the "Header Data Form" subform adds data to the "Header Data" table and is connected to the furnace data table via the Furnace Run ID. The Furnace Run ID is on both the parent form and as an invisible field on the Header Data subform. All fields on the Header Data Form are connected to the Header Data Table.

As a side note, there is a button within this subform to add a new record. Once the button is clicked, a new record with the furnace data and header data is saved and the header data subform is cleared. When you enter more data into the header data form and click the add button, another line (with the same Furnace Run ID) will be added to the header data table. No data is added to the furnace table. This all works great. There are multiple records in the Header Data table for each Furnace Run ID.

Initially, I was trying to find the record based on a text field named "Header SN Field" on the subform named "Header Data Form". This text field is connected to the "Header SN" Field from the "Header Data" table. Upon clicking the search button (on the Header Data subform), I wanted to use the text typed into the Header SN Field to search for the previous record in the Header Data table that has Header SN (field in Header Data table) = Header SN Field (field in form where user enters text). I could get this to run with no errors, but I kept getting the message saying that no matching record was found, even though there definitely is an existing record with the matching Header SN. My code for this is below.

Private Sub Search_SN_Button_Click()
If ([Header SN Placeholder] & vbNullString) = vbNullString Then Exit Sub
Dim strCriteria As String
Dim rs As DAO.Recordset

Set rs = Me.RecordsetClone  
  
strCriteria = "[Header SN]='" & Me.[Header SN Field] & "'"  
  
rs.FindFirst strCriteria  
  
If rs.NoMatch Then  
    MsgBox "Sorry, no such record '" & [Header SN Field] & "' was found.", _  
           vbOKOnly + vbInformation  
Else  
    Me.Bookmark = rs.Bookmark  
End If  
rs.Close  
txtGoTo = Null  

End Sub

After searching a bit, I thought the solution might be to have a text box and search button on the main form instead. The text box on the main form is called "HeaderSN". I am still getting the message that there is no existing record that matches. My code for this is below.
Private Sub Search_Header_SN_Record_Click()
If (HeaderSN & vbNullString) = vbNullString Then Exit Sub
Dim strCriteria As String
Dim rs As DAO.Recordset

Set rs = Me.[Header Data Form].Form.RecordsetClone  
  
strCriteria = "Header SN = '" & Me.HeaderSN & "'"  
  
rs.FindFirst strCriteria  
  
If rs.NoMatch Then  
    MsgBox "Sorry, no such record '" & [HeaderSN] & "' was found.", _  
           vbOKOnly + vbInformation  
Else  
    Me.[Header Data Form].Form.Bookmark = rs.Bookmark  
End If  
rs.Close  
txtGoTo = Null  

End Sub

Can someone please help me with this? I have tried a million different ways to do this and have been searching for days on different forums but I can't figure it out.

Microsoft 365 and Office | Access | Development
Developer technologies | Visual Basic for Applications

Locked Question. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

31 answers

Sort by: Newest
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. carmens 6 Reputation points
    2022-12-22T18:26:53.697+00:00

    @Ken Sheridan Just realized it was because the Macros on the file were blocked and that is why it wasn't working, now it works! Thank you so much for the help!!

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  3. Ken Sheridan 3,581 Reputation points
    2022-12-22T17:43:18.97+00:00

    Are you saying that you cannot get the form to open at an empty new record in the amended copy which I sent you with my last post? If so I can offer no explanation for that.

    If you are saying that it does not work in your operational file. If so I would suspect the multi-field LinkMasterFields and LinkChildFields properties in your case. No subform records are returned at all if I use the multi-field LinkMasterFields and LinkChildFields properties. You have not addressed my comments about this suggesting redundancy in the referencing table.

    With my copy of the file it works as expected, the form opening at an empty new record, and filtering both the parent and subform if the search button in the parent form is used, or navigating to the matching records in the parent form and subform if the search button in the subform is used.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  4. carmens 6 Reputation points
    2022-12-22T16:40:58.627+00:00

    @Ken Sheridan Thanks for all the help on this. I still can’t get it to work and even with the code to load on a new record, it still loads on the first record. When opening the parent form w/ subform, both the subform and parent form open on the parent's first record. When opening the subform separately, it opens on the subforms first record. I was trying this out yesterday and got it to work when on load with a macro instead of VBA (GoToRecord New), which I don’t understand. Were you able to get anything to load in the form on your end? I tried changing the data entry mode to no of the older code that used filters instead of findfirst/recordsetclone, but I still can’t get the subform to load – only the parent form loads.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  5. Ken Sheridan 3,581 Reputation points
    2022-12-22T13:12:27.513+00:00

    I've sent the latest version of your file directly to you, with the corrections mentioned in my last post, and the code in the form's Load event procedure to move the form to an empty new record when it opens.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments