Close main form with an if statement

Reese

Registered User.
Local time
Today, 18:28
Joined
Jan 13, 2013
Messages
387
(Solved) Close main form with an if statement

I have a subform that is used in multiple other forms. I wish to have a command button that will close whichever main form the subform is in. Is there some way I can make an if statement that is something like the following?

If (main form = A) then
Close (form A)
ElseIf (main form = B) then
Close (form B)
etc.
End if

Unfortunately the button has to be in the subform, not the main form (which would be easier). The purpose of the button is to delete the current records that are open and then close the form. The catch is that the subform is the parent record to the main form, so the command has to originate from the subform so that the cascade delete works properly.

Thanks!
 
Last edited:
First, take a look to this short article.
Once you have the parent name, seems to be a simple task to close the subform and the main form:

Code:
Private sub CloseButton_Click
  ' Code to close subform
  ' Code to close the parent form
End Sub
 
Do you only ever have one main form open, if so you can use the GetObjectState to test which main form is open
If SysCmd(acSysCmdGetObjectState, acForm, "frmA") <> 0 Then: close form A

David
 
First, take a look to this short article.

I looked at the article but it sounded like you needed to know the name of the parent form anyway, which doesn't make sense. At least that's how I interpreted their example:

Action: MsgBox
Message: ="Parent: " & Forms.Orders![Orders Subform].Form.Parent.Name
Title: Parent Form Name

The bold part looks like the code requires the form name to work anyway. Unless I can use a generic version like: " Forms.[Main Form Name]!...".

Am I reading that wrong?

Do you only ever have one main form open

It depends... There may be cases where that's the case but I can't say for sure. I don't think that there will be multiple forms that utilize this particular subform open at the same time. Ultimately the database will be shared from a server, would that cause any problem?
 
Clicking on the cmdClose button on the subform forces its outer most Parent Form to become the Screen.ActiveForm, so:-

Code:
Private Sub cmdClose_Click()

    DoCmd.Close acForm, Screen.ActiveForm.Name
    
End Sub

Chris.
 
Code:
Private Sub cmdClose_Click()
Dim ParentForm As Form
    Set ParentForm = Me.Parent
    DoCmd.Close acForm, ParentForm.Name
End Sub

The cmdClose is in the subform.
This work but, unfortunately, I have discovered something that seems to be a bug (A2007):
If I open multiple forms (main forms for this sub form) at the same time, the Me.Parent statement return, alwais, the last opened (main) form.
 
I have managed somehow this issue:

In a module:
Code:
Option Compare Database
Option Explicit

Public ParentFormForChild As Form
In each main (parent) form:
Code:
Private Sub Form_Activate()
    Set ParentFormForChild = Me
End Sub
In the child (subform) form:
Code:
Private Sub cmdClose_Click()
    DoCmd.Close acForm, ParentFormForChild.Name
End Sub
 
Thanks everyone for the suggestions. I don't know if I'll have time to try them until next week but I'll let everyone know the results when I do. In the meantime if anyone has any other ideas please let me know.

Thank.
 
Fun... when trying the first of these solutions I discovered a different problem that has stopped me in my tracks before I can even continue addressing this one.

I'll have to solve that one first. I've posted the issue here.
 
Okay, so the other problems which came up have been fixed. Back to this one. Thank you for all of your suggestions, but I've reached a road block with them.

All of your suggestions use ".Name" as part of the code but VBA debuger keepers coming up with "Method or data member not found." Did you guys mean that ".Name" should be replaced with the name of whichever form is the main form at that time?

If that's so doesn't that come back to the problem of the code needing to know what form is the current main form?

At this point I'm thinking of just separating the two functions--one button to delete the records in both forms and then one button in the main form to close the forms. Less intuitive for the user but easier for me and lets me roll out the database sooner. After all, in the grand scheme of things this is a small detail.
 
Have you dim the ParentFormForChild as PUBLIC ? And in a module NOT in a Form Module.
If Yes then show us please your code (where the compiler become angry).
 
Yep, it's public, I also tried making all parts of your code public and it doesn't seem to make a difference.

I'm not sure what the difference between a module and a form module is, but I created it going to the Create tab and selecting Module from the Macro drop-down menu. In the VBA viewer it's under a separate Module folder then the forms, etc.

Here's the code in the subform that is problematic. The bold part is the specific error.

Code:
Public Sub DelClientEventCloseCmd_Click()

DelClientEventCloseCmdMsg = MsgBox("Delete event & client?", vbYesNo + vbDefaultButton1, "Delete Records and Close?")
If (CmdDelClientEventMsg = vbYes) Then
    DoCmd.SetWarnings False
    DoCmd.Close acForm, ParentFormForChild[B].Name[/B]
    DoCmd.SetWarnings True
End If

End Sub

Here's the main form's code:

Code:
Public Sub Form_Activate()
    Set ParentFormForChild.ParentFormForChild = Me
End Sub

And the Module's code:

Code:
Option Compare Database

Option Explicit

Public ParentFormForChild As Form
 
Where I write this ?!?!?!?!?
Set ParentFormForChild.ParentFormForChild = Me

I write only
Code:
Private Sub Form_Activate()
  [COLOR=Blue][B]Set ParentFormForChild = Me[/B][/COLOR]
End Sub
The Form_Activate event is Private, not Public

And turn ON the Option Explicit !!!!!!!!!
 
This exercise is being made complicated.

On the button in the subform, the following line is all that is needed.

Docmd.close acform, Me.Parent.Name

Me.Parent refers to the form holding the particular instance of the subform where the button is clicked, not any other form.
 
Mihail: Oops. Sorry about that; I suppose I should have checked the code more before I posted.

Despite that there were still some problems (probably because I don't know how to turn Option Explicit on). But Cronk's suggestion worked perfectly & is much simpler! Thanks everyone!
 
Cronk's code, work, of course.
But if you take a look to post #6 you will see why I avoid this solution and I have looked for another one.
Cheers !
 
The code in post #5 should work fine at all levels of Form or subform.

Chris.
 
Option Explicit

If this is included at the top of a VBA module, then all variables have to be declared eg Dim str as String.

This is absolute good practice because it means the system picks up typos in code.

To have the Option Explicit automatically included in any _new_ module, go to the VBA screen (press Alt + F11), click on Tools | Options and on the Editor tab, check the box that says Require Variable Declaration. (I'd also suggest changing the Tab width from 4 to 3 but that is my personal preference.)

Not having variable declaration a requirement is bringing a rod to your back needlessly. This can't be emphasised enough.
 
The code in post #5 should work fine at all levels of Form or subform.

Chris.

That seems to work as well. I could have sworn that I had tried that and it came up with the same ".Name" error. If this method doesn't trip that bug that Mihail was talking about then I'll use that.
Option Explicit

If this is included at the top of a VBA module, then all variables have to be declared eg Dim str as String.

This is absolute good practice because it means the system picks up typos in code.

I understand what you are getting at and why it is good practice. Unfortunately I don't know what parts of code constitue variables and what to declare them as. Yeah, I know, not the best situation. But coding & IT isn't really my job; I just happen to know some of it and am using it to try to make my & other's jobs easier here in the office.

Given all of that I'm trying to get this database rolled out & running in a manner that works, then I can take time to learn more and improve the background coding.

I know it might bite me in the butt but I don't really have much of a choice.
 

Users who are viewing this thread

Back
Top Bottom