Solved

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

Posted on 2016-11-16
11
85 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)
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 58
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 58
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 8

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 48

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 58
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
 
LVL 48

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 17

Expert Comment

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

Open in new window

0
 
LVL 8

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 58

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 48

Author Closing Comment

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

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

691 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