views:

30

answers:

2

I am using Access 2003 and I have a form that collects some filtering criteria for the report. Once the criteria is entered the user clicks and "OK" button and the report is launched using the following VBA.

DoCmd.OpenReport "ReportName", acViewPreview

After the report is opened I issue the following command to close the form that collected the filtering criteria ...

Me.Close

The form closes however the report, which I wanted to stay open in the foreground, is hidden. Any idea why this is happening?

A: 

Does closing the form, then opening the report work? I'm guessing it has to do with the focus being shifted back and forth between the objects.

EDIT

Your code should look something like this:

Private Sub Command0_Click()

    'your code here

    DoCmd.Close acForm, Me.Name

    DoCmd.OpenReport "Report1", acViewPreview

End Sub

Also, there is no native close function as others have pointed out.

Fink
@Fink - Perhaps I am not understanding what you meant. How could I go about closing the form but then opening the report? The input form is where the opening of the report is invoked.
webworm
There's something of a logic problem there and I'd hate to write code that works that way, i.e., closing the form where the code is running before the code stops running. I don't know what Access does in that case, but I'd rather not depend on it doing it right.
David-W-Fenton
+1  A: 

When all else fails with forms and reports coming to the front at the desired time, you can do it explicitly with DoCmd.SelectObject:

  DoCmd.OpenReport "rptMyReport", acViewPreview
  DoCmd.SelectObject acReport, "rptMyReport"
  DoCmd.Close acForm, Me.Name

If that doesn't work, there's something else involved, like forms or reports opened with the acDialog switch, or with forms/reports having the Modal or Popup properties set to True.

Or, there might be a timer running somewhere that's causing something to happen that's grabbing focus.

David-W-Fenton