Solved

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

Posted on 2016-11-16
11
63 Views
Last Modified: 2016-11-21
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
Comment
Question by:Dale Fye (Access MVP)
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 57
ID: 41890449
<<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
 
LVL 57
ID: 41890452
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
 
LVL 6

Assisted Solution

by:Anders Ebro (Microsoft MVP)
Anders Ebro (Microsoft MVP) earned 175 total points
ID: 41890459
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
 
LVL 47

Author Comment

by:Dale Fye (Access MVP)
ID: 41890461
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
 
LVL 57
ID: 41890463
<<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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 47

Author Comment

by:Dale Fye (Access MVP)
ID: 41890619
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
 
LVL 13

Expert Comment

by:John Tsioumpris
ID: 41891302
Does this does the job ?
Application.VBE.ActiveCodePane.CodeModule.Name

Open in new window

0
 
LVL 6

Expert Comment

by:Anders Ebro (Microsoft MVP)
ID: 41891308
Nope: That will show what the code EDITOR has active. Not what module the code is run from.
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 325 total points
ID: 41891316
<< 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
 
LVL 47

Author Closing Comment

by:Dale Fye (Access MVP)
ID: 41896728
Thanks guys, combination of the two will meet my needs.
1

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now