Close form and open it again

G

Guest

I created a form to manage the reports.
I would like to keep always open.
So I created a button to close but the event that occurs is to put it
visible false.
However I have a problem, when the user closes the form using the X upper
right button.

On close form event, I wrote the code
DoCmd.OpenForm "E1_MenuRpt", , , , , acHidden
But it doesn't open the form.
I know, I can remove the X close button but I didn't want.
Any body knows how to maintain the form always open?
Thanks
jcp
 
G

Guest

If you cancel the form's Unload event, it will not close; however, you will
need some method for actually closing the form. To do this, create a public
funtion in your form. This function has a static varialble. a static
variable retains its value as long as the form is open unless you explicitly
change it.
This function creates a boolean static varialbe. Boolean varialbes
initialize as false.
The argument is a Variant so you don't have to pass a value. The way this
works is if you pass a value, it will change the variables value. It then
returns the current value of the variable.

To use it.
When you want to allow the form to be closed, call the function and pass it
True. Then it can be closed; otherwise, it will go invisible.

If you want to call it from another form:

if CurrentProject.Allforms("E1_MenuRpt").IsLoaded then
forms!E1_MenuRpt.OkToClosereports(true)
End If

If you call it from within the E1_MenuRpt form:
OkToClosereports(true)

Public Function OkToCloseReports(Optional varSetClose As Variant) As Boolean
Static blnCloser As Boolean

If Not IsMissing(varSetClose) Then
blnCloser = varSetClose
End If
OkToCloseReports = blnCloser

End Function


Then test the variable in the form's unload event:

Private Sub Form_Unload(Cancel As Integer)

If Not OkToCloseReports() Then
Cancel = True
Me.Visible = False
End If

End Sub
 
G

Guest

Thanks a lot.
It works very well
jcp
--
Jose


Klatuu said:
If you cancel the form's Unload event, it will not close; however, you will
need some method for actually closing the form. To do this, create a public
funtion in your form. This function has a static varialble. a static
variable retains its value as long as the form is open unless you explicitly
change it.
This function creates a boolean static varialbe. Boolean varialbes
initialize as false.
The argument is a Variant so you don't have to pass a value. The way this
works is if you pass a value, it will change the variables value. It then
returns the current value of the variable.

To use it.
When you want to allow the form to be closed, call the function and pass it
True. Then it can be closed; otherwise, it will go invisible.

If you want to call it from another form:

if CurrentProject.Allforms("E1_MenuRpt").IsLoaded then
forms!E1_MenuRpt.OkToClosereports(true)
End If

If you call it from within the E1_MenuRpt form:
OkToClosereports(true)

Public Function OkToCloseReports(Optional varSetClose As Variant) As Boolean
Static blnCloser As Boolean

If Not IsMissing(varSetClose) Then
blnCloser = varSetClose
End If
OkToCloseReports = blnCloser

End Function


Then test the variable in the form's unload event:

Private Sub Form_Unload(Cancel As Integer)

If Not OkToCloseReports() Then
Cancel = True
Me.Visible = False
End If

End Sub
 
G

Guest

Hi Klatuu,
I have one question more.
Using your code, it works but now if I can open the form in design view, I
can't.
I need to close the database and open it again or from another form, cancel
the event.

Is it possible to add some code in your example to detect if the database
was open with display database window true? If database window is true than
the form can close.

Thanks
jcp
 

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