Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2009
    Posts
    223

    Red face Unanswered: Close active Form and Open a different Form

    Hi,

    I am creating a SubForm which will be located in the Page Footer of various Main Forms. The SubForm will contain Command buttons to take the user to different parts of the database.

    When the button is clicked, I want to Close the current Form (the name of the current Form will be different and so I cannot specify the actual name of it).

    Then I want to Open a new Form (lets call it 'Employee Menu Form').

    I wanted to create a Macro (I am a newbie and this seemed to be the easiest way). But the Macro asks me the name fo the Form which i want to Close. As the name of the Form will be different each time, I cannot give it a name and so I cannot make a Macro - shame Can I put "not known" in the Macro?

    So I have tried to work in Visual Basic (see below).

    Please could someone tell me how to do a simple Macro, or maybe check my code to see which of the below options are "correct" coding??

    Thanks ever so much.

    Option 1
    Private Sub Close_Form_Click()
    On Error GoTo Err_Close_Form_Click

    DoCmd.Close
    DoCmd.OpenForm "Employee Form"

    Exit_Close_Form_Click:
    Exit Sub

    Err_Close_Form_Click:
    MsgBox Err.Description
    Resume Exit_Close_Form_Click

    End Sub



    Option 2

    Private Sub Goto_Employee_Click()
    On Error GoTo Err_Goto_Employee_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "Employee Form"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

    Exit_Goto_Employee_Click:
    Exit Sub

    Err_Goto_Employee_Click:
    MsgBox Err.Description
    Resume Exit_Goto_Employee_Click

    End Sub

  2. #2
    Join Date
    Feb 2004
    Location
    New Zealand
    Posts
    1,424
    Provided Answers: 8
    try

    DoCmd.Close acForm, "[theformyouwanttoclose]"
    hope this help

    See clear as mud


    StePhan McKillen
    the aim is store once, not store multiple times
    Remember... Optimize 'til you die!
    Progaming environment:
    Access based on my own environment: DAO3.6/A97/A2000/A2003/A2007/A2010
    VB based on my own environment: vb6 sp5
    ASP based on my own environment: 5.6
    VB-NET based on my own environment started 2007
    SQL-2005 based on my own environment started 2008
    MYLE
    YOUR PASSWORD IS JUST LIKE YOUR TOOTHBRUSH DON'T SHARE IT.

  3. #3
    Join Date
    Nov 2009
    Posts
    223
    thanks for your reply.

    the form i wish to close will bwe different every time. is ther a code for closing a form with whatever name the active formj is at that particlular time?

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    If the button is in a subform, you can use:
    Code:
    DoCmd.Close acForm, Me.Parent.Name
    Have a nice day!

  5. #5
    Join Date
    Nov 2009
    Posts
    223
    Yes, the button is in a SubForm.

    But the same SubForm will be in the Page Footer of many different Parent Forms.

    Therefore, it is not possible to specify the Parent.Name because the Parent will be different all the time.

    I wonder if there is any code which will Close a Form without specifying the Name of the Form to Close???

  6. #6
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Me.Parent.Name will always return the name of the parent form, whatever it can be.
    Have a nice day!

  7. #7
    Join Date
    Nov 2009
    Posts
    223
    OK.

    So if I type

    "Me.Parent.Employee Form" then that will close whichever Parent form is currently open?

    Cool - thanks.

  8. #8
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    No literally: Me.Parent.Name. In this expression Parent is a property that represents a handle (a pointer) to the parent form and Name is one of its properties.

    If you have a form named "MyParentForm" that has a subform named "MySubForm", from MySubForm Me.Parent.Name yields "MyParentForm". Now if you have another form named "MyOtherParentForm" that also has the form MySubForm as subform, this time from MySubForm the value of Me.Parent.Name will be "MyOtherParentForm".
    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •