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

Procedure that returns Function or Sub Routine Name 1

Status
Not open for further replies.

sethdromgoole

Programmer
Apr 7, 2005
11
0
0
US
Hello,

I am finalizing the error handling of an app I built. When a function or sub routine errors, I call a function that creates a new record on the ErrorLog table. I use the err.*** command to get information about the error; however, I haven't figured out a way to get the function or sub routine name where the error occurred. Thus, I have to type out the function or sub routine name in quotes when passing it to the function that creates the record in the error table.

Thanks,

Seth Dromgoole

 
Sorry, I don't think there's any other way...

Roy-Vidar
 
...well except storing the sub or function name in a variable within the sub or function, and pass that, but that's really the same, isn't it?

Roy-Vidar
 
You might also find this useful.

Using the standard IsLoaded function as an example, I trap errors occuring in forms and reports, and write them to a table so I can have a log of them for later action. When supporting multiple sites and users, no-one really remembers what happened or wrote down the error they received.

Code:
Function IsLoaded(ByVal strFormName As String) As Boolean
On Error GoTo Errorhandler
    
    Const conObjStateClosed = 0
    Const conDesignView = 0
    
    If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
        If Forms(strFormName).CurrentView <> conDesignView Then
            IsLoaded = True
        End If
    End If
    
    Exit Function
    
Errorhandler:
    Call Error_Display_Vars(Err, Application.CurrentObjectName)
    
End Function

And now for the actual error display and log subroutine -

Code:
Sub Error_Display_Vars(Err, ProgramName)

    Dim dbs As Database
    Dim rst As Recordset
    Dim strCriteria As String
    
    Dim msg
    
    Set dbs = CurrentDb
    
    strCriteria = "SELECT * FROM U_Error_Log WHERE ErrorID =0;"
    Set rst = dbs.OpenRecordset(strCriteria, dbOpenDynaset)

    If Err Then
        msg = "Error # " & str(Err.Number) & " was generated by " _
            & Err.Source & " - " & Err.Description
        MsgBox msg, , "Error", Err.HelpFile, Err.HelpContext
        
        rst.AddNew
        rst!WhenItHappened = Now()
        rst!WhichProgram = Left(ProgramName, 50)
        rst!WhoWasIt = GetUserName
        rst!WhatHappened = Left(msg, 250)
        rst.Update
    End If
    
    rst.Close
    Set dbs = Nothing
    
End Sub

If you want to debug the error while working on you program, comment out the On Error statement. Be aware however that subroutines calling subroutines or functions might require you to comment out in each subroutine.

Before distributing, I run a search and replace for 'On Error and replace them with On Error so the error trapping is bulletproof for the users.
 
Payback,

My code that writes to an error log is very similar. Do trap the function name or sub routine name that errored? I couldn't spot this in your code. Also, will putting the error trap in the IsLoaded() event cascade down to all functions/routines in the form? Currently, I have the on error goto.... in each function/routine.

Thanks,

-Seth
 
I was not really addressing your first question regarding function names etc. I just thought the above would be helpful.

The IsLoaded function was just an example. I have the code in every subroutine and function I write (copy and paste it each time) therefore in a form those pertinent lines are repeated many times.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top