Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Capture running code name

Status
Not open for further replies.

dendic

Programmer
Jan 19, 2003
106
US
Is there a method to capture the name of the code running. So I can save it to variable for later use.
 

What exactly do you mean by the "name of the code?"

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
This may help:

As the article points out you can not capture the procedure name through code, but you can do a work around. I use this concept for debugging. However, IMHO saving a procedure name as a variable and then using the EVAL , docmd.openfunction, or application.run method are work arounds for poor programming design.
 
How are ya dendic . . .

Your post can only be related to troubleshooting, and [purple]a bad way to go about it at that[/purple]. Access has more advanced ways to do this if you learn and use them. If for instance a routine/function is called 100's or 1000's of times, the method you seek fails miserably.

Learn to use breakpoints and stepping thru code, as well as using the error object. Aside from this . . . [blue]programmers can troubleshoot no better than they know their own code! . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 
On my form I do a double click to run code. The procudure name is sub lbltest9_DblClick for example. Because my control is a label I can't do Screen.ActiveControl to get the calling control name therefore if I can save the code name in a variable I can then determine what label was clicked. The reason for this is because I have 200 labels on a form and name of the label is tied to a record in a table so I do some processing based on the label clicked.
 
That makes even less sense to me. If you can save the code name in a variable, why can you not save the control name in a variable?

IMHO if I was going to have a form with 200 labels that all triggered events I would definately build a custom class module. Using with events, I could build a single event to trap any click of a label instead of 200 events. I would probably also build a custom collection to manage these labels and load them into the collection on form load.
 
MajP makes a great point....I like his idea.

That being said, you could also do something like this (a bit easier to do since you may not have the experience or technical knowhow for what MajP suggests).

Create a module with something like this :

Code:
Public Sub NameControl(lblClicked As Label)

    MsgBox (lblClicked.Name)
    ' Your code goes here to do whatever based on the label that was clicked

End Sub

Then, in each of the Label's DoubleClick events, you would need something like:

Code:
Private Sub Label0_DblClick(Cancel As Integer)

    Call NameControl(Me.Label0)

End Sub

You only need to change the Me.Label0 for each label to pass the correct label through. This also exposes the properties of your label to the module code for use - such as text, location, etc...

This is nowhere are graceful or as powerful as MajP's suggestion...just something that might be easier to use right away while you learn...

Hope it helps somewhat...

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
This example may get you started with building a custom class. Without building the custom collection you do not see the true power of this approach. With the custom collection, I can iterate through and set all 200 objects. Here is the custom class call "ClickLabel".

Code:
Option Compare Database
Option Explicit

Private WithEvents mLabel As Access.Label
Private mRecordID As Long

Public Property Get RecordID() As Long
  RecordID = mRecordID
End Property
Public Property Let RecordID(ByVal lngRecordID As Long)
  mRecordID = lngRecordID
End Property
Public Property Get ClickLabel() As Access.Label
  Set ClickLabel = mLabel
End Property
Public Property Set ClickLabel(ByVal lblClickLabel As Access.Label)
  Set mLabel = lblClickLabel
  mLabel.OnClick = "[Event Procedure]"
  mLabel.OnMouseUp = "[Event Procedure]"
 End Property
Private Sub mLabel_Click()
  'run code here
  MsgBox "You clicked label " & mLabel.Name
  If mLabel.ForeColor = vbRed Then
    mLabel.ForeColor = vbGreen
  Else
    mLabel.ForeColor = vbRed
  End If
End Sub
Private Sub mLabel_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  MsgBox "You moused up from " & mLabel.Name & "It is associated with record" & mRecordID
End Sub

now I built a form with five labels and used this class.

Code:
Option Compare Database
Private click1 As New ClickLabel
Private click2 As New ClickLabel
Private click3 As New ClickLabel
Private click4 As New ClickLabel
Private click5 As New ClickLabel
Private Sub Form_Load()
  Set click1.ClickLabel = Me.Label0
  click1.RecordID = 1
  Set click2.ClickLabel = Me.Label1
  click2.RecordID = 2
  Set click3.ClickLabel = Me.Label2
  click3.RecordID = 3
  Set click4.ClickLabel = Me.Label3
  click4.RecordID = 4
  Set click5.ClickLabel = Me.Label4
  click5.RecordID = 5
End Sub

On the form there are no events; however, when I mouse down and click on any label on the form the class traps the events. If you are interested drop the class module code into a class module named 'ClickLabel'. Build a form with 5 labels named label0 to label4. Instantiate the objects with the form code.
 
This is getting more complicated than I expected. All I'm trying to do is click on a form label and get the name of the label in the on_click code.
 
Maybe you are not describing the problem clearly. But this is what I understand. You have a label for example "lbltest9". Associated with this is the event "lbltest9_DblClick". So now I do not understand what the issue is. You know the name of the label and can hard code it like mstrmage said.

public strLblSelected as string

private sub lblTest9_DblClick()
strLblSelected = "lblTest9"
rest of your code
end sub

If this is not what you are asking, can you explain why you think you can get the procedure name and save it, but can not get the label's name? Clicking the label triggers the code so obviously you should be able to just save the label name to a variable.
 
I did something similar a few years back so I tried to recall what I had done.

I was able to get this to work with a command button but not a label for some reason. (Same problem before if I recall)

I also run into a problem when I try to place the control name in a textbox since it wants to put in the textboxes name so I had to use a combobox to hold the value

I created a Module with a function Called GetCommandCaption

Code:
Public Function GetCommandCaption()
[COLOR=green]'Forms![frmLabelTest]!txtControlName.SetFocus
'Forms![frmLabelTest]!txtControlName.Text = ""[/color]
Forms![frmLabelTest]!cboTest1.Value = Screen.ActiveControl.Name
[COLOR=green]'Forms![frmLabelTest]!cboTest1.Value = Screen.ActiveControl.Caption[/color]
End Function

I then created a macro that "runscode" and calls the function.

Then, in the click event, I enter "GetCommandCaption" and it sets the value for the combobox to the name or caption of the command button.

In my other program I used a control array of command buttons since I had 144 of them.

Note: You will not see Screen.ActiveControl.Caption or Name with intelisence but it still works.
 
I played with my code I posted last night and I can get the name, Tag and caption into the txtbox. (I was changing the control focus to the textbox and then getting the textbox name.

When I click a label I get an error stating the label "does not support that proprty or method"

The command button will pass the various values.

Here are my changes.

Code:
Public Function GetCommandCaption()
On Error GoTo ErrHandler
Dim sControlName As String
Dim sControlCaption As String
Dim sControlTag As String

sControlName = Screen.ActiveControl.Name
sControlCaption = Screen.ActiveControl.Caption
sControlTag = Screen.ActiveControl.Tag

Forms![frmLabelTest]!cboTest1.Value = sControlName

Forms![frmLabelTest]!txtControlName.SetFocus
Forms![frmLabelTest]!txtControlName.Text = sControlCaption


Exit Function
ErrHandler:
    MsgBox "Error in Function GetCommandCaption. Error # " & Err.Number & ", " & Err.Description, vbOKOnly, "Error"
End Function

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top