Open/Close form with same cmd button

steve1111

Registered User.
Local time
Today, 18:44
Joined
Jul 9, 2013
Messages
170
I am tying to create a cmd btn called "Report" that when clicked opens a form [quick report] in dialog window. I have that done easy enough. But what I want is if that form [quick report] is open, and you click the same button "Report" for it to then close the form. Is this possible?

I am still learning code so i am not that advanced at writing VBA, was trying to do in the macro wizard. Any tips are appreciated.
 
I don't think you'll get there with wizards. Look in VBA help at IsLoaded. You can use that to test if the form is open already, and open/close it based on the result (using an If/Then/Else block).
 
There are a couple of ways to do this one is to have a public boolean values which switches between true and false on each click of the button but the safer way is to check if the report form is open and if it is then close it and if not, open it

Code:
Private Sub ReportButton_Click()
Dim s As String
On Error GoTo OpenRpt
    s = Application.Reports("myReport").Tag ' if form is not open, will generate an error, otherwise close the report
    DoCmd.Close acReport, "myReport"
    Exit Sub
 
OpenRpt:
    DoCmd.OpenReport "myReport"
 
End Sub
 
Thank you so much CJ, I trying to become more familiar with what the code is saying to do, so i appreciate the exposure, works great!
 

Users who are viewing this thread

Back
Top Bottom