Results 1 to 4 of 4
  1. #1
    qprjohn121 is offline Novice
    Windows Vista Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    3

    Subform to be filtered when the main form opens

    Hi


    My app is working with 'Organisations' and 'Contacts' (one Organisation can have many Contacts).

    When the user selects a particular organisation from a list on one form, I want to open a new form that contains some Organisation info plus a list of Contacts that belong only to that Organisation. I want to do this in VBA but can't get it to work.

    I pass the selected Organisation primary key to the new form with:

    DoCmd.OpenForm "frmViewOrganisationContacts", , , , , acDialog, OpenArgs:=Me.txtOrganisation_PK

    In the new form that opens, I have tried:

    Private Sub Form_Open(Cancel As Integer)
    Dim mySQL As String
    mySQL = "SELECT vwContact.* FROM vwContact WHERE Organisation_FK = " & Me.OpenArgs & " ;"
    'Me.frmViewContacts.Form.RecordSource = mySQL
    'Me.frmViewContacts.Requery
    End Sub



    but I get the error: "The expression you entered refers to an object that is closed or does'nt exist."

    I have tried using some of the 'later' events in the form loading process, to give the subform 'frmViewContacts' time to load before attempting to requery,but to no avail.

    Any ideas? Thanks
    Last edited by qprjohn121; 08-10-2012 at 12:41 PM. Reason: correction re actual code used

  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
    I would use this technique to filter the main form to the desired organization:

    BaldyWeb wherecondition

    I'd keep the subform in sync with the main form by using the master/child link properties of the subform control. If you want to do what you're doing, you'd probably want to use the open event of the subform, referring to the parent's OpenArgs.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    qprjohn121 is offline Novice
    Windows Vista Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    3
    Thanks for your suggestions.

    I got it to work by referencing Parent.OpenArgs in the subform's Open event:

    mySQL = "SELECT vwContact.* FROM vwContact WHERE vwContact.Organisation_FK = " & Me.Parent.OpenArgs & " ;"
    Me.RecordSource = mySQL
    Me.Requery

  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. Replies: 3
    Last Post: 04-17-2012, 10:28 AM
  2. Replies: 35
    Last Post: 12-21-2011, 02:16 PM
  3. Link main form with subform
    By lizzywu in forum Forms
    Replies: 1
    Last Post: 11-18-2011, 03:22 PM
  4. Replies: 3
    Last Post: 11-16-2011, 01:56 PM
  5. Replies: 5
    Last Post: 01-02-2011, 10:09 AM

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