Full Version: Command to run referenced query for report, then close form
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
djleister
I have a form where the user selects a project from a combo box, once selected the detail is displayed within the form. I have a button which allows for the user to print preview the selected report howver when selected the report open behind the form so I used this command:
ECHO False
DoCmd.Close
DoCmd.Maximize
DoCmd.OpenReport "rptEmployees", acViewPreview
ECHO True

The problem is that the form closes but the reference to the combo box of which project to display did not have time to query and run, so I get the message box to type in the project number. How do I have the report run, display for the referenced project and the form to close all in one command?

Thank you,
Deborah
HiTechCoach
In the form try using this:

CODE
Me.Visible = False    ' hide form before reporting the report

DoCmd.OpenReport "rptEmployees", acViewPreview


In the form's On Open event, I you could use something like:

CODE
If CurrentProject.AllForms("frmPopReportParms").IsLoaded = False Then

  MsgBox "Can't run this report."

  Cancel = true

End if


Edited by: HiTechCoach on Sat Sep 20 1:08:06 EDT 2008.
djleister
I have a command button which allows the user to print preview the report for the slected project, do I code on the onclick of the button?
HiTechCoach
Yes. I assumed that this was the code for the click event.

TIP: when posting VBA code, include all the code for the event

Example
CODE
Private Sub cmdPreviewReport_Click()

  Me.Visible = false

  DoCmd.OpenReport "rptEmployees", acViewPreview



End Sub
polant
Deborah

Not sure if I fully understand the problem, however I will make some comments that may help.

- If the report opens behind the form, then probably this is because the PopUp and/or Modal properties of the form are set to Yes.

- When I want to close a form but hold on to the value of a control for a little longer, then I assign the value of control to a variable before the docmd.close statement. The variable exists until the sub finishes. So it goes about like this:

variable = me!control

docmd.close

use variable to do whatever is to be done

end sub
djleister
Again, thank you! It works great.

Deborah
djleister
Thanks Polant, This information helps me too.
HiTechCoach
You're welcome! sad.gif

Glad we could assist again.

uarulez2.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.