Open/Close Forms using buttons and If/Then

G

Guest

I have 4 forms, one is the main form, two are forms that open on occasion,
and the last is a navigation form with buttons such as "New Job", "Print
Job", "Open Customers Form", etc.

I am trying to set up the "Job Work Orders Form" button to close either of
the other two forms IF they are open. How do I set up my If/Then statement
to do this?

I got it to work, but if the forms aren't open, then I get an error.

Private Sub Open_Job_Work_Orders_Form_Click()
On Error GoTo Err_Job_Work_Orders_Form_Click

Forms("Customers Form").SetFocus
DoCmd.Close

Forms("Insurance Companies Form").SetFocus
DoCmd.Close

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Job Work Orders Form"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Job_Work_Orders_Form_Click:
Exit Sub

Err_Job_Work_Orders_Form_Click:
MsgBox Err.Description
Resume Exit_Job_Work_Orders_Form_Click

End Sub
 
G

Guest

Add the IsLoded function to your mdb, if you don't aleady have it
==========================
Function IsLoaded(ByVal strFormName As String) As Boolean
' Returns True if the specified form is open in Form view or Datasheet view.

Const conObjStateClosed = 0
Const conDesignView = 0

If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <>
conObjStateClosed Then
If Forms(strFormName).CurrentView <> conDesignView Then
IsLoaded = True
End If
End If

End Function
=============================
Add it to a module

Then create the If criteria using the IsLoaded function
========================
Private Sub Open_Job_Work_Orders_Form_Click()
On Error GoTo Err_Job_Work_Orders_Form_Click

If IsLoaded ("Customers Form") Then
DoCmd.Close acForm, "Customers Form"
End If
If IsLoaded ("Insurance Companies Form") Then
DoCmd.Close acForm, "Insurance Companies Form"
End If

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Job Work Orders Form"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Job_Work_Orders_Form_Click:
Exit Sub

Err_Job_Work_Orders_Form_Click:
MsgBox Err.Description
Resume Exit_Job_Work_Orders_Form_Click

End Sub
 
G

Guest

Perfect! You ROCK dude, thanks!

Ofer Cohen said:
Add the IsLoded function to your mdb, if you don't aleady have it
==========================
Function IsLoaded(ByVal strFormName As String) As Boolean
' Returns True if the specified form is open in Form view or Datasheet view.

Const conObjStateClosed = 0
Const conDesignView = 0

If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <>
conObjStateClosed Then
If Forms(strFormName).CurrentView <> conDesignView Then
IsLoaded = True
End If
End If

End Function
=============================
Add it to a module

Then create the If criteria using the IsLoaded function
========================
Private Sub Open_Job_Work_Orders_Form_Click()
On Error GoTo Err_Job_Work_Orders_Form_Click

If IsLoaded ("Customers Form") Then
DoCmd.Close acForm, "Customers Form"
End If
If IsLoaded ("Insurance Companies Form") Then
DoCmd.Close acForm, "Insurance Companies Form"
End If

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Job Work Orders Form"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Job_Work_Orders_Form_Click:
Exit Sub

Err_Job_Work_Orders_Form_Click:
MsgBox Err.Description
Resume Exit_Job_Work_Orders_Form_Click

End Sub
 
E

eos

AUTO-REPLY From George Levitt

Please allow this to confirm a system receipt of your e-mail.

I am out of the office until Wednesday morning (1/12/05) and will not be
reviewing or responding to email or voicemail until that time.

I look forward to replying to your message on Wednesday.

Thanks and warmest regards, George
 
E

eos

AUTO-REPLY From George Levitt

Please allow this to confirm a system receipt of your e-mail.

I am out of the office until Wednesday morning (1/12/05) and will not be
reviewing or responding to email or voicemail until that time.

I look forward to replying to your message on Wednesday.

Thanks and warmest regards, George
 
E

eos

AUTO-REPLY From George Levitt

Please allow this to confirm a system receipt of your e-mail.

I am out of the office until Wednesday morning (1/12/05) and will not be
reviewing or responding to email or voicemail until that time.

I look forward to replying to your message on Wednesday.

Thanks and warmest regards, George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top