Sandra Smith
asked on
Me.Name not recognized in procedure
Ok, sometimes I get so frusrated as sonething works in one procedure but not antoher. I have a procedure that simply changes the color of a lable on a form. Simple, I want to pass the name of the form and the label, but is a lable considered a control? I have a sample of the calling procedure as well as the called procedure. This is an Access 2003 form.
Private Sub lblATM_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Call ChangeLabelColor(Me.Name, "lblATM")
End Sub
Public Sub ChangeLabelColor(frmName As Form, strLabelName As String)
'Changes color of label so user know it was pressed
Dim ctl As Control
For Each ctl In frmName.Controls
If ctl.Name = strLabelName Then
ctl.BackColor = 2858687
End If
Next ctl
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I will try that, be right back.
ASKER
Me alone worked. Thank you
Sandra
Sandra
imnorie is correct - you're using a form object in your sub. Passing the form's name won't work in this context.
ASKER
mbizup, that you for explaining that. I assumed Me.name was just passing the name of the form, not the object itself.
Sandra
Sandra
<<I assumed Me.name was just passing the name of the form, not the object itself.>>
You're actually understanding that 100% correctly.
"Me.Name" IS the name of the form.
"Me" is the form object itself
It just didn't work because....
>> Public Sub ChangeLabelColor(frmName As Form, strLabelName As String)
Your function requires a form object (not the name of the form) as a parameter.
An alternative writing of your function would be (highlighting the changes):
Public Sub ChangeLabelColor(frmName As String, strLabelName As String)
'Changes color of label so user know it was pressed
Dim ctl As Control
For Each ctl In Forms(frmName).Controls
If ctl.Name = strLabelName Then
ctl.BackColor = 2858687
End If
Next ctl
End Sub
That code would use the form name, not the object and your original calling statement would have worked with it.
You're actually understanding that 100% correctly.
"Me.Name" IS the name of the form.
"Me" is the form object itself
It just didn't work because....
>> Public Sub ChangeLabelColor(frmName As Form, strLabelName As String)
Your function requires a form object (not the name of the form) as a parameter.
An alternative writing of your function would be (highlighting the changes):
Public Sub ChangeLabelColor(frmName As String, strLabelName As String)
'Changes color of label so user know it was pressed
Dim ctl As Control
For Each ctl In Forms(frmName).Controls
If ctl.Name = strLabelName Then
ctl.BackColor = 2858687
End If
Next ctl
End Sub
That code would use the form name, not the object and your original calling statement would have worked with it.
You could rewrite the sub so it works with the label object as the argument passed.
Which you can call like this:
Call ChangeLabelColor(blATM)
Public Sub ChangeLabelColor(ctlLabel As Label)
ctl.BackColor = 2858687
End Sub
Which you can call like this:
Call ChangeLabelColor(blATM)
ASKER
imnorie, I like that concept, but wouldn't I still have to pass the form to the procedure knows whick form the control is on?
No, the control on it's own will do.
You are kind of passing the form anyway - it's the Parent of the control.
You are kind of passing the form anyway - it's the Parent of the control.
Try using this instead:
Me!Name
And if that is the case, avoid using Name and other reserved words for fieldnames, etc.