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

Error Handling 1

Status
Not open for further replies.

shelby55

Technical User
Jun 27, 2003
1,229
CA
Hi

I am using Access 2000.

I have searched this site for all the "error handling" threads. However, I still can't get them to work. For instance, in the OnError event of my form I included:

Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 3314 And IsNull(Me.ChartNo) Then
MsgBox "Chart Number is a required field"
Me.ChartNo.SetFocus
Response = acDataErrContinue
Else
Response = acDataErrDisplay
End If
End Sub

However, it still shows me the Access error and not my message. What am I doing wrong? Also, some of the threads talk about creating an error subroutine. What would code for that look like and how would I call it (I haven't ever used a sub or function!).


Thanks so much!

Shelby
 
Hi!

I restrict my initial test in the forms on error event to only the dataerr. Then if necessary, I check which controls/field validations invoked the handler.

[tt]Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 3314 Then
if IsNull(Me.ChartNo) then
MsgBox "Chart Number is a required field"
Me.ChartNo.SetFocus
Response = acDataErrContinue
end if
Else
Response = acDataErrDisplay
End If
End Sub[/tt]

To do some more errorchecking, try setting a breakpoint in the first executable line (F9), then step thru it line by line (F8), whatch the variables (hover the mouse over them to read the values). It might be other DataErr than you've trepped for (3317 datavalidation, 2113 datatype, 3022 dupes, 3058 null or others). Do a msgbox DataErr for instance to check for the correct number.

Error sub - hmmm - this is trapping form errors, and displaying custom messages. Trapping runtime errors, the easiest way is to study the result from some of the wizard created stuff, on error goto <label> at the top and the exit and err routines at the bottom.

Roy-Vidar
 
You are tremendous, Roy-Vidar!! Thanks so much - I learn so much from you even if I'm just reading your replies to others but especially when you reply to me! Thanks so much for sharing your time and expertise!

Have a great day!

Shelby
 
Hi

I know I'm probably pushing my luck but I have encountered yet another problem that I hope you can help with (and then I won't bug you for awhile!!): I have a checkbox that opens up a subform. However, the form needs to be requeried to create a record for the "subrecord" has a parent record. The catch is that a required field on the second page of the tab control won't be filled in yet (because it's on page 2!) so requery sends the error messages flying! Any ideas on how to solve this?

Thanks in advance!!

Shelby
 
Thanx for the star and the kind words!

I don't design my forms that way;-)

Could a design change do the trick, place the control on the first tab?

Else, I'm a great fan of using the forms before update event on bound forms. I triggers whenever a save operation is invoked. The event can be cancelled. There you could test for contents in the actual control, for instance something like this:

[tt]if isnull(Me!SomeControl.Value) then
msgbox "appropriate message"
Me!SomeControl.SetFocus
Cancel=True
end if[/tt]

- would that do?

Roy-Vidar
 
I think you're right - I'll go for a design change!! I don't usually design forms this way either but the client didn't want this particular dataset showing up all the time, only if the checkbox was ticked.


Thanks again for all your help!!

Shelby
 
Roy-Vidar,

Is there a way to modify the first set of code to include multiple fields. I have a form bound to a table which has four required fields. FirstName, LastName, Acct, SubAcct.
 
Several ways, here are two suggestions.

Combine the tests:

[tt]Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 3314 Then
if IsNull(Me!FirstControl) or _
IsNull(Me!SecondControl) or _
IsNull(Me!ThirdControl) or _
IsNull(Me!FourthControl) then
MsgBox "One or more required controls..."
Me!FirstControl.SetFocus
Response = acDataErrContinue
end if
Else
Response = acDataErrDisplay
End If
End Sub[/tt]

Use a select case:

[tt]Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 3314 Then
select case true
case isnull(Me!FirstControl)
MsgBox "Firstcontrol..."
Me!FirstControl.SetFocus
Response = acDataErrContinue
case isnull(Me!SecondControl)
MsgBox "Secondcontrol..."
Me!SecondControl.SetFocus
Response = acDataErrContinue
' ... rest of the tests
end select
Else
Response = acDataErrDisplay
End If
End Sub[/tt]

Remember also to rename the controls of the form so that they don't have the same name as the fields they are bound to (txtLastName...)

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top