check if another form is open before closing current form

dark11984

Registered User.
Local time
Tomorrow, 05:21
Joined
Mar 3, 2008
Messages
129
Hi,
When i click a command button to close frmcarriers i want to check if frmcontract is open, if it is open then just close frmcarriers.
If frmcontract is not open then close frmcarriers and open frmswitch. The current code i have to close frmcarriers and open frmswitch is:

Code:
Private Sub CmdSwitchMain_Click()
On Error GoTo Err_CmdSwitchMain_Click
    Dim stDocName As String
    Dim stLinkCriteria As String
    DoCmd.Close
    stDocName = "frmSwitch"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_CmdSwitchMain_Click:
    Exit Sub
Err_CmdSwitchMain_Click:
    MsgBox Err.Description
    Resume Exit_CmdSwitchMain_Click
    
End Sub

I am unsure what to add to make this happen?

Thanks
 
You can test for a form being loaded with:
IsLoaded("formname")
It returns a Boolean.

BTW It is utterly pointless to define a variable, stLinkCriteria, leave it as nothing and then use it in an OpenForm command.

Just forget the pointless variables and use a simple line to open the form:
DoCmd.OpenForm "frmSwitch"
 
You can test for a form being loaded with:
IsLoaded("formname")
It returns a Boolean.
Actually, it is

CurrentProject.AllForms("yourFormNameHereInQuotes").IsLoaded

not just IsLoaded("formname") as that is an Add-in function that was necessary to use with Access 97 and prior (and 2000 prior to SP1 or SP2, can't remember which).
 
You say it returns a boolean, does the code still go on the command button that exits the form or do i have to put it on a boolean then write an if statement? Sorry i am a bit confused.:confused:
 
I believe you want:


Code:
Private Sub CmdSwitchMain_Click()
On Error GoTo Err_CmdSwitchMain_Click
    Dim stDocName As String

If CurrentProject.AllForms("frmContract").IsLoaded Then
   DoCmd.Close acForm, "frmcarriers", acSaveNo
Else
   DoCmd.Close acForm, "frmcarriers", acSaveNo
   DoCmd.OpenForm "frmswitch"
End If

Exit_CmdSwitchMain_Click:
    Exit Sub
Err_CmdSwitchMain_Click:
    MsgBox Err.Description
    Resume Exit_CmdSwitchMain_Click
    
End Sub
 
Thanks very much Bob! I had to make a couple of changes to get it to work. see below.

Code:
Private Sub CmdSwitchMain_Click()
On Error GoTo Err_CmdSwitchMain_Click
    Dim stDocName As String

If CurrentProject.AllForms("frmContract").IsLoaded Then
   DoCmd.Close
Else
   DoCmd.Close
   DoCmd.OpenForm "frmswitch"
End If

Exit_CmdSwitchMain_Click:
    Exit Sub
Err_CmdSwitchMain_Click:
    MsgBox Err.Description
    Resume Exit_CmdSwitchMain_Click
    
End Sub

Cheers!
 
Thanks very much Bob! I had to make a couple of changes to get it to work. see below.

Code:
Private Sub CmdSwitchMain_Click()
On Error GoTo Err_CmdSwitchMain_Click
    Dim stDocName As String

If CurrentProject.AllForms("frmContract").IsLoaded Then
   DoCmd.Close
Else
   DoCmd.Close
   DoCmd.OpenForm "frmswitch"
End If

Exit_CmdSwitchMain_Click:
    Exit Sub
Err_CmdSwitchMain_Click:
    MsgBox Err.Description
    Resume Exit_CmdSwitchMain_Click
    
End Sub

Cheers!
You should not just use DoCmd.Close. You should do it the way I showed, because DoCmd.Close by itself will do funky things if someone makes another object active somehow. It will close the currently active object and not necessarily do what you want. And if you close the current form where the code is on it should be:

DoCmd.Close acForm, Me.Name, acSaveNo

(as written)
 
Is there a way to do this with a sub form?

When you say "sub form" do you really mean a subform (example shown below):
attachment.php
 

Attachments

  • formwithsubform.png
    formwithsubform.png
    36.1 KB · Views: 841
Yeah. Again, this is with the Navigation Form. It doesn't seem to recognize a form if it is not currently open.

I tried it with the subform linked to the home tab of the Navigation Form and it didn't recognize it even when I had it open.
 
The Navigation form is a completely different thing from what I've gathered from others. It doesn't follow the same pattern as other forms so you can't use the standard types of code with it. Personally, I'm starting to wonder if it is so good to use it unless you are not going to need to do things with code to affect the display. Although it may have its own methods, as I mentioned in the other thread I don't have much personal experience with it yet.
 

Users who are viewing this thread

Back
Top Bottom