Results 1 to 4 of 4
  1. #1
    ittechguy is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Aug 2015
    Posts
    218

    How to filter bound subform from unbound main form?

    I have a main form which is used for data entry and a sub form which displays data (all data in tblCustomer). The main form is unbound. In the header I have text boxes and combo boxes to add new records. In the footer, there are text boxes and combo boxes to filter the records displayed in the subform. For an example, if I only wanted to see records for one organization, I'd select the organization and hit the search button.

    The form looks like this:



    Click image for larger version. 

Name:	form.png 
Views:	14 
Size:	46.4 KB 
ID:	22496

    I'm having issue with the search form in the footer. I need to filter the subform based on values selected from the main form's footer.

    Here is my VBA code:

    Code:
    Private Sub cmdSearch_Click()
    Dim startStr As String
    Dim strFilter As String
    If Not IsNullOrEmpty(Me.cboSearchOfficeSym) Then
        startStr = IIf(strFilter = "", "", " AND ")
        strFilter = strFilter & startStr & " Forms!frmMainAddCustomer!frmSubAddCustomer.Form.OfficeSymPK = " & Me.cboSearchOfficeSym
    End If
    Call MsgBox(strFilter, vbOKOnly, "Debug")
    If DCount("*", "qrySubAddCustomer", strFilter) = 0 Then
                    MsgBox "No corresponding records to your search criteria." & vbCrLf & vbCrLf
    Call cmdReset_Click
    End If
    Forms!frmMainAddCustomer!frmSubAddCustomer.Form.Filter = strFilter
    Forms!frmMainAddCustomer!frmSubAddCustomer.Form.FilterOn = True
    End Sub
    I believe the issue I'm having is that I can't just use [OfficeSymFK] because that doesn't exist in the main form's recordsource. I need to either reference the subform, or the query for the subform. How do I do that?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    When creating the filter string, you just use the field name, not the whole form reference. It doesn't matter if that field is in the main form since you're using the string in the subform's filter.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ittechguy is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Aug 2015
    Posts
    218
    Thanks pbaldy! That was much easier than I thought.

  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
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Filter subform from text box on main form
    By ittechguy in forum Macros
    Replies: 5
    Last Post: 09-21-2015, 10:55 PM
  2. Replies: 1
    Last Post: 01-16-2015, 09:28 AM
  3. Replies: 5
    Last Post: 02-12-2014, 11:52 PM
  4. Replies: 3
    Last Post: 06-02-2012, 07:39 PM
  5. Replies: 0
    Last Post: 05-09-2010, 08:43 AM

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