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

Microsoft DevelopmentMicrosoft ApplicationsMicrosoft Access

Avatar of undefined
Last Comment
Norie
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of mbizup
mbizup
Flag of Kazakhstan image

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
Sandra Smith
Flag of United States of America image

ASKER

I will try that, be right back.
Avatar of Sandra Smith
Sandra Smith
Flag of United States of America image

ASKER

Me alone worked.  Thank you

Sandra
Avatar of mbizup
mbizup
Flag of Kazakhstan image

imnorie is correct - you're using a form object in your sub.  Passing the form's name won't work in this context.
Avatar of Sandra Smith
Sandra Smith
Flag of United States of America image

ASKER

mbizup, that you for explaining that.  I assumed Me.name was just passing the name of the form, not the object itself.

Sandra
Avatar of mbizup
mbizup
Flag of Kazakhstan image

<<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)
Avatar of Sandra Smith
Sandra Smith
Flag of United States of America image

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

No, the control on it's own will do.

You are kind of passing the form anyway - it's the Parent of the control.
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo