Results 1 to 6 of 6

Thread: VBA - Put the currently running module/sub name when running code in it

  1. #1

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    VBA - Put the currently running module/sub name when running code in it

    Hi,

    A colleague (who is even more lazy than me) wants to write a generic Error handler for his db/excel code.

    Only he is so lazy he doesn't want to hard code if possible.

    Example Code:
    Code:
    Public sub Blah()
        On error goto ErrH:
    
    '---- other code
    
    ErrH:
        ErrorHandlerSub <thissub>
    
    End Sub
    
    Public Sub ErrorHandlerSub(byval strSubThatErrored as string)
       debug.print "Error : " & err.number & " - " & err.description
       debug.print "Errored in : " & strSubThatErrored
    End Sub
    I haven't come across it, and I personally would hardcode it. But it was just a thought if there was a way to retrieve the sub name etc whilst running.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: VBA - Put the currently running module/sub name when running code in it

    I saw here a while back that for Excel you can use Application.Caller. Doesn't work in Access, though.
    Tengo mas preguntas que contestas

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA - Put the currently running module/sub name when running code in it

    You may want to check out .LastDLLError and .Source of the Err object, depending on how your project is structured.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: VBA - Put the currently running module/sub name when running code in it

    How about a central error handling method?

    In a standard module:
    VB Code:
    1. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    2. '         CENTRAL ERROR HANDLING SYSTEM:            '
    3. '   Log all errors to a log file which tracks the   '
    4. '   logged in user, the code module the error came  '
    5. '   from, the procedure, and the date and time      '
    6. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    7. Option Explicit
    8.  
    9. 'Name of our error log
    10. Private Const msAPP_ERRLOG As String = "\Error.log"
    11.  
    12.  
    13. Public Function bCentralErrorHandler(ByVal sModule As String, ByVal sSub As String) As Boolean
    14.     Dim sFullSource As String
    15.     Dim sDescription As String
    16.     Dim iFileNum As Integer
    17.     Dim sPath As String
    18.    
    19.     sPath = ThisWorkbook.Path
    20.    
    21. '   Make sure there's no errors in the Error Handling system
    22.     On Error Resume Next
    23.     iFileNum = FreeFile()
    24.    
    25. '   Make sure the log path is valid
    26.     If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"
    27.  
    28. '   Get the file the error came from, the module, and a procedure description
    29.     sFullSource = "Module: " & sModule & "  Procedure: " & sSub & " "
    30.    
    31.     Select Case Err.Number
    32.         Case Is <> 0
    33.             sDescription = Err.Description
    34.         Case Else
    35.             sDescription = "Minor Error"
    36.     End Select
    37.                      
    38. '   Log the error
    39.     Open sPath & msAPP_ERRLOG For Append As #iFileNum
    40.     Print #iFileNum, Format$(Now(), "mm/dd/yy hh:mm AM/PM")
    41.     Print #iFileNum, "******************************"
    42.     Print #iFileNum, Application.UserName
    43.     Print #iFileNum, sFullSource
    44.     Print #iFileNum, sDescription
    45.     Print #iFileNum, "******************************"
    46.     Print #iFileNum, " "
    47.     Close #iFileNum
    48.    
    49.     Err.Clear
    50. '   Reset the error handler
    51.     bCentralErrorHandler = False
    52. End Function

    Then just add this to every error handler necessary:
    VB Code:
    1. ErrHandle:
    2.     If bCentralErrorHandler("Module Name", "Sub's Name") Then
    3.     End If
    4. End Sub

    I hard code each module name in a constant string at the top of each module,
    and then use a constant within each procedure which contains the procedures name.

    This logs all errors to a log file stored in the same path as the Excel file.
    Hope it helps out with some ideas...

  5. #5

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: VBA - Put the currently running module/sub name when running code in it

    Justin:
    Yeah thats what I would have done, but you then need to 'hardcode' where it is coming from.

    The Thread is to see if there is a way of identifying the sub as it is running, not hard coding it.

    I am guessing that there isn't a way, or that it is very tricky if there is.

    RogDogg:
    Thanks, I will check and see if it is useful, but in normal errors I do not think that the err.lastDllError is set :/

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA - Put the currently running module/sub name when running code in it

    Correct, Vince thats why I CMA with the "depending how your program is structured". Although if you were to make a dll out
    of your code then it would be populated with error info.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width