Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

run-time error

Status
Not open for further replies.

ahau

Programmer
Apr 19, 2006
51
AU
Hi,

I have got a run-time error here. It said as follows
"Execution of this application has stopped due to a run-time error. The application can't continue and will be shut down." That's all there is for the error message.

I'm not sure if it's to do with runcommand thing that i put into the form.

This form has a number of comboboxes that are used for filtering fields for example
Table customer(custID, CustFName, CustLName, ...)

cb1 filters custID, cb2 for CustFName and so on. So that when a user comes along, he can type in "2*" in the cb1 and it lists out all the record starting from 2 on the list_box. The codes here are very simple, no runcommand or stuff like that.

Then, on the bottom of the form, there is a button that does printing. With this, he can select one of the records displayed on the list_box and prints only that record out. The codes that i put in the button print is as follows

DoCmd.SelectObject acReport, stDocName
DoCmd.RunCommand acCmdPrint
If Err.Number <> 0 And Err.Number <> 2501 Then
MsgBox Err.Number & ":" & Err.Description, vbCritical
Else
Err.Clear
End If

how do i get around this problem?

Thank you in advance
 
Have you stepped through the code to determine exactly where the run time error occurs? If it is failing on your call to "DoCmd.RunCommand acCmdPrint", you can replace that with...
Code:
DoCmd.OpenReport stDocName, acViewNormal

Good Luck!
 
You do not have an error trap. Read the help file on "Elements of Run Time Error Trapping"

Routing Execution When an Error Occurs
An error handler specifies what happens within a procedure when an error occurs. For example, you may want the procedure to end if a certain error occurs, or you may want to correct the condition that caused the error and resume execution. The On Error and Resume statements determine how execution proceeds in the event of an error.

The On Error Statement

The On Error statement enables or disables an error-handling routine. If an error-handling routine is enabled, execution passes to the error-handling routine when an error occurs.

There are three forms of the On Error statement: On Error GoTo label, On Error GoTo 0, and On Error Resume Next. The On Error GoTo label statement enables an error-handling routine, beginning with the line on which the statement is found. You should enable the error-handling routine before the first line at which an error could occur. When the error handler is active and an error occurs, execution passes to the line specified by the label argument.

The line specified by the label argument should be the beginning of the error-handling routine. For example, the following procedure specifies that if an error occurs, execution passes to the line labeled Error_MayCauseAnError:

Function MayCauseAnError()
' Enable error handler.
On Error GoTo Error_MayCauseAnError
. ' Include code here that may generate error.
.
.

Error_MayCauseAnError:
. ' Include code here to handle error.
.
.
End Function
The On Error GoTo 0 statement disables error handling within a procedure. It doesn't specify line 0 as the start of the error-handling code, even if the procedure contains a line numbered 0. If there's no On Error GoTo 0 statement in your code, the error handler is automatically disabled when the procedure has run completely. The On Error GoTo 0 statement resets the properties of the Err object, having the same effect as the Clear method of the Err object.

The On Error Resume Next statement ignores the line that causes an error and routes execution to the line following the line that caused the error. Execution isn't interrupted. You can use the On Error Resume Next statement if you want to check the properties of the Err object immediately after a line at which you anticipate an error will occur, and handle the error within the procedure rather than in an error handler.

The Resume Statement

The Resume statement directs execution back to the body of the procedure from within an error-handling routine. You can include a Resume statement within an error-handling routine if you want execution to continue at a particular point in a procedure. However, a Resume statement isn't necessary; you can also end the procedure after the error-handling routine.

There are three forms of the Resume statement. The Resume or Resume 0 statement returns execution to the line at which the error occurred. The Resume Next statement returns execution to the line immediately following the line at which the error occurred. The Resume label statement returns execution to the line specified by the label argument. The label argument must indicate either a line label or a line number.

You typically use the Resume or Resume 0 statement when the user must make a correction. For example, if you prompt the user for the name of a table to open, and the user enters the name of a table that doesn't exist, you can prompt the user again and resume execution with the statement that caused the error.

You use the Resume Next statement when your code corrects for the error within an error handler, and you want to continue execution without rerunning the line that caused the error. You use the Resume label statement when you want to continue execution at another point in the procedure, specified by the label argument. For example, you might want to resume execution at an exit routine, as described in the following section.

Exiting a Procedure

When you include an error-handling routine in a procedure, you should also include an exit routine, so that the error-handling routine will run only if an error occurs. You can specify an exit routine with a line label in the same way that you specify an error-handling routine.

For example, you can add an exit routine to the example in the previous section. If an error doesn't occur, the exit routine runs after the body of the procedure. If an error occurs, then execution passes to the exit routine after the code in the error-handling routine has run. The exit routine contains an Exit statement.

Function MayCauseAnError()
' Enable error handler.
On Error GoTo Error_MayCauseAnError
. ' Include code here that may generate error.
.
.

Exit_MayCauseAnError:
Exit Function

Error_MayCauseAnError:
. ' Include code to handle error.
.
.
' Resume execution with exit routine to exit function.
Resume Exit_MayCauseAnError
End Function
 
To add. Since you have no "On Error" Statement, this code will never get reached if an error does occur.
Code:
 If Err.Number <> 0 And Err.Number <> 2501 Then
     ..
 
Thank you MajP and Rjoubert for your support.

Rjoubert, I can replace the runcommand with docmd.open ..., however it won't allow the user to select a printer they want. In another words, it doesn't bring up the printer dialog windows which is what the user wants.

MajP, yes, you're right i need to know how to use the error handler. That's very helpful information there. However, i'd like to know how to make it run in run-time. Because i'd like to produce that run-time error myself so that i can test it and make it work. The user apparently can only run it in the run-time environment.

So my question is how to make it run in the run-time error because this error never occurs to me. I actually use microsoft office 2003. So i was shocked when they told me they got this error.

Thank you in advance
 
Hi MajP,

I have checked the code once again and i realise that i actually do have the on error statement on the top. Here is the code.

On Error Resume Next

If PS_ID > 0 Then
DoEvents
stDocName = "Plan Summary"
stLinkCriteria = "[PS_ID]=" & PS_ID 'Set Filter

DoCmd.OpenReport stDocName, acViewPreview
DoCmd.Close acReport, stDocName, acSaveNo 'Refresh Report
DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria
DoEvents

DoCmd.SelectObject acReport, stDocName
DoCmd.RunCommand acCmdPrint
If Err.Number <> 0 And Err.Number <> 2501 Then
MsgBox Err.Number & ":" & Err.Description, vbCritical
Else
Err.Clear
End If

It should execute the line of Err.Number... if the error happens. But again, i'm still unsure as what might be the cause. At this time i can only guess.

Thank you in advance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top