Error handling on Form

  • Thread starter Thread starter NP
  • Start date Start date
N

NP

Generally there are certian situation when the code is
perfect but due to some reason like user not selecting the
proper criteria or option on Form, the code show error and
show the dailog to Debug or end or help etc.

To avoid the dailog which appears, the programmer writes
Error handling Code. As I am not very good in VB coding, I
tried at some places like below, just before End Sub line:

On Error Exit

I also tried : On Error MsgBox "Not Correct Choice"

But nothing worked. My requirement is a small peice of
code, that on error, it will just cancel the event, so
that it will not display any error dailog box etc. , and
this code I will put where ever it may be necessary just
above the End Sub Line

Can any please advise me
 
Each procedure needs its own error handing sections.

You can call a generic error handler from there. As described in this
article, you can decide whether to show the user to the error or not.
http://members.iinet.net.au/~allenbrowne/ser-23a.html

Not all events can be cancelled. For example, BeforeUpdate can (in which
case you are stuck there until you supply a valid value), but AfterUpdate
cannot.
 
Attn: Mr. Allen Browne

Sir,
I took your said Website tips print out, but regret to say
that I was unable to make out, how to get benifit of it,
as my VB is very poor. Therefore, I have written the
complete code used on the click event of Label for your
check up and advise:
Situation is the same as mentioned, that when a user
select the Option of the Frame after opening the Form, the
code works normal and if user does not select Frame and
click directly on the label, error dailog appears. I
repeat my request that i dont want the user to View the
Error dailog. If error, then nothing to be shown, if all
ok, then the code should run normal:


Private Sub Label18_Click()

Dim strField As String

Select Case Me!Frame0
Case 1
strField = "CoName"
Case 2
strField = "ContPerson"
Case 3
strField = "PostBox"
End Select

With Me!Child1.Form
.Filter = strField & " Like '*" & .Parent!Text13 & "*'"
.FilterOn = True
End With
End Sub



Please advise.
 
If all you want to do is avoid the error message, you can just add this line
to the top of the procedure:
On Error Resume Next

However, that will *stop* you from discovering what is wrong with your code,
and therefore it stops you from fixing it properly.

What is the error message? What error number? Which line produces the error?

The most obvious thing is that you have no "Case Else", so if Frame0 does
not match any of the cases, strField is not assigned. You could fix that in
several ways:
- Add a case Else.
- Test Len(strField) before you tryto apply the filter.
- In form Design view, set the Default Value of Frame0 to 1, so it has a
value when opened.

Private Sub Label18_Click()
On Error Goto Err_Handler
Dim strField As String
Dim bCancel As Boolean

Select Case Me!Frame0
Case 1
strField = "CoName"
Case 2
strField = "ContPerson"
Case 3
strField = "PostBox"
Case Else
bCancel = True
End Select

If Not bCancel Then
With Me!Child1.Form
.Filter = strField & " Like '*" & .Parent!Text13 & "*'"
.FilterOn = True
End With
End If

Exit_Handler:
Exit Sub

Err_Handler:
If Err.Number <> 9999 Then
MsgBox "You just generated Error " & Err.Number & _
". Change the 9999 to this number if you do not wish to see this
message."
End If
Resume Exit_Handler
End Sub
 
Back
Top