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: Oldest
  1. carmens 6 Reputation points
    2022-12-20T19:06:38.617+00:00

    @Ken Sheridan Unfortunately I can't share things outside of my organization on sites like OneDrive or Google Drive without an email/name to send it to

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Ken Sheridan 3,581 Reputation points
    2022-12-20T21:09:01.393+00:00

    You can forward it to me at the following (munged) email address:

    kenwsheridan<at>yahoo<dot>co<dot>uk

    All subsequent correspondence will be via this forum.

    Was this answer helpful?

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

    I've had a look at your file. The problem wasn't really with your code, but that the forms' DataEntry property was set to True (Yes). This property is confusingly named. What it does is restrict the form so that only new records can be entered. Previously entered records are not returned in the form. Consequently when you searched for a record nothing was found.

    Also, the LinkMasterFields and LinkChildFields properties of the subform included multiple fields. Is this correct? With your sample data no records where returned in the subform at all. When I changed it to just the primary and foreign keys, i.e. Furnace Run ID in each case, at least one record was returned in the subform for each parent record.

    There are a number of other design faults, but, as these are not directly related to the current issue. I won't confuse matters with them here.

    As far as the code is concerned I've amended that behind the search button in the subform to the following so that it now navigates to the relevant records in the parent form and subfom, rather than filtering the forms:

    Private Sub Search_SN_Button_Click()

     Const MESSAGETEXT = "No matching records found."  
     Dim ctrl As Control  
     Dim strFilter As String  
       
     ' turn off filter  
     Me.FilterOn = False  
     Me.[Header Data Form].Form.FilterOn = False  
    
          
     Set ctrl = Me.[Header SN Placeholder]  
        'Header SN Placeholder is not bound to the table, formula equals text entered in Header SN Field textbox on subform  
     strFilter = Nz(DLookup("[Furnace Run ID]", "[Header Data]", "[Header SN] = """ & ctrl & """"), "0")  
          
     If Nz(ctrl, "0") = "0" Then  
         ' requery parent form  
         Me.Parent.Requery  
     Else  
         With Me.Parent.RecordsetClone  
            .FindFirst "[Furnace Run ID] = " & strFilter  
            If Not .NoMatch Then  
                Me.Parent.Bookmark = .Bookmark  
                ' navigate to record in subform  
                With Me.RecordsetClone  
                    .FindFirst "[Header SN] = """ & ctrl & """"  
                    If Not .NoMatch Then  
                        Me.Bookmark = .Bookmark  
                    End If  
                End With  
            Else  
                ' inform user if no matching records found  
                MsgBox "Sorry, no such record '" & [Header SN Placeholder] & "' was found.", _  
                vbOKOnly + vbInformation  
                ' requery parent form  
                Me.Parent.Requery  
            End If  
        End With  
    End If  
    

    End Sub

    I've left the code behind the search button in the parent form so that it filters the forms, but if you wanted to use that button rather than the subform's button, you should be able to adapt the code easily enough.

    Regards,

    Ken

    Was this answer helpful?

    0 comments No comments
  4. carmens 6 Reputation points
    2022-12-21T17:58:53.457+00:00

    @Ken Sheridan If Data Entry is off, will the user be able to edit to record that is navigated to in this code? That is the main goal of this, so comments/test data after a furnace run can be entered while everything else can be entered before the run. I also want the form to open on a blank, new form. With the updated code you sent, it is opening on the first record (I am also not a fan of navigation buttons). For the LinkMasterFields and LinkChildFields properties of the subform, yes there are fields on the main table (lot, numbers of x) that I want to be added to the header data table as well and there are fields for these on that table. I still can't get anything to come up in the subform, I need to look into this a bit more.

    Any other recommendations to improve the design are definitely appreciated! This is my first data base - we are establishing a new process entirely so I am trying to set up a comprehensive data recording method.

    Was this answer helpful?

    0 comments No comments
  5. Ken Sheridan 3,581 Reputation points
    2022-12-21T21:08:55.983+00:00

    Firstly, I did spot one mistake I'd made in the Click event procedure of the search button in the subform. To turn off any curent filters the code should have been:

     ' turn off filter  
     Me.FilterOn = False  
     Me.Parent.FilterOn = False  
    

    Also I found an error in the Click event of the search button on the parent form. It should read:

             ' inform user if no matching records found  
             MsgBox "Sorry, no such record '" & Me.HeaderSN & "' was found.", _  
             vbOKOnly + vbInformation  
    

    The DataEntry property must be set to False (No) for the form to return previously entered records, and yes, you will be able to edit them. To open the form at an empty new record put the following code in its Load event procedure:

    As regards the LinkMasterFields and LinkChildFields properties, inserting multiple values from columns in a referenced table is only valid if they are part of the referenced table's composite primary key, and therefore a part of a composite foreign key in the referencing table. Otherwise they introduce redundancy and the risk of update anomalies. The values can always be returned by joining the tables in a query, or by means of unbound controls in a form.

    As far as other design faults go, I'll just mention two obvious ones. Two of your tables, Header Data and Surrogate Data, have no primary key designated. Every table must have a primary key, which can be one column of distinct values, or a set of columns whose values in combination are distinct.

    None of the relationships in the database have referential integrity enforced. An unenforced relationship serves no purpose whatsoever, and leaves the database wide open to invalid data being inserted.

    I would strongly advise that you spend some time studying the principles of the database relational model, particularly the process of normalization. You might like to take a look at DatabaseBasics.zip and Normalizatio.zip in my public databases folder at:

    https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169

    If you have trouble downloading a specific file, clicking on the 'Download' button at the top of the page while no files are selected should download a zip file of all files in the folder, from which you should then be able to unpack the relevant file.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments