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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handler resume

Status
Not open for further replies.

balistikb

Technical User
Nov 12, 2002
177
0
0
US
How can I have the error handler resume to a certaine spot of code?
 
The easiest way would be to place a label in your code and the have the error handler continue from the label.

Private sub Example()

on error go to Handler

'Some code

ContinueHere:

'Some Code

Exit sub
Handler:
if err.number =XX then Resume ContinueHere

end sub Thanks and Good Luck!

zemp
 

Example
[tt]
Private Sub YourSub()

On Error GoTo YourSubError

Dim I As Integer

I = 10 / 0 'division by zero error

I = 100000 'overflow error

I = 1

OverFlowMarker:

I = 10

Exit Sub
YourSubError:
DoEvents

If Err.Number = 11 Then Resume Next

If Err.Number = 6 Then GoTo OverFlowMarker

End Sub
[/tt]

walk through this example to under the resume next and the goto lable


I Hope this helps, Good Luck

 
Well I tried the first suggestion but the prompt just goes away and nothing else happens. HEre is the code can you see why?

Private Sub Form_Activate()

On Error GoTo ErrorHandler
ContinueHere:
prompt$ = "Please enter class number."

Do
SearchStr$ = InputBox(prompt$, "CLASS NUMBER")
datClassEval.Recordset.Index = "PrimaryKey"
datClassEval.Recordset.Seek "=", SearchStr$


Loop While datClassEval.Recordset.NoMatch

txtLocator.Text = datClassEval.Recordset![LocNumber]
txtName.Text = datClassEval.Recordset![CLASS_NAME]
txtTrainer.Text = datClassEval.Recordset![REGISTRAR_INSTRUCTOR]
txtDate.Text = datClassEval.Recordset![Date]
txtHours.Text = datClassEval.Recordset![CLASS_HOURS]
ErrorHandler:
If Error = 3421 Then
MsgBox "Please check", vbCritical
Resume ContinueHere

End If

End Sub

 
One thing I see in the code is that you miss Exit Sub before ErrorHandler:
I don't know if that's the reason...
 
Nope that didn't solve the problem. ANy other ideas?
 
I would try two things.

1. In your error handler change 'if error =' to 'if err.number ='.

2. If that doesn't work put the code into its own procedure and call that procedure from the form activate event. Form loads and form activates can sometimes act funny. Thanks and Good Luck!

zemp
 
I am new at VB and I am not quite sure how to do that. Could you help please?
 
Did you try the first change?

To create a stored procedure select tools-add procedure... from the vb menu. Enter a name and select Sub and Private. Then cut and paste everything from the 'On error..' statement to the last 'end if' before the end sub line. Paste it between the two lines of your new stored procedure. Last step go back to the form activate and type in the name of the new procedure you just created. It should be the onlt line that you see.

You should end up with something like you see below.

Private Sub Form_Activate()
ClassNumber
End Sub

Private Sub ClassNumber()

On Error GoTo ErrorHandler
ContinueHere:
prompt$ = "Please enter class number."

Do
SearchStr$ = InputBox(prompt$, "CLASS NUMBER")
datClassEval.Recordset.Index = "PrimaryKey"
datClassEval.Recordset.Seek "=", SearchStr$


Loop While datClassEval.Recordset.NoMatch

txtLocator.Text = datClassEval.Recordset![LocNumber]
txtName.Text = datClassEval.Recordset![CLASS_NAME]
txtTrainer.Text = datClassEval.Recordset![REGISTRAR_INSTRUCTOR]
txtDate.Text = datClassEval.Recordset![Date]
txtHours.Text = datClassEval.Recordset![CLASS_HOURS]
ErrorHandler:
If Error = 3421 Then
MsgBox "Please check", vbCritical
Resume ContinueHere

End If

End Sub
Thanks and Good Luck!

zemp
 
If you only need the code to kick-in when the form loads, then you could also do this:


Public Sub DisplayForm()

me.Show
DoEvents
'Form should be activated now

'Place your code from above here

End Sub

Place the above proceedure somewhere in the Form's code module.

Remove the code from the Form_Activate

When you open your form form the menu or where ever, do not call the load method:

Load Form1

But, instead call the newly created proceedure:

Form1.DisplayForm
[/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Zemp I did exactly what you said and after running it it didn't give me an error, but it also did pop the prompt back up to get the correct information.
 
So if I understand you did not get an error and the program went back to the 'continuehere:' label.

Then everything is working fine.

One more thing, when I copied your procedure into my last post I forgot to change your error handler. I hope you caught and corrected that. Your error handler should be

ErrorHandler:
If Err.Number = 3421 Then
MsgBox "Please check", vbCritical
Resume ContinueHere
End If
Thanks and Good Luck!

zemp
 
Well when the form loads it pops up with a prompt that asks the user for information. If they enter txt istead of numbers then they get the error 3421. Yes you did solve that but the dialog box closes and I need it to open up again to give the user another chance to enter the numbers.
 
Make sure that after you trap the error you are closing the recordset, I would even set it to nothing.

You could also try placing your input box outside of the do loop. Try placing it before the DO line. Thanks and Good Luck!

zemp
 
I just realized something, it is not goveing me the error message that I told it to give me when the error accurs..Could that mean something?
 
Place a msgbox at the end of your error handler to double check what error you are getting.

msgbox err.number & ": " & err.description Thanks and Good Luck!

zemp
 
What I meant to say was that I do have a message box command but it is not reading it/ It doesn't get that far down the lines of command. So it seems I got something in the wrong place or I am missing something.
 
Is everything working as you want iif the user enters a proper value? I am assuming yes.

Post the procedure as you have it so far. Thanks and Good Luck!

zemp
 
Private Sub Form_Activate()

NoMatch

End Sub
__________________________________________________________
Public Sub NoMatch()

On Error GoTo ErrorHandler

prompt$ = "Please enter class number."

Do
SearchStr$ = InputBox(prompt$, "CLASS NUMBER")
datClassEval.Recordset.Index = "PrimaryKey"
datClassEval.Recordset.Seek "=", SearchStr$


Loop While datClassEval.Recordset.NoMatch

txtLocator.Text = datClassEval.Recordset![LocNumber]
txtName.Text = datClassEval.Recordset![CLASS_NAME]
txtTrainer.Text = datClassEval.Recordset![REGISTRAR_INSTRUCTOR]
txtDate.Text = datClassEval.Recordset![Date]
txtHours.Text = datClassEval.Recordset![CLASS_HOURS]
ErrorHandler:
If Error = 3421 Then
MsgBox "Please enter correct value", vbCritical

End If
 
Try this,

Public Sub NoMatch()

On Error GoTo ErrorHandler

prompt$ = "Please enter class number."
SearchStr$ = InputBox(prompt$, "CLASS NUMBER")

Do
datClassEval.Recordset.Index = "PrimaryKey"
datClassEval.Recordset.Seek "=", SearchStr$

Loop While datClassEval.Recordset.NoMatch

txtLocator.Text = datClassEval.Recordset![LocNumber]
txtName.Text = datClassEval.Recordset![CLASS_NAME]
txtTrainer.Text = datClassEval.Recordset![REGISTRAR_INSTRUCTOR]
txtDate.Text = datClassEval.Recordset![Date]
txtHours.Text = datClassEval.Recordset![CLASS_HOURS]
Exit Sub
ErrorHandler:
If Error = 3421 Then
MsgBox "Please enter correct value", vbCritical
else
msgbox err.number & ": " & err.description 'Catch other errors
End If
Thanks and Good Luck!

zemp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top