Runtime error 2501

S

Saj

Hi all

Need a little help with the below if possible:

I have a cmd button that when clicked runs the following code:

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "StockAvailability"
DoCmd.OpenForm stDocName, , , stLinkCriteria

This works fine... a parameter box first appears I then enter a stock no and
it then opens the form showing me the details for that stock item.
BUT
When I dont enter anything in the parameter box or even if I do enter
something and then click on cancel... it brings up a runtime error 2501?

Not sure why it does this and dont know how to fix or what to fix...

PLEASE HELP!!!

Thanks in advance
 
D

Dennis

IIRC, that means that there's some error in your opening form, which causes
it to "cancel" the open event. I single-step through my code for issues like
this, and usually find by screwup pretty quickly.
 
S

Saj

hmm... I have stepped through the code but dont see where its happening..

onclicking the cmdstockAvail button it goes through the code:

1: Private Sub cmdStockAvail_Click()
2: On Error GoTo Err_cmdStockAvail_Click

3: Dim stDocName As String
4: Dim stLinkCriteria As String

5: stDocName = "StockAvailability"
6: DoCmd.OpenForm stDocName, , , stLinkCriteria

7: Exit_cmdStockAvail_Click:
8: Exit Sub

9: Err_cmdStockAvail_Click:
10: MsgBox Err.Description
11: Resume Exit_cmdStockAvail_Click

12: End Sub

It runs up to line 6 then parameter box kicks in.. I then click cancel on
parameter box and then it brings up the error from line 10 saying openform
canceled. end the sub.

So not sure what is happening but know its to do with line 6 as telling it
to open something but then I cancel so it goes to error... ?

Any further help would be appreciated

Thanks again in advance
 
L

Larry Koehler

Add the following line:
"On Error Resume Next"
just above the stDocName = "StockAvailability" line. This will stop the
error if you interrupt the DoCommand command with a cancel.
 
V

Van T. Dinh

You might have some code in the Open Event of the Form "StockAvailability"
that cancels its opening on some condition, e.g. empty RecordSource.

If that the behaviour you want, you need to give the user some message about
the empty RecordSource and use the error-trapping code to trap and ignore
error number 2501.
 
S

Saj

Thanks for getting back to me... but Larry's answer worked.

I did check my StockAvailability form though, but nothing there... thanks
anyway.. much appreciated ;o)

Saj
 
Top