DoCmd.Close acReport not working (1 Viewer)

Ingeneeus

Registered User.
Local time
Yesterday, 23:09
Joined
Jul 29, 2011
Messages
89
A little background (because who doesn't love a story?):

I've got an Access-based application -- not built by me -- that our company uses to run invoices, keep track of inventory, generate purchase orders, etc.

Said application also allows us to run various reports, with fixed parameters. I was asked to create a report that would take variable parameters, which could be entered into a form. So far, so good.

My boss wanted to be able to call up this form from the "Reports" window. Not really how that works (since the Reports window has two options: Preview and Print the reports in the menu), but I came up with a work around.

I created a blank report, which has a DoCmd.OpenForm statement that calls up my data entry form in the OnOpen event. Works exactly as planned.

Except . . . my blank report, which is opening in acPreview mode, stays open. My boss has to manually close it by hitting the little red Windows "X." I've been trying to figure out how to close the blank report after my form is launched, but I'm missing something. If I place a DoCmd.Close acReport immediately after the DoCmd.OpenForm, I get an error that basically says I can't do that. I thought I had my solution by placing my DoCmd.Close acReport statement in the OnActivate event of the report (since that would happen after the OnOpen event), but that doesn't appear to work. The blank report stays open in acPreview mode. I'm kind of thinking the newly-opened form is keeping my report from getting to the OnActivate stage, but I don't know where to go with that.

Any thoughts on this?
 

MarkK

bit cruncher
Local time
Yesterday, 23:09
Joined
Mar 17, 2004
Messages
8,194
DoCmd.Close takes a three parameters, not just the single ObjectType parameter, acReport. You can specify the name of the report you want to close, for instance, and that might make a difference.
 

Ingeneeus

Registered User.
Local time
Yesterday, 23:09
Joined
Jul 29, 2011
Messages
89
Hi, MarkK -- thanks for your prompt response.
I actually did use the full parameter: DoCmd.close acReport "Customer Past Due", acSaveNo; I just abbreveiated it in my post because I was in a hurry to get out of the office last night :)
I'm afraid it's got to be something else.
 

MarkK

bit cruncher
Local time
Yesterday, 23:09
Joined
Mar 17, 2004
Messages
8,194
Maybe post the exact code and error messages.
 

Ingeneeus

Registered User.
Local time
Yesterday, 23:09
Joined
Jul 29, 2011
Messages
89
Hi, MarkK -- sorry for the delayed reply; I've been unable to hit this site for better than a day. Hope you're still there!

Here's what I've tried so far:

First, I tried to put the close command right after the OpenForm command in the Reports OnOpen event:
Code:
Private Sub Report_Open(Cancel As Integer)
DoCmd.OpenForm "CustPastDue"
DoCmd.Close acReport, "CustomerStatement Past Due Launch", acSaveNo
End Sub
. . . which produced Run-time error '2585'
This action can't be carried out while processing a form or report event.


That's when I got the idea to put the close command into the report's OnActivate event, on the theory that since that event sequences after the OnOpen, it should have proceeded Open Report ==> Open Form ==> Close report.
Code:
Private Sub Report_Activate()
DoCmd.Close acReport, "CustomerStatement Past Due Launch", acSaveNo
End Sub
This does exactly nothing, as far as I can tell. The report remains open in Preview mode.

Then I tried putting the close command in the opening event for the form
Code:
Private Sub Form_Open(Cancel As Integer)
Me.TxtCustID_1.SetFocus
DoCmd.Close acReport, "CustomerStatement Past Due Launch", acSaveNo
End Sub
. . . which also got me the Run-time error '2585' message

I even tried moving the close command to the report's OnNoData event (on the theory that there is no data in the blank report), but this doesn't do anything either.
It seems like this should be pretty straightforward, but I can't figure it out. I have a feeling it's something utterly simple that I'm missing.
 
Last edited:

MarkK

bit cruncher
Local time
Yesterday, 23:09
Joined
Mar 17, 2004
Messages
8,194
When you post this code . . .
Code:
Private Sub Report_Open(Cancel As Integer)
   DoCmd.OpenForm "CustPastDue"
   DoCmd.Close acReport, "CustomerStatement Past Due Launch", acSaveNo
End Sub
. . . what is the name of the report that is opening here?
 

Ingeneeus

Registered User.
Local time
Yesterday, 23:09
Joined
Jul 29, 2011
Messages
89
Hi again, MarkK; glad you're still with me --

The report that is opening is CustomerStatement Past Due Launch, which is also the report I'm trying to close, after I use the report's OnOpen event to to launch my CustPastDue form, which in turn allows me to input parameters into the report I really want to run.

It's a little convoluted, I know. It's the result of the workaround I described in my initial post. CustomerStatement Past Due is just a blank report; it's not bound to anything, it has no textboxes or labels, it doesn't actually report anything. It's only purpose is to launch my form. I just want it to go away once the form is launched, without having to close it manually.
 

MarkK

bit cruncher
Local time
Yesterday, 23:09
Joined
Mar 17, 2004
Messages
8,194
It's a little convoluted, I know.
See the Cancel parameter in the Report_Open() method's signature? Set that to True. See if that works. It may raise an error in calling code that you have to handle.

But what you might also consider is open the form from the Report_Open() event handler of your actual report, but open it as a modal dialog, which pauses the running code in the calling object. So consider this . . .
Code:
Private Sub Report_Open(cancel as integer)
[COLOR="Green"]'   This is your actual report opening[/COLOR]
    Const FN as string = "fReportFilter"
[COLOR="Green"]    'open form using acDialog switch, which pauses code execution here[/COLOR]
    docmd.OpenForm FN, , , , , acDialog
[COLOR="Green"]    'code stays paused while user interacts with 'fReportFilter' creating the config for this report
    'and when you hide that form, code execution continues here
    'so we read some value from that form . . .[/COLOR]
    Me.Recordsource = Forms(FN).FinalRecordSource
[COLOR="Green"]    'and close it here[/COLOR]
    DoCmd.Close acForm, FN
End Sub

See what's happening there? This way the config step for the report is built in to the report, so it doesn't matter how you open it, and it shows in the reports list, and so on. You just have to make sure the user doesn't close the config form opened as acDialog. They can only hide it so your calling code can still use it.

Cheers,
 

Ingeneeus

Registered User.
Local time
Yesterday, 23:09
Joined
Jul 29, 2011
Messages
89
So, my code would look something like:
Code:
Private Sub Report_Open (cancel as integer)
DoCmd.OpenForm "CustPastDue"
Cancel=True
End Sub
If I try to go your 1st option? Or would I still need the DoCmd.Close acReport statement in there as well?
(I'll need to do more reading before I can think about your second one -- that's a little over my head, I'm afraid)
 

MarkK

bit cruncher
Local time
Yesterday, 23:09
Joined
Mar 17, 2004
Messages
8,194
Or would I still need the DoCmd.Close acReport statement in there as well?
Quicker for you to try it than to ask me. Let us know which one worked, if any. If not, we'll keep at it. :)
 

Ingeneeus

Registered User.
Local time
Yesterday, 23:09
Joined
Jul 29, 2011
Messages
89
Thanks, MarkK --

I will give this a try when I get back to my workstation this afternoon. I'll let you know how it goes.
 

MarkK

bit cruncher
Local time
Yesterday, 23:09
Joined
Mar 17, 2004
Messages
8,194
Sorry, I just assume everyone is sitting at their development machine :eek: . . .
Code:
Private Sub Report_Open(Cancel As Integer)
    DoCmd.OpenForm "CustPastDue"
    Cancel = True  [COLOR="Green"]'this cancels the Open event, has the effect of suppressing the form from opening[/COLOR]
End Sub
I would expect that to work, but it's likely to raise an error in the calling code, which you'll need to handle.
 

Ingeneeus

Registered User.
Local time
Yesterday, 23:09
Joined
Jul 29, 2011
Messages
89
Huzzah!
It works perfectly!

Thank you, MarkK!

You have not only made my day better, but taught me something as well (and any day you can learn something without losing a finger or toe is a good day :D)!
 

MarkK

bit cruncher
Local time
Yesterday, 23:09
Joined
Mar 17, 2004
Messages
8,194
and any day you can learn something without losing a finger or toe is a good day :D
Lol, yeah, that's why I do programming! Compiler errors are not life threatening!
Cheers,
 

Users who are viewing this thread

Top Bottom