• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 113
  • Last Modified:

Microsoft Access - determine the name of the module which code is running in.

I'm having a brain cramp this afternoon.

I need to capture the name of the current code module, programmatically.

I know there is a function or some such which will return this value, but it just escapes me this afternoon.

Dale
0
Dale Fye
Asked:
Dale Fye
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<<I'm having a brain cramp this afternoon.>>

 No, your not.  There's no built-in function to do this.

 Best you can do is a Constant in each module.

Jim.
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
and maybe to clear up some confusion, vbWatchDog from Wayne Phillips will return a module name, but he hooked into VBA outside of Access and VBA itself.

Jim.
0
 
Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
The Application.CodeContextObject.Name can return the name of the form or report that called the code. This is regardless of whether the code being executed is actually in a module.
E.g. button on form "frmTest" calls procedure fTest in module mTest:
Public Sub fTest()
    MsgBox CodeContextObject.Name
End Sub

Open in new window

This returns "frmTest"

Not exactly what you asked for, but I believe that is the best that can be done.
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
Dale FyeAuthor Commented:
Jim,

I distinctly remember a discussion of this in the MVP google group, and I remember someone there mentioning it.  I even tested it, and it worked, I just for the life of me cannot even come up with a search term for the group email that might result in the discussion.  I'll wait a while here and if no one comes up with it, I'll check the google group.

Dale
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<<and I remember someone there mentioning it. >>

 That would be news to me....

you can come up with the line number with vba.erl (undocumented), but not the routine or module name.

Jim.
0
 
Dale FyeAuthor Commented:
Well it turns out that the Application.CodeContextObject will return the name of the object that called the code, but will not return the name of the procedure that is actually running. And if you simply create a procedure which you want to display the CodeContextObject.Name, and you call that from the immediate window, you will get an error message.

 So I guess I'm going to have to go with setting a variable for each module name.
0
 
John TsioumprisSoftware & Systems EngineerCommented:
Does this does the job ?
Application.VBE.ActiveCodePane.CodeModule.Name

Open in new window

0
 
Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
Nope: That will show what the code EDITOR has active. Not what module the code is run from.
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<< So I guess I'm going to have to go with setting a variable for each module name.>>

 That's what I do.   Each module has a const called ModuleName:

Const ModuleName = "Application Specific"

Open in new window


 and then each procedure has a const for RoutineName, along with a Version:

Public Sub AppMain()

          ' Mainline for processing of orders.

          ' 2.2.0.0 - OCS/JRD - 04/26/14 - Added check for orders that needed to be auto scheduled.
          ' 2.3.0.0 - OCS/JRD - 12/10/14 - Added call to get FedEx ship data and adjusted percentages.
          ' 2.4.0.0 - OCS/JRD - 03/18/15 - Added call to check for routing requests, get any routing instructions returned (EDI 754), and adjusted percentages.

          Const RoutineName = "AppMain"
          Const Version = "2.4.0.0"

Open in new window


and then an error handler that uses it all:

AppMain_Error:
1150      UnexpectedError ModuleName, RoutineName, Version, Err.Number, Err.Description, Err.Source, VBA.Erl
1160      Resume AppMain_Exit

Open in new window


and note the above was not for those here, but for anyone that might read this in the future and not have a clear understanding of what we are discussing.

Jim.
0
 
Dale FyeAuthor Commented:
Thanks guys, combination of the two will meet my needs.
1

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now