How can I determine in which module my code works?

For a very long time, when I have an error handler, I do this to report which project, module, and procedure were introduced. I always did this by simply storing their name through constants. I know that in the class you get the name programmatically using TypeName (Me), but obviously this gives me only one of the three pieces of information and only when I'm not in the 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, even worse, they copy and paste, and then you report the wrong name, etc. So I would like to figure out how to get rid of the constants shown in the example without losing 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

- , , , ? , , :)

:
, Err.Source. . , , .
, , , , , Module.Procedure.

+3
4

, , - - Sub Main(), Err.Source g_sProjectName. , , , DLL, TLBINF32.DLL, COM-, , , , , . , , EXE, . , EXE, , , .

+2

.

, App.Name , . MZ Tools, , , .

, , EXE ( lib), ActiveX DLL. , :

'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
+2

, On Error GoTo X. Err.Source. VBA . , , /.

(, BASIC), ERL, , . , , , ERL , , . .

Access 2007 ( Office), :

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

Microsoft :

  • , , , , .
  • , , .
  • Microsoft Access.
  • .

- . .

+1

CodeSMART VB6. VB6 , , , .. .

There are other very nice features: a search in Find In files, which was superior to everything I've seen before ReSharper, a tab designer and much more.

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

0
source

Source: https://habr.com/ru/post/1711361/


All Articles