Error Trap

Lyncroft

QPR for ever
Local time
Today, 13:34
Joined
May 18, 2002
Messages
168
I use this code to trap a duplicate entry on a form. I realise I could simply copy and paste this to all the other forms and amend the message box. Am I right in thinking that I could put this into a module and then call it from the various forms or am I way off track here. If I could any suggestions on how to construct the code.


Const RecordAlreadyExists = 3022

If dataerr = RecordAlreadyExists Then
MsgBox "You have already inserted that training course." & vbCrLf _
& " Please try again.", vbCritical
Response = acDataErrAdded
Me.Undo
End If
End Sub
 
Hi

Yes this is something i do to trap errors in general.

Here is an example sub place this on a button. When you click the cancel option on the input box it passes a null to the variable which generates an error. This is passed to the function which generates the msgbox. As you will see the msg is not pretty. What i've tried in the past is to build a library of error codes in a module each with a more suitable message. The only problem with this is you may have a more specific msg you wish to use, as the same generated error will mean different things in different databases.

Hope this helps

Chris

Private Sub CMD_Weight_Convert_Click()
Dim IntInputStone As Integer
Dim IntInputlbs As Integer

On Error GoTo notint
' create input box
IntInputStone = Nz(InputBox("Please first part of weight in stone", "Data Request"), 0)
' create input box
IntInputlbs = Nz(InputBox("Please second part of weight in pounds", "Data Request"), 0)


msgbox = "Your weight is " & ((IntInputStone * 14) + IntInputlbs) / 2.2 & "KG get up and do some exersize!",vbexclamation,"Fat lump alert"

Exit Sub

notint:
' call function
errtrap Err
end sub

place this in a module

Public Function errtrap(errcode As ErrObject)
dim msg as string
' you could say
' select case errcode.number
' case = 13
' msg = "Please don't do that it hurts"
' end select
' or

MsgBox Err.Description
Err.Clear
End Function
 
Cheers BJ. Very helpful - thats the sort of thing I thought you should be able to do. Just have to sit down and have a good look at it now!

Cheers again.
 
That's ok glad to help.

PS its CJ, i don't want anyone getting the wrong ideas !!!


Chris
 
Whoops! Bloody hell! See what you mean. What was I thinking of?
 

Users who are viewing this thread

Back
Top Bottom