Link to home
Start Free TrialLog in
Avatar of Sandra Smith
Sandra SmithFlag for United States of America

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do you have a control or field somewhere on your form or in it's recordsource called "Name"?

Try using this instead:

Me!Name

And if that is the case, avoid using Name and other reserved words for fieldnames, etc.
Avatar of Sandra Smith

ASKER

I will try that, be right back.
Me alone worked.  Thank you

Sandra
imnorie is correct - you're using a form object in your sub.  Passing the form's name won't work in this context.
mbizup, that you for explaining that.  I assumed Me.name was just passing the name of the form, not the object itself.

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.
Avatar of Norie
Norie

You could rewrite the sub so it works with the label object as the argument passed.

Public Sub ChangeLabelColor(ctlLabel As Label)
    ctl.BackColor = 2858687     
End Sub

Open in new window


Which you can call like this:

Call ChangeLabelColor(blATM)
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.