Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS Access - "phantom" process created by canceling report

Tags:

vba

ms-access

I have encountered a problem in Access 2010 for Windows:

  1. Launch a report using doCmd.OpenReport
  2. Set "Cancel = True" in an event associated with the report
  3. Close Access
  4. Access continues to run as a "phantom" process and must be killed in Task Manager. Access cannot open another database until this process is killed. The task initially shows some CPU utilization but quiets down to 0%.

I discovered this while using doCmd.OpenReport to launch a report. The report opens a form in the Report_Open event. The form prompts the user for report parameters and allows them to press "OK" to display the report or press "Cancel". The On Click event for "Cancel" sets "Cancel = True".

It appears that OpenReport() is not responding gracefully to the cancel. This seems like a frequently used technique so I hesitate to call it a bug and am wondering if I am doing something wrong. Actually... the more I explain this, it does sound like a bug. At this point, I am hoping somebody has a workaround or that I am missing something obvious.

This is just a simplified example I created to illustrate the problem:

form

Private Sub Form_Open(Cancel As Integer)
On Error GoTo errHandler

DoCmd.OpenReport "Test Report", acViewPreview, , , acDialog

Exit Sub

errHandler:
Select Case Err.Number
Case 2501   ' Cancelled by user, or by NoData event.
    MsgBox "Report cancelled, or no matching data.", vbInformation, "Information"
Case Else
    MsgBox "Error " & Err & ": " & Error$, vbInformation, "Form_Open()"
End Select
Resume Next

End Sub

report

Private Sub Report_Open(Cancel As Integer)
    Cancel = True
End Sub
like image 359
user1882222 Avatar asked Dec 31 '12 23:12

user1882222


1 Answers

The reason for the problem in this case is acDialog:

 DoCmd.OpenReport "Test Report", acViewPreview, , , acDialog

I think you will find:

 DoCmd.OpenReport "Test Report", acViewPreview

Works without problems.

Edit re Comment

You should not need to cancel the report. For the most part, it is better to avoid errors than to trap them, so check for data and get parameters before opening the form. Move the form from the report open event to step of its own and use DLookUp or a query to check for data before launching the form.

like image 51
Fionnuala Avatar answered Sep 23 '22 23:09

Fionnuala