The creation and customization of database applications using Microsoft Access
You might like to take a look at FindRecord.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.
This little demo includes two methods for searching on the basis of a value in a row in a subform. The first filters the parent form only, using the following code:
Private Sub cboLastname_AfterUpdate()
Const MESSAGETEXT = "No matching records found."
Dim ctrl As Control
Dim strFilter As String
Set ctrl = Me.ActiveControl
strFilter = "ProjectID IN(SELECT ProjectID " & _
"FROM ContactProjects WHERE ContactID = " & ctrl & ")"
If Nz(ctrl, 0) = 0 Then
' turn off filter
Me.FilterOn = False
Else
If Not IsNull(DLookup("ContactID", "ContactProjects", "ContactID = " & ctrl)) Then
' filter form to name selected in combo box
Me.Filter = strFilter
Me.FilterOn = True
Else
' inform user if no matching records found and show all records
MsgBox MESSAGETEXT, vbInformation, "Warning"
Me.FilterOn = False
Me.Requery
End If
End If
End Sub
The second filters both the form and subform, using the following code:
Private Sub cboLastname_AfterUpdate()
Const MESSAGETEXT = "No matching records found."
Dim ctrl As Control
Dim strFilter As String
Set ctrl = Me.ActiveControl
strFilter = "ProjectID IN(SELECT ProjectID " & _
"FROM ContactProjects WHERE ContactID = " & ctrl & ")"
If Nz(ctrl, 0) = 0 Then
' turn off filter
Me.FilterOn = False
Me.fsubContacts.Form.FilterOn = False
Else
If Not IsNull(DLookup("ContactID", "ContactProjects", "ContactID = " & ctrl)) Then
' filter form to name selected in combo box
Me.Filter = strFilter
Me.FilterOn = True
' filter subform to selected contact
Me.fsubContacts.Form.Filter = "ContactID = " & ctrl
Me.fsubContacts.Form.FilterOn = True
Else
' inform user if no matching records found and show all records
MsgBox MESSAGETEXT, vbInformation, "Warning"
Me.FilterOn = False
Me.Requery
Me.fsubContacts.Form.FilterOn = False
End If
End If
End Sub