views:

257

answers:

3

Hi

can't believe I am losing so much time on this one.

I have an order form, and when I click on a button "reports", a dialog pop ups with a list of different reports to chose from. Double-clicking selects and starts the correspondent report.

On one of these reports, there is an unbound text box I need the user to enter data with. The ControlSource of this field is set to its Name property. When the report is started, an input box appears with an OK and a Cancel button. Whenever I enter some data, all is fine.

But when I click on Cancel, the app crashes and I get an errormessage: "Runtime Error 2501: The Action OpenReport has been canceled" (translated from German).

The Report is called through this code:

DoCmd.OpenReport vBerichtName, nAnsicht
End If   

On Error Resume Next
  DoCmd.Close acForm, "F_BerichtDrucken"
On Error GoTo 0

1) Why does the error handling not kick in?
2) I googled and found lots of weird solutions for this, like the official Microsoft one saying you need to install/update a printer driver (come on...). None helped.

I am doing this for a friend and I normally work on linux/php,java, etc. I apologize if the solution is somewhat obvious or something like that.

+1  A: 

The error probably comes from the DoCmd.OpenReport line. This is why the error handler does not work.

I guess the value you requested is somehow mandatory in the report. Did you try to put your error management line before the docmd.openReport?

Philippe Grondier
+2  A: 

Ditto to Phillipe's answer. you didn't give us the whole procedures but you need to do something like this...

Sub MyButton_Click
On Error Goto myError

DoCmd.OpenReport vBerichtName, nAnsicht


MyExit:
   Exit Sub

MyError:
   If Err.number = 2501 then goto myExit
   msgbox err.description
   goto myExit

End Sub

This is a common error but you can catch it like any other error and ignore it if is 2501.
Seth

Seth Spearman
OK guys, thanks, I got a step further. Now it looks like this:<code>On Error GoTo CancelError If Not IsNull(vFilter) Then DoCmd.OpenReport vBerichtName, nAnsicht, , vFilter Else DoCmd.OpenReport vBerichtName, nAnsicht End IfEcho True CancelError: Exit Function</code>(Hey, I am editing someone else's code and I am quite beginner at VB, please consider ;)Now actually it works, but when I click on cancel, I am back with the dialog - but the app hangs....need to restart Access to be able to continue!
fablife
Sorry for ugly code formatting in comments, need to find out how to do that ;)
fablife
Yup - anytime you use DoCmd.OpenReport you have to trap that error
DJ
A: 

OK, it works now.

After your suggestions, I put the code like this:

    On Error GoTo CancelError 
    If Not IsNull(vFilter) Then 
       DoCmd.OpenReport vBerichtName, nAnsicht, , vFilter 
    Else 
       DoCmd.OpenReport vBerichtName, nAnsicht 
    End If
CancelError:
  DoCmd.Close acReport, vBerichtName
  DoCmd.Close acForm, "F_BerichtDrucken"
  Echo True  ' this did the trick 
  Exit Function

As soon as I put Echo True into the error handling, it works now smoothly, going back to the previous form and allowing to continue to work - looks like "Echo" is kind of a refresher for the screen...?

fablife
yeah...echo is a pain. It has to be used with great care.Seth
Seth Spearman
fablife...one other thing...instead of doing the if block you could do something like this....Docmd.OpenReport vBerichtName, nAnsicht,,NZ(vFilter,vbnNullString)...that will convert the null to '' which will be treated as "show all records"
Seth Spearman
One last thing...ECHO False PREVENTS screen refreshes...so when I say you have to be careful I mean you have to absolutely ensure that Echo is set to True at some point. You should only use Echo if your code or macros are doing a lot of things that will cause the screen to jump and flash etc.
Seth Spearman
It should be DoCmd.Echo. Also, you don't need to test for your filter -- just pass it the variable either way. If there's nothing in it, it won't do anything at all. In general, though, it's customary to pass the filter in a string variable, not a variant. Variants for function parameters should be avoided except where you know you need to pass a Null (such as a function that you call in each row of a query, where the input fields might be Null), or you really do have variable data types. In this case, a string seems preferable to me.
David-W-Fenton