[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

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

0
ssmith94015
Asked:
ssmith94015
  • 4
  • 3
  • 3
1 Solution
 
NorieCommented:
Just pass Me, not Me.Name.
Call ChangeLabelColor(Me, "lblATM")

Open in new window

0
 
mbizupCommented:
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.
0
 
ssmith94015Author Commented:
I will try that, be right back.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
ssmith94015Author Commented:
Me alone worked.  Thank you

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

Sandra
0
 
mbizupCommented:
<<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.
0
 
NorieCommented:
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)
0
 
ssmith94015Author Commented:
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?
0
 
NorieCommented:
No, the control on it's own will do.

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

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now