self-aware procedures, quines and more

Corticus

Well-known Member
Joined
Apr 30, 2002
Messages
1,579
Hi all,

I'm messing around with self-referencing in procedures, I wondered,
How do I retrieve the name of the module from which a procedure is running?

I'm playing with this code:
Code:
Sub quine()
    
    For I = 0 To Application.Modules.Count - 1
        If Modules(I).Lines(1, 1) = "Sub quine()" Then
            strModName = Modules(I).Name
        End If
    Next I

    DoCmd.OpenModule strModName
    ct = Modules(strModName).CountOfLines
    
    For I = 1 To ct
        strMod = strMod & Chr(10) & Modules(strModName).Lines(I, 1)
    Next I
        
    Modules(strModName).InsertLines ct, "'I'm learning"
        
    MsgBox (strMod)

End Sub

I would like to figure out what strModName is some other way besides looping through all the modules. Something like me.module.name, but that obviously doesn't work.

In the same way you retreive a forms name like Me.Name, I would like to do the same from a procedure.

Thanks,
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I'm thinking there has to be a way, but not sure how myself.
I was fiddling and did this:

Code:
For i = 0 To Application.Modules.Count - 1
  Debug.Print Application.Modules.Item(i).Name
  Debug.Print Application.Modules(i).Find("name_of_sub", 1, 1, Modules(i).CountOfLines, 90)
Next i
 
Upvote 0
Thanks mdmilner,

That looks like something similar to what I'm doing, but it still requires the name of the procedure in order to tell what module it's coming from. I was hoping for a more universal expression that would return the name of the module from which the currenty running procudure in running from, regardless of the name of the procedure. Or even just a way to refer to the name of the procedure running without explicitly referencing it.

This is all just for fun, btw, I'm rereading Godel, Escher, Back by Hofstadter (one of the best books ever written) for the fiftieth time, and I realize I can actually apply some of his logical concepts about self-referencing systems to code. Fun stuff...

This is sort of a VBA quine (I think I may be cheating but its hard to tell):
Code:
Sub quine()
    
    For i& = 0 To Application.Modules.Count - 1
        If Modules(i).Lines(1, 1) = "Sub quine()" Then
            strModName = Modules(i).Name
        End If
    Next i
    
    For i = 1 To Modules(strModName).CountOfLines
        strMod = strMod & Chr(10) & Modules(strModName).Lines(i, 1)
    Next i
        
    MsgBox (strMod)

End Sub
Now I just need to get rid of the loop that I use to find out the name of the module from which the code is run. Well I guess I don't need to, I just want to.

Thanks,
 
Upvote 0
Something I was thinking about is the Automatic Error handler built into the Developer version of Access. It throws a line of identifying text into the error handler routine it generates. What I was wondering is how to duplicate that.

One work-around might be to:
1) Identify the cursor position
2) Turn off screen refresh
3) Write some control value
4) Search for it (#3) to identify the given module/procedure

Mike
 
Upvote 0

Forum statistics

Threads
1,221,832
Messages
6,162,255
Members
451,757
Latest member
iours

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top