Results 1 to 13 of 13
  1. #1
    Join Date
    Oct 2004
    Posts
    18

    Question Unanswered: VBA Error trapping question

    Hi,

    When an error occurs I use the VBA error trapping to log the name of the screen, the user, the error number and the error description in a table. Is it possible to dump the local variables (f.i. as seen in the Locals Window) as well ?
    Can this be done with VBA or with an API ?

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Post your error handling code... It might come in handy when trying to help you make the necessary changes.
    This can be done, it all depends on your handling method.
    George
    Home | Blog

  3. #3
    Join Date
    Oct 2004
    Posts
    18

    Error code:

    Some procedure:

    Private Sub cmdOK_Click()
    On Error GoTo Error_cmdOK_Click

    ...

    Exit_cmdOK_Click:
    Exit Sub

    Error_cmdOK_Click:
    ErrorOK = RMS_ErrorHandling(Err.Number, Err.Description)
    Resume Exit_cmdOK_Click
    Resume

    End Sub


    The error handling function:

    Public Function RMS_ErrorHandling(ByVal ErrNumber As Long, ByVal ErrDescription As String) As Boolean
    'General function to handle errors
    On Error GoTo Error_RMS_ErrorHandling

    Dim strMsg As String
    Dim strFormName As String
    Dim strSQL As String
    Dim rst As ADODB.Recordset

    DoCmd.Hourglass False

    If Not IsNull(Screen.ActiveForm.Name) Then
    strFormName = Screen.ActiveForm.Name
    Else
    strFormName = ""
    End If

    'Set error messages
    Select Case ErrNumber
    Case 3146

    'Some message

    GoTo Exit_RMS_ErrorHandling
    Case 3051

    'Some message

    GoTo Exit_RMS_ErrorHandling
    Case 30014
    'do nothing
    Case Else
    'General error message


    'Log the error
    strSQL = "Select * From tblErrorLog Where 1 = 0"
    Set rst = New ADODB.Recordset
    rst.Open strSQL, CurrentProject.Connection, adOpenStatic, adLockPessimistic
    rst.AddNew
    rst("intErrorNr") = ErrNumber
    rst("strErrorDescription") = ErrDescription
    rst("dtmErrorDate") = Now()
    rst("strFormName") = strFormName
    If Not IsNull(Forms!frm_Switchboard!txtUserID) Then
    rst("intUserID") = Forms!frm_Switchboard!txtUserID
    End If
    If Not IsNull(Forms!frm_Switchboard!txtCountry) Then
    rst("strCountry") = Forms!frm_Switchboard!TxtCountryNSC
    End If
    rst("blnSolved") = 0
    rst.Update
    rst.Close
    Set rst = Nothing

    GoTo Exit_RMS_ErrorHandling
    End Select

    Exit_RMS_ErrorHandling:
    Exit Function

    Error_RMS_ErrorHandling:
    'Set error message if an error occured during the error handling
    strMsg = "Error " & Err.Number & ": " & vbCrLf
    strMsg = strMsg & Err.Description & vbCrLf & vbCrLf
    strMsg = strMsg & "While trapping following error:" & vbCrLf
    strMsg = strMsg & ErrNumber & vbCrLf
    strMsg = strMsg & ErrDescription & vbCrLf & vbCrLf & conMsgFoot
    MsgBox strMsg
    Resume Exit_RMS_ErrorHandling
    Resume
    End Function

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Ah, you use an error log - clever... Perhaps too clever

    I think you may have to pass your variables to the function much like the error number and description.
    Code:
    Public Function RMS_ErrorHandling(ByVal ErrNumber As Long, ByVal ErrDescription As String, ErrVariable1 As String, ErrVariable2 As String, ErrVariable...) As Boolean
    Not neat, not flexible, but it's all I can think of right now...
    George
    Home | Blog

  5. #5
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    The only problem you're gonna have is with trying to capture local variables where the error happened ... In your error handling routine, any local vars in the calling function/proc are out of scope to you (don't exist/can't be accessed) ...
    Back to Access ... ADO is not the way to go for speed ...

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    That's a much more concise and coherant way of saying what I wanted to... say...
    Which is why I suggested passing them to the function as you are doing with the error numbers and descriptions
    George
    Home | Blog

  7. #7
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    George is a bit too fast here ... Beat me to the punch. You don't need to pass the Err # and Desc to the logging function - pick those up within -> they are not going to reset by invoking your log function (the only way the errors get lost is on an Err.Clear or another error occurs) ... You could pass in your varaible values as a variant array (like George pointed out) ...
    Back to Access ... ADO is not the way to go for speed ...

  8. #8
    Join Date
    Oct 2004
    Posts
    18
    OK, I can pass the variables of a certain procedure to the error logging function with lets say a ParamArray but I want to avoid this because this means a awful lot of code because every procedure is different. I need some VBA function or API that "dumps" the variables of regardless what procedure. I thought: if this can be done in the 'Locals Window', why not in code ?

  9. #9
    Join Date
    Jul 2003
    Location
    Michigan
    Posts
    1,941
    Hmmm... you could have a lot of variables active (in scope) at one time when the error happened. How will your handler know which to pass?

    I use an error log similar to what you have. I save the function name instead of the form name, though. I've thought about doing what you are suggesting (saving the variable name and value) but decided it was just too cumbersome.
    Inspiration Through Fermentation

  10. #10
    Join Date
    Oct 2004
    Posts
    18
    The error trapping is used in an adp application that runs in several countries. With the current error logging I can only reproduce about 50% of the errors. A lot of error messages or just to vague.... Thats why I thought of a variable dump. But it seems there is no simple solution for this with VBA. Guess I just will have to live with it....

  11. #11
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    The beauty of a variant array is that it's flexible for every invocation ... So, for function X with vars A-J you pass those in. For proc Y with vars K-T you pass those in ... For function Z with vars A-F and M-S you do the same ...
    Back to Access ... ADO is not the way to go for speed ...

  12. #12
    Join Date
    Oct 2004
    Posts
    18
    That's the problem: I don't want to pass in procedure A variables U,V,W in the array and in procedure B variables X,Y,Z and so on. I want to say: pass the variables of the procedure, regardless of what the names of the variables are. So I need some 'dump' instruction.

  13. #13
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Quote Originally Posted by naverty
    That's the problem: I don't want to pass in procedure A variables U,V,W in the array and in procedure B variables X,Y,Z and so on. I want to say: pass the variables of the procedure, regardless of what the names of the variables are. So I need some 'dump' instruction.
    Can't happen ... At some point you're gonna have to explicitly send the variables ... Hell, even in VB/VC, those "stack"/variable dumps are a matter of knowing that automatic variables are created on the stack and that is what they dump ...

    Access probably also creates the automatic variables on the stack, it's just that that stack is unavailable to the VBA developer ...

    The thing I think you're getting stuck on is that you're thinking that the logging function is going to need to know the variable names that are push in thru the array ... It doesn't. You reference/work/display/twiddle that variable's value only as an array index ... The names are gone to protect the guilty as it were.
    Back to Access ... ADO is not the way to go for speed ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •