Error Messages

SteveC24

Registered User.
Local time
Today, 21:20
Joined
Feb 1, 2003
Messages
444
Error Messages - GO AWAY!

Hi,

I would like my database to simply display my custom error message, and preferably the error details below, or in another box or something, then just stop....no go on about Halting macros and all this caper!

Can anyone help get me out of my quandry of errors!?!

Thanks
 
Okay, I have searched around a bit, and I think I need to error trap.......HELP!

I have been at this database for a long time, but have no idea how to error trap, can someone tell me how....I have little or no knowledge of writing VB code.

Thanks!
 
Use the:

On Error GoTo errortrap

before your code begins, then somewhere in the procedure

errortrap:
Select Case Err.Number
Case 'an error number that needs attention'

Case Else 'display default error message'
MsgBox Err.Description & " " & Err.Number
Resume procedure_exit 'stop'
end select

procedure_exit:
exit sub

hope this helps
 
Hi,

I tried to implement that code, but it didn't seem to like it....but as I say, I don't know VB at all, so I probably did something wrong!
 
Here is an example of using error trapping and also trapping for two specific errors.
Code:
Private Sub bDeleteRecord_Click()
On Error GoTo bDeleteRecord_Click_Err
    
    Beep
    If MsgBox("Delete current record?", vbQuestion + vbYesNo) = vbYes Then
        DoCmd.RunCommand acCmdDeleteRecord
    End If
    
bDeleteRecord_Click_Exit:
    Exit Sub
    
bDeleteRecord_Click_Err:
    If Err = 2046 Then 'The command or action 'DeleteRecord' isn't available now
        Exit Sub
    ElseIf Err = 2501 Then 'The RunCommand action was canceled
        Exit Sub
    Else
        MsgBox Err.Number & " - " & Err.Description
        Resume bDeleteRecord_Click_Exit
    End If
    
End Sub
HTH
 
Something to bear in mind is that you cannot trap errors in macros.
If you wish to trap these errors you will have to convert your macros to VB.

Just to explain a little bit further the concept of Error handling;
When an Error occurs the 'Err' object is called.
The Err object contains information about the last Error that occured.
Each Error has a unique Error number
To check what the Error is you check the Err object's Error Number & Error Description
Code:
Err.Number                  'Error Number
Err.Description             'Error Description
You decide what action to take based upon the Error Number.
I understand that this can be daunting if you don't have VBA knowledge.
If you post more detail about the errors & where they occur I'll write a handler for you.

Regards,
Patrick.
 
Thanks loads for all your help!

GHudson, I tried your code, and it worked great...I am pretty sure I can adapt this for the other buttons at least.

My main problem is that i have several (about 10) forms, all with identical buttons on. Is there a way to make them all get their code from the same place? So I don't have to enter it everytime.

Also, is there some kind of reference for those error message numbers?

Could I ask someone to tell me what the VB is for the nav buttons (they are listed below), as I am currently using macros.

For the moment at least my problems are with standard nav buttons (first, previous, new, delete, next, last), an error about not having a field in another table that it needs, and not being able to find the backend.

Thanks for any help - trust me - I am VERY grateful!
 
Last edited:
Testing, testing and more testing of your forms, command buttons, functions and events
is the only way to find out what types of errors your users might encounter. All subs and
functions should have error trapping. Any decent programmer is going to attempt to trap
for all possible errors.

Here is the VBA for some of the standard buttons...

DoCmd.GoToRecord , , acFirst
DoCmd.GoToRecord , , acPrevious
DoCmd.GoToRecord , , acNext
DoCmd.GoToRecord , , acLast
DoCmd.GoToRecord , , acNewRec
DoCmd.RunCommand acCmdSaveRecord
DoCmd.RunCommand acCmdUndo
DoCmd.RunCommand acCmdDeleteRecord

Here some code that will create a table detailing most (not all) common error numbers and
their descriptions. It is from Microsoft and I have made a few modifications to it.
Code:
'The following procedure creates a table containing many of the error codes and
'strings used or reserved by Microsoft Access and by the Microsoft Jet database
'engine. Not all error codes are included in the resulting table, as some exist
'outside the range of error codes evaluated by this procedure (0 to 4500).
    
Function AccessAndJetErrorsTable() As Boolean
On Error GoTo Err_AccessAndJetErrorsTable
    
    Dim dbs As Database, tdf As TableDef, fld As Field
    Dim rst As Recordset, lngCode As Long
    Dim strAccessErr As String
    Const conAppObjectError = "Application-defined or object-defined error"
    
    ' Delete tAccessAndJetErrors table if it already exists
    If CurrentDb.OpenRecordset("SELECT 1 FROM MSysObjects WHERE Type=1 AND Flags=0 AND Name='tAccessAndJetErrors'").RecordCount = 1 Then DoCmd.DeleteObject acTable, "tAccessAndJetErrors"
    
    ' Create Errors table with ErrorNumber and ErrorDescription fields.
    Set dbs = CurrentDb
    Set tdf = dbs.CreateTableDef("tAccessAndJetErrors")
    Set fld = tdf.CreateField("ErrorCode", dbLong)
    
    tdf.Fields.Append fld
    Set fld = tdf.CreateField("ErrorString", dbMemo)
    tdf.Fields.Append fld
    
    dbs.TableDefs.Append tdf
    ' Open recordset on Errors table.
    Set rst = dbs.OpenRecordset("tAccessAndJetErrors")
    ' Loop through error codes.
    For lngCode = 0 To 3500
        On Error Resume Next
        ' Raise each error.
        strAccessErr = AccessError(lngCode)
        DoCmd.Hourglass True
        ' Skip error numbers without associated strings.
        If strAccessErr <> "" Then

        ' Skip codes that generate application or object-defined errors.
            If strAccessErr <> conAppObjectError Then
                ' Add each error code and string to Errors table.
                rst.AddNew
                rst!ErrorCode = lngCode
                ' Append string to memo field.
                rst!ErrorString.AppendChunk strAccessErr
                rst.Update
            End If
        End If
    Next lngCode
    ' Close recordset.
    rst.Close
    DoCmd.Hourglass False
    RefreshDatabaseWindow
    
    Set dbs = Nothing
    Set tdf = Nothing
    Set fld = Nothing
    Set rst = Nothing
    
    Beep
    MsgBox "Access and Jet errors table was created.", vbInformation, "tAccessAndJetErrors"
    
AccessAndJetErrorsTable = True
    
Exit_AccessAndJetErrorsTable:
    Exit Function
    
Err_AccessAndJetErrorsTable:
    Beep
    MsgBox "Runtime Error # " & Err.Number & vbCrLf & vbLf & Err.Description
    Resume Exit_AccessAndJetErrorsTable
    
End Function
HTH
 
Sorry for my incompetance!

How do you make that code make the table? I have put it into the VB editor thing in Access, and tried to make it run from a button, but that doesn't work, then tried a macro, but it didn't find any functions.

When I try to click Run in the VB editor, it just asks me for a macro to run?!?!

Help! Sorry!
 
How to run Run

Your procedure must have the focus or it doesn't know which function to run. Place your mouse in the function and left click. Then do the run.
 
Also, is this right....

Private Sub NextButton_Click()
On Error GoTo NextButton_Click_Err

NextButton_Click_Exit:
Exit Sub

NextButton_Click_Err:
If Err = 2046 Then 'The command or action 'Next Record' isn't available now
Exit Sub
ElseIf Err = 2501 Then 'The RunCommand action was canceled
Exit Sub
Else
MsgBox Err.Number & " - " & Err.Description
Resume NextButton_Click_Exit
End If

End Sub

I know those error numbers arn't right....but otherwise is that OK?

Thanks
 
I still can't get that table creation code to work....I don't suppose I could trouble someone to send me the database with it working?

Thanks....you have no idea how much help you people have been!
 
Looks good except I did not see the actual command.
Code:
Private Sub NextButton_Click()
On Error GoTo NextButton_Click_Err
     
    [COLOR=blue]DoCmd.GoToRecord , , acNext[/COLOR]
    
NextButton_Click_Exit:
Exit Sub
    
NextButton_Click_Err:
If Err = 2046 Then 'The command or action 'Next Record' isn't available now
    Exit Sub
ElseIf Err = 2501 Then 'The RunCommand action was canceled
    Exit Sub
Else
    MsgBox Err.Number & " - " & Err.Description
    Resume NextButton_Click_Exit
End If
    
End Sub
 
What does this part of the code do?

Code:
If Err = 2046 Then 'The command or action 'Next Record' isn't available now

Where does the "'The command or action 'Next Record' isn't available now" bit appear?
 
Here is a db with the function to make the error table. I set up an AutoExec macro to run the code for you. HTH
 

Attachments

Thanks LOADS ghudson, that worked a treat.....I even figured out how it was done in the end (from yours!).

Anyway, my new question is, in that list, I can't seem to find anything about a missing backend. I would like to error trap the message that comes up when the db can't find it's backend.

Also, for that, where would I put the code?

Thanks
 
This is how I test if the user has access to the directory that the back end is located on.
I use this in the OnOpen event of the main form.
Code:
    If Dir("\\Server\Partition\Folder", vbDirectory) = "" Then
    'If Dir("X:\Folder", vbDirectory) = "" Then
        Beep
        MsgBox "Your PC is not connected to the network or you are not authorized to access the X:\Database\ directory!", vbCritical, "Database Connection Error"
        DoCmd.Quit acQuitSaveNone
    Else
        'MsgBox "OK - Network access to folder is verified"
    End If
This is one way to test if the users computer can find the backend file.
Code:
    If Dir("\\Server\Partition\Folder\BackendDB.mdb") = "" Then
    'If Dir("X:\Folder\BackendDB.mdb") = "" Then
        Beep
        MsgBox "Your PC can not find the backend of the database!", vbCritical, "Database Connection Error"
        DoCmd.Quit acQuitSaveNone
    Else
        'MsgBox "OK - found the backend file"
    End If
I am sure that there is a way to test if the connection to the linked tables is OK
but I have never had to do that.

HTH
 
Is there a way of creating a list of A2000 error msg's?
Downloaded the above sample and it worked a treat but seemed to refer to A97 (Or are errors consistant across versions?) hevean help me - no - it is a Microsoft product :p
Dave
 
When I make a database I create a table primarily for collecting errors.

On every event I send the error details to a function that writes the information into the table with other stuff such as the date and time and the user who created the error.

This way I can look at my error log and find out which controls are causing which errors.
 

Users who are viewing this thread

Back
Top Bottom