Results 1 to 9 of 9
  1. #1
    Camp is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    5

    Filter subform via listbox on main form

    I am new to access and struggling to get my sub-form to filter on a record i am selecting from a listbox on my main form.

    my main form is called frmInput, my sub-form is called frmSubform

    the two tables tblContractor and tblDetails are associated with each other via a one to many relationship, and i have a them linked together on the frmInput form i use for data input.



    I have tried various combinations of code to try and filter the sub-form based upon a value from a list box when i click on a row in the list box.

    The main form does filter when I click a row from the listbox (list29), however I cannot get the subform to filter.

    the list box has "SELECT tblContractor.ID, tblDetails.idTravel as the first two columns of the list box, i can assign the listbox values to variables.


    i can filter the main form using the following:

    [code here]

    Private Sub List29_Click()

    ' Trip ID
    Text45 = Me.List29.Column(1)


    X = Me.List29.Column(0)
    Me.FilterOn = True
    Me.Filter = "tblContractor.ID =" & X


    'when i attempt to filter the subform

    y = Me.List29.Column(1)

    Me!frmsubform.Form.Filter = "[tbldetails].[idtravel] =" & y
    Me!frmsubform.Form.FilterOn = True

    [/code]


    it fails

    i have attempted many variation of the subfrom filtering code to no eval, access keeps telling me it cant find frmsubform



    any help would be greatly appreciated

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    This should work if your col#s are correct.

    try adding a requery...
    Code:
    Me!frmsubform.Form.Filter = "[tbldetails].[idtravel] =" & y
    Me!frmsubform.Form.FilterOn = True
    frmsubform.requery
    if still not working, I sometimes use 2 queries.
    unfiltered: frmsubform.rowsource = "qsAllRecs"

    after list update
    Code:
    Text45= Me.List29.Column(1)
    if isnull(lst29) then
       frmsubform.rowsource = "qsAllRecs"
    else
       frmsubform.rowsource = "qsFilteredRecs"    'this uses sql based on text45
    endif

  3. #3
    Camp is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    5
    I get an error message stating that access can't find the field 'frmsubform' referred to in your expression.

  4. #4
    Camp is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    5
    I tried to just re-query the subform and i get the same can't find frmsubform error.

    Forms![frminput]![frmSubform].Form.Requery

    it acts like it cant find the subform, i have checked the spelling, it is correct.

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Let me see if I understand correctly.

    The master form is named "frmInput" ; the record source is table "tblContractor" (or a query based on tblContractor)
    The sub-form is named "frmSubform"; the record source is table "tblDetails" (or a query based on tblDetails)

    The main form and sub form are linked by the "Link Master field" and the "Link Child Fields".
    So the "Link Master field" is the PK field of the table "tblContractor" and the "Link Child Fields" is the FK field in table "tblDetails".

    When you select a record in the main form, the sub form changes records. Good so far?



    Now you want to use a list box to filter the main form.
    You have
    Code:
    X = Me.List29.Column(0)
            Me.FilterOn = True
            Me.Filter = "tblContractor.ID =" & X
    You cannot set the filter on, then set the filter expression.

    The code behind the list box should be:
    Code:
    Private Sub List29_Click()
    
       Me.Filter = "ID =" & Me.List29
       Me.FilterOn = True
    
    End Sub
    That is all it takes...... IF I understood correctly.

    (You should take the time to rename the objects. "List29" is a poor name as is "ID" for a field name)

    You should also have a button to remove the main form filter.
    Code:
    Private Sub btnRemoveFilter_Click()
        Me.Filter = ""   '<<-- not really needed, but I like to keep it clean
        Me.FilterOn = False
    End Sub

  6. #6
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    You must use the name of YOUR subForm. I don't know it.

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    If the main form and the sub form are linked by the "Link Master/Child" fields, filtering the main form will cause the sub form to show the related records. No filtering has to be done to the sub form.

  8. #8
    Camp is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    5

    close

    Quote Originally Posted by ssanfu View Post
    Let me see if I understand correctly.

    The master form is named "frmInput" ; the record source is table "tblContractor" (or a query based on tblContractor)
    The sub-form is named "frmSubform"; the record source is table "tblDetails" (or a query based on tblDetails)

    The main form and sub form are linked by the "Link Master field" and the "Link Child Fields".
    So the "Link Master field" is the PK field of the table "tblContractor" and the "Link Child Fields" is the FK field in table "tblDetails".

    When you select a record in the main form, the sub form changes records. Good so far?



    Now you want to use a list box to filter the main form.
    You have
    Code:
    X = Me.List29.Column(0)
            Me.FilterOn = True
            Me.Filter = "tblContractor.ID =" & X
    You cannot set the filter on, then set the filter expression.

    The code behind the list box should be:
    Code:
    Private Sub List29_Click()
    
       Me.Filter = "ID =" & Me.List29
       Me.FilterOn = True
    
    End Sub
    That is all it takes...... IF I understood correctly.

    (You should take the time to rename the objects. "List29" is a poor name as is "ID" for a field name)

    You should also have a button to remove the main form filter.
    Code:
    Private Sub btnRemoveFilter_Click()
        Me.Filter = ""   '<<-- not really needed, but I like to keep it clean
        Me.FilterOn = False
    End Sub




    The main form and sub form are linked by the "Link Master field" and the "Link Child Fields".
    So the "Link Master field" is the PK field of the table "tblContractor" and the "Link Child Fields" is the FK field in table "tblDetails".

    What do PK and FK mean ?

    When you select a record in the main form, the sub form changes records. Good so far?

    Yes, this works, but the subform shows all the sub records, dozens for every record on the main form. What i am trying to do is get the Subfrom to go to the record I am interested in via clicking on the list box on the main form.

    I want to filter the sub form, based on the filtered main form via the list box which i use to filter the main form and the corresponding subform



    My code below:

    Private Sub List29_Click()

    ' Trip ID
    Text45 = Me.List29.Column(1)

    X = Me.List29.Column(0)

    Me.Filter = "tblContractor.ID =" & X
    Me.FilterOn = True

    y = Me.List29.Column(1)

    'frmSubform.Requery
    Me!frmSubform.Form.Filter = "[tbldetails].[idtravel] =" & y
    Me!frmSubform.Form.FilterOn = True


    Call HomeVisit


    End Sub



    X and Y return the desired values from the list box.

    the main form filters properly based upon the X value obtained from the list box

    But, when the code reaches the following line:

    Me!frmSubform.Form.Filter = "[tbldetails].[idtravel] =" & y

    I get the following error

    Run-time error '2465'

    Microsoft Access can't find the field 'frmsubform' referred to in your expression.

    thank you for helping




  9. #9
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

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

Similar Threads

  1. How to filter bound subform from unbound main form?
    By ittechguy in forum Programming
    Replies: 3
    Last Post: 10-25-2015, 09:12 PM
  2. Filter subform from text box on main form
    By ittechguy in forum Macros
    Replies: 5
    Last Post: 09-21-2015, 10:55 PM
  3. Replies: 1
    Last Post: 01-31-2015, 09:03 PM
  4. Filter main form based on subform data
    By Varda in forum Forms
    Replies: 9
    Last Post: 01-02-2015, 10:40 PM
  5. Replies: 3
    Last Post: 06-02-2012, 07:39 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