Solved

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

Posted on 2016-11-16
11
50 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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

11 Experts available now in Live!

Get 1:1 Help Now