|
-
Jul 8th, 2005, 08:35 AM
#1
Thread Starter
Don't Panic!
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.
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...
-
Jul 8th, 2005, 10:47 AM
#2
Frenzied Member
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
-
Jul 8th, 2005, 11:56 AM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jul 8th, 2005, 11:36 PM
#4
Lively Member
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:
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
' CENTRAL ERROR HANDLING SYSTEM: '
' Log all errors to a log file which tracks the '
' logged in user, the code module the error came '
' from, the procedure, and the date and time '
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
Option Explicit
'Name of our error log
Private Const msAPP_ERRLOG As String = "\Error.log"
Public Function bCentralErrorHandler(ByVal sModule As String, ByVal sSub As String) As Boolean
Dim sFullSource As String
Dim sDescription As String
Dim iFileNum As Integer
Dim sPath As String
sPath = ThisWorkbook.Path
' Make sure there's no errors in the Error Handling system
On Error Resume Next
iFileNum = FreeFile()
' Make sure the log path is valid
If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"
' Get the file the error came from, the module, and a procedure description
sFullSource = "Module: " & sModule & " Procedure: " & sSub & " "
Select Case Err.Number
Case Is <> 0
sDescription = Err.Description
Case Else
sDescription = "Minor Error"
End Select
' Log the error
Open sPath & msAPP_ERRLOG For Append As #iFileNum
Print #iFileNum, Format$(Now(), "mm/dd/yy hh:mm AM/PM")
Print #iFileNum, "******************************"
Print #iFileNum, Application.UserName
Print #iFileNum, sFullSource
Print #iFileNum, sDescription
Print #iFileNum, "******************************"
Print #iFileNum, " "
Close #iFileNum
Err.Clear
' Reset the error handler
bCentralErrorHandler = False
End Function
Then just add this to every error handler necessary:
VB Code:
ErrHandle:
If bCentralErrorHandler("Module Name", "Sub's Name") Then
End If
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...
-
Jul 11th, 2005, 04:53 AM
#5
Thread Starter
Don't Panic!
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 :/
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...
-
Jul 11th, 2005, 10:53 AM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|