Current module name?

E

Ed Adamthwaite

Hi all,
I am trying to find a way of returning the currently open module's name so
that I can programmatically insert a sub or function code template into it
without having to type out the module's name when calling the procedure.
I think can do it with an API call on the VBA IDE's Title bar Caption, but
would prefer to stay within VB.

Any Ideas?

Thanks in advance for any answers.
Regards
Ed.
 
M

Marshall Barton

Ed said:
I am trying to find a way of returning the currently open module's name so
that I can programmatically insert a sub or function code template into it
without having to type out the module's name when calling the procedure.
I think can do it with an API call on the VBA IDE's Title bar Caption, but
would prefer to stay within VB.


If there is only one module open, you can get its name by
using:
Modules(0).Name
 
E

Ed Adamthwaite

Thanks Marshall,
I didn't think of that, but it is common to have more than one module open
at a time, so it would be good if there was an "OnTop" or "Visible" property
for a module.
eg:

Function CurrentModule() As String
Dim i As Integer
For i = 0 To Modules.Count - 1
If Modules(i).OnTop Then
CurrentModule = Modules(i).Name
Exit Function
Next
End Function
 
M

Marshall Barton

Ed said:
I didn't think of that, but it is common to have more than one module open
at a time, so it would be good if there was an "OnTop" or "Visible" property
for a module.
eg:

Function CurrentModule() As String
Dim i As Integer
For i = 0 To Modules.Count - 1
If Modules(i).OnTop Then
CurrentModule = Modules(i).Name
Exit Function
Next
End Function


"Marshall Barton" wrote
 
M

Marshall Barton

That might be nice for your situation, but I have never seen
a way to get that info.

In general, I think that concept may be ambiguous in the
context of an entire application. The item "on top" is
pretty much defined as the object with the focus. However,
in order to initiate the code you are looking to write, some
other object (form, report, debug window, ...) must have the
focus, so the "on top" module would not be a well defined
term. I haven't explored this, but a tool/menu bar button
does not take the focus so you may be able to set up
conditions where CurrenObjectName gets what you want???
 
E

Ed Adamthwaite

Thanks Marshall,
CurrentObjectName works for the name of all other Access objects except
modules. Damn!
Because the VBA IDE is in a separate MDI app, it doesn't seem to recognise
objects within it's self as Access objects.
I will have to try an API call to see if it is possible but I don't know the
class name for the VBA IDE.
Oh well, I'll just have to keep plugging on...

Regards,
Ed.
Marshall Barton said:
That might be nice for your situation, but I have never seen
a way to get that info.

In general, I think that concept may be ambiguous in the
context of an entire application. The item "on top" is
pretty much defined as the object with the focus. However,
in order to initiate the code you are looking to write, some
other object (form, report, debug window, ...) must have the
focus, so the "on top" module would not be a well defined
term. I haven't explored this, but a tool/menu bar button
does not take the focus so you may be able to set up
conditions where CurrenObjectName gets what you want???
--
Marsh
MVP [MS Access]


Ed said:
I didn't think of that, but it is common to have more than one module open
at a time, so it would be good if there was an "OnTop" or "Visible"
property
for a module.
eg:

Function CurrentModule() As String
Dim i As Integer
For i = 0 To Modules.Count - 1
If Modules(i).OnTop Then
CurrentModule = Modules(i).Name
Exit Function
Next
End Function


"Marshall Barton" wrote
 
E

Ed Adamthwaite

Wacko! I have discovered how to do it!

Set mdl = Modules(VBE.ActiveCodePane.CodeModule.Parent.Name)

Works like a charm!

Ed.

Ed Adamthwaite said:
Thanks Marshall,
CurrentObjectName works for the name of all other Access objects except
modules. Damn!
Because the VBA IDE is in a separate MDI app, it doesn't seem to recognise
objects within it's self as Access objects.
I will have to try an API call to see if it is possible but I don't know
the class name for the VBA IDE.
Oh well, I'll just have to keep plugging on...

Regards,
Ed.
Marshall Barton said:
That might be nice for your situation, but I have never seen
a way to get that info.

In general, I think that concept may be ambiguous in the
context of an entire application. The item "on top" is
pretty much defined as the object with the focus. However,
in order to initiate the code you are looking to write, some
other object (form, report, debug window, ...) must have the
focus, so the "on top" module would not be a well defined
term. I haven't explored this, but a tool/menu bar button
does not take the focus so you may be able to set up
conditions where CurrenObjectName gets what you want???
--
Marsh
MVP [MS Access]


Ed said:
I didn't think of that, but it is common to have more than one module
open
at a time, so it would be good if there was an "OnTop" or "Visible"
property
for a module.
eg:

Function CurrentModule() As String
Dim i As Integer
For i = 0 To Modules.Count - 1
If Modules(i).OnTop Then
CurrentModule = Modules(i).Name
Exit Function
Next
End Function


"Marshall Barton" wrote
Ed Adamthwaite wrote:
I am trying to find a way of returning the currently open module's name
so
that I can programmatically insert a sub or function code template into
it
without having to type out the module's name when calling the
procedure.
I think can do it with an API call on the VBA IDE's Title bar Caption,
but
would prefer to stay within VB.


If there is only one module open, you can get its name by
using:
Modules(0).Name
 
M

Marshall Barton

Very nice work Ed. I've never stumbled over the CodePane
object before. Just goes to show, where there's a will,
there's a way.

Thanks for posting your solution for everyone's
enlightenment.
 
Joined
May 3, 2011
Messages
1
Reaction score
0
That is all well and good if you have the module open and want to get the name of it.

How do you get the name of it in code within the module?

I am looking for something like this:

strModuleName = CurrentModule.name

Of course this does not work, but you get the idea.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top