views:

179

answers:

4

For a very long time, when I have an error handler I make it report what Project, Module, and Procedure the error was thrown in. I have always accomplished this by simply storing their name via constants. I know that in a Class you get the name programmatically with TypeName(Me), but obviously that only gets me one out of three pieces of information and only when I'm not in a "Standard" module.

I don't have a really huge problem with using constants, it's just that people don't always keep them up to date, or worse they copy and paste and then you have the wrong name being reported, etc. So what I would like to do is figure out a way to get rid of the Constants shown in the example, without losing the information.

Option Compare Binary
Option Explicit
Option Base 0
Option Private Module

Private Const m_strModuleName_c As String = "MyModule"

Private Sub Example()
    Const strProcedureName_c As String = "Example"
    On Error GoTo Err_Hnd
Exit_Proc:
    On Error Resume Next
    Exit Sub
Err_Hnd:
    ErrorHandler.FormattedErrorMessage strProcedureName_c, m_strModuleName_c, _
        Err.Description, Err.Source, Err.Number, Erl
    Resume Exit_Proc
End Sub

Does anyone know ways to for the code to tell where it is? If you can conclusively show it can't be done, that's an answer too:)

Edit:
I am also aware that the project name is in Err.Source. I was hoping to be able to get it without an exception for other purposes. If you know great, if not we can define that as outside the scope of the question.
I am also aware of how to get the error line, but that information is of course only somewhat helpful without knowing Module.Procedure.

+1  A: 

Unfortunately, you'll need to have individual On Error GoTo X statements for individual modules and procedures. The project is always stored in Err.Source. The VBA error handling isn't all that great in this area -- after all, how much good does the project name as the source of the error, as opposed to procedure/module, do you.

If you manually or programatically number your lines (like old-school BASIC), you can use ERL to list the line number the error occurred on. Be warned, though, that an error that occurs on a line without a number will make ERL throw its own error, instead of returning a zero. More information can be found at this blog post.

If you're using Access 2007 (not sure about other Office apps/versions), try this code snippet dug out of the help documentation:

Sub PrintOpenModuleNames()
    Dim i As Integer
    Dim modOpenModules As Modules

    Set modOpenModules = Application.Modules

    For i = 0 To modOpenModules.Count - 1

        Debug.Print modOpenModules(i).Name

    Next
End Sub

And Microsoft includes these remarks:

  • All open modules are included in the Modules collection, whether they are uncompiled, are compiled, are in break mode, or contain the code that's running.
  • To determine whether an individual Module object represents a standard module or a class module, check the Module object's Type property.
  • The Modules collection belongs to the Microsoft Access Application object.
  • Individual Module objects in the Modules collection are indexed beginning with zero.

So far, I haven't been able to find anything on referencing the current Project or Procedure. but this should point you in the right direction.

A. Scagnelli
Hello, I apologize I should have been more specific in what I was asking. I edited the main question to try and narrow up the scope a little. +1 for effort:)
Oorang
The added information should help, even if it only gives granularity based on the module.
A. Scagnelli
On Access and VBIDE and the like... That's a good thought but it doesn't tell you where you are in the code:) Also VBIDE comes with AV baggage.
Oorang
+2  A: 

There are several questions here.

You can get the Project Name by calling App.Name You cannot get the name of the method you are in. I recommend using the automated procedure templates from MZ Tools, which will automatically put in all the constants you need and your headache will be over.

The last piece is possibly having to know the name of the EXE (or lib) that invoked your ActiveX DLL. To figure this out, try the following:

'API Declarations'
Private Declare Function GetModuleFileName Lib _
    "kernel32" Alias "GetModuleFileNameA" (ByVal _
    hModule As Long, ByVal lpFileName As String, _
    ByVal nSize As Long) As Long

Private Function WhosYourDaddy() As String
    Dim AppPath As String
    Const MAX_PATH = 260

    On Error Resume Next

    'allocate space for the string'
    AppPath = Space$(MAX_PATH)

    If GetModuleFileName(0, AppPath, Len(AppPath)) Then
        'Remove NULLs from the result'
        AppPath = Left$(AppPath, InStr(AppPath, vbNullChar) - 1)
        WhosYourDaddy = AppPath
    Else
        WhosYourDaddy = "Not Found"
    End If
End Function
AngryHacker
As it happens, I do use MZ tools:) Doesn't really get the Module name, but still very helpful. +1
Oorang
+1. By the way, the syntax highlighting works better if you add a single quote at the end of each comment line. Otherwise it thinks the comment spans multiple lines.
MarkJ
I tried it - doesn't seem to work. Feel free to edit my response. Looks like you have the right now that you are over 2000 rep.
AngryHacker
OK I will! I think you simply missed one of the comments. I must use this power only for good! :)
MarkJ
+2  A: 

For the project name, the only way I can think of doing this is by deliberately throwing an error somewhere in Sub Main(), and in the error handling code, save the resulting Err.Source into an global variable g_sProjectName. Otherwise, I seem to remember that there was a free 3rd party DLL called TLBINF32.DLL which did COM reflection - but that seems way over the top for what you want to do, and in any case there is probably a difference between public and private classes. And finally, you could use a binary editor to search for the project name string in your EXE, and then try to read the string from the position. Whilst it is frustrating that the names of every project and code module is embedded in the EXE, there seems to be no predictable way of doing this, so it is NOT recommended.

Mark Bertenshaw
Hmmm, looks like this one shipped with Visual Studio 6. But it it's not supported for Vista and Windows 7. If possible I'd like a solution that could be used without relying on dlls not part of the standard install. Still, good info. +1
Oorang
A: 

I suggest you take a look at CodeSMART for VB6, This VB6 addin has a customizable Error Handling Schemes Manager, with macros that will insert strings for module name, method name, etc., into your error handling code with a single context menu selection.

Has some other very nice features as well - a Find In Files search that's superior to anything I'd seen till ReSharper, a Tab Order designer, and much more.

At my previous employer, we used this tool for many years, starting with the 2005 version. Once you get used to it,it's really hard to do without it.

Tom Bushell