View Full Version : Error Handling


Chipcom
05-26-2007, 07:34 AM
Hi

I need to Change the error messege to other text.
I use command that generates a new record and when I type a duplicate record I get the error :
Run-time error '2105':
You can't go to the specified record.
So I need to change this error text.
Can you please show me how?


Thanks

boblarson
05-26-2007, 07:44 AM
Create an error handler:

In the code that you have, at the top put:


On Error Go To err_handler


And at the bottom of your code, just after the end of your code but before the End Sub, put



Exit Sub

err_handler:
If Err.Number = 2105 Then
MsgBox "Put Your Message Here", vbExclamation, "Error"'
Resume Next
Else
MsgBox Err.Description, "vbExclamation", "Error Number " & Err.Number
Resume Next
End If

Chipcom
05-26-2007, 08:38 AM
Hi

There is a problem , The program is generate the new record and not exits from the sub.


I need that when it comes to :
DoCmd.GoToRecord acDataForm, "Test", acNewRec

so if there is an error in this line it will show the error message and won't run this command but it will exit the sub

boblarson
05-26-2007, 08:51 AM
Just put it in the event (the red is just for reference to show you where, but use the code between the red):


Private Sub Whatever_Click()

On Error Go To err_handler

DoCmd.GoToRecord acDataForm, "Test", acNewRec

Exit Sub

err_handler:
If Err.Number = 2105 Then
MsgBox "Put Your Message Here", vbExclamation, "Error"'
Resume Next
Else
MsgBox Err.Description, "vbExclamation", "Error Number " & Err.Number
Resume Next
End If
End Sub

Chipcom
05-26-2007, 10:05 AM
HI Bob

Your code is works But..............

If you try to debug the code you will see that line:
DoCmd.GoToRecord acDataForm, "Test", acNewRec
runs first then it shows the error message and what I need is to stop that line from generates a new record - It is try to save the record :
I use a custom autonumber in the beforeupdate event and it increments even if I got the customized error message and I need to stop the customized autonumber in the beforeupdate event.

boblarson
05-26-2007, 10:43 AM
It would help, then, if you could post ALL of the APPLICABLE code. Also, it is good to share the WHOLE problem. For every post I answered exactly how it would work for what you asked. I think you need to remember that we can't read minds so you need to state the entire problem. But, if you aren't that experienced with Access I can understand that you wouldn't necessarily know what to ask. But, just a suggestion for the future, eh? :)

Chipcom
06-01-2007, 01:14 AM
Hi Bob

THis is the code:

When the user Press Enter Access opens a new record so it calls to beforeupdate sub and the same record is already found in the database it give customizes 2105 error and it have to stop calling the Next_Custom_Counter() .

The problem is that I don't know how to implement this.



Public Sub Form_KeyPress(KeyAscii As Integer)
Dim Erro As Integer
'13=Carriage Return
If KeyAscii = 13 Then
On Error GoTo err_handler
DoCmd.GoToRecord acDataForm, "TestTable", acNewRec
Exit Sub
err_handler:
If Err.Number = 2105 Then
ErrCheck = -1
MsgBox "Duplicate Record", vbExclamation, "ERROR!!!"

Resume Next
Else
Resume Next
End If
End If
End Sub


Private Sub Form_BeforeUpdate(Cancel As Integer)
'Calls to Autonumber function
'Calls Next_Custom_Counter

ErrCheck = Form_KeyPress(k)
If ErrCheck = 2105 Then
Exit Sub
ErrCheck = 0
Else
Me.Line.DefaultValue = Chr$(34) & Next_Custom_Counter() & Chr$(34)
End If
End Dub


Thanks