Results 1 to 3 of 3
  1. #1
    ItsRoland is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jun 2018
    Posts
    111

    Applying a Filter to a Subform From Another Subform

    I have a form with a number of subforms, the important ones here being the main form, subform1, and subform2. If a new record is added to subform1, I want to apply a filter to subform2 that only shows records related to the new record created through subform1.

    In case this isn't obvious, all the subforms are open when the main form opens, but only the one the user want's to work with at any given moment will be visible. This is changed through command buttons.

    I've tried going about this a few different ways:

    1. I used a global variable that gets assigned a value anytime a new record is added through subform1, and tried to use that to tell subform2 whether or not to apply the filter. This would have worked, but I can't figure out where to place the code telling it if it should filter or not. I initially placed this in the OnOpen event, but all the subforms open when the main form does, so that doesn't work. I also tried placing it in the OnCurrent event, but that caused Access to crash (presumably because it was constantly checking for the value of the global variable).
    The code: (glbvar is set to 0 by default and then set to the appropriate ID value when a new record is added from subform1, in case that wasn't clear)
    Code:
    If glbvar > 0 Then
    
           Me.Filter = "fieldname = " & glbvar
          Me.FilterOn = True
    
    End If

    2. applying the filter to subform2 in the code of subform1, but that doesn't seem to work either. After a bit more digging on this one I don't think it's possible to do that, but I'm not 100%.

    3. applying the filter to subform2 in the code of the main form, had a similar result/issue as attempt 2, unsure if this can even be done.


    If anyone knows where I should place the code from attempt 1, if I can apply the filter to subform2 from another source like I tried in attempt 2 and 3, or another/better way of doing this, please let me know.



    Any help is greatly appreciated.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    You can apply filters but use the entire path. (See builder)
    if in sub1, filter sub2 via:

    forms!myForm!sub2!form.filter= forms!myForm!sub1!form.filter

  3. #3
    ItsRoland is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jun 2018
    Posts
    111
    I tried what you said, and got the error "Couldn't find field 'Form'", so I changed it up a bit and got it to work

    Forms!mainForn!subform2.Form.Filter = "fieldName = " & variable & " "


    Forms!mainForn!subform2.Form.FilterOn = True

    Thanks for the help!


    Side note, while I was testing this I noticed that the lost focus event on one of my controls doesn't trigger properly if I use tab to move away from the control, but does if I click away. When using tab it displays message boxes but doesn't call the function, or at least the function doesn't do anything. But when clicking away everything's fine. Is that supposed to happen or is there something else going on here? Just figured I'd ask while I was here lol

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

Similar Threads

  1. Replies: 1
    Last Post: 12-05-2016, 02:23 PM
  2. Replies: 4
    Last Post: 04-22-2013, 06:45 AM
  3. Replies: 6
    Last Post: 10-16-2012, 07:10 AM
  4. Applying AuditTrail to subform on a form
    By jle0003 in forum Access
    Replies: 2
    Last Post: 09-28-2012, 04:59 PM
  5. Access Reports - Applying a subform filter
    By AMCUser in forum Queries
    Replies: 13
    Last Post: 06-25-2010, 07:32 PM

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