code problem

eugz

Registered User.
Local time
Today, 15:57
Joined
Aug 31, 2004
Messages
128
Hi All.
Access2003. In my code:
Code:
    If NameOfActiveControl = Me(NameOfControl).Name Then
        Exit Sub
    End If
I got error msg Ivalid use of Me keyword. What is wrong?
Thanks.
 
Should be Me!nameofcontrol.name
 
Thanks for replay.
I did like you suggest and got same error msg.
 
Where are you getting NameofActiveControl from? Can you let us see alittle more of your code soi we can seewhat you are trying to do.
 
And if the code is in a standard module rather than a form/report module, you'll get that error.
 
That is Module code to change color of caption of button
Code:
Option Compare Database
Option Explicit

Dim NameOfActiveControl As String
'Private Const ActiveForeColor As Long = 16711680 'color contents button

Public Sub ChangeColor(ActiveForeColor As Long, _
                        NameOfActiveControl As String, _
                        NameOfControl As String)
'If name active and current the same yet then to exit....
    If NameOfActiveControl = Me(NameOfControl).Name Then
        Exit Sub
    End If
'If some control was activated before so deactivating it
    If Len(NameOfActiveControl) > 0 Then
        Me(NameOfActiveControl).ForeColor = -2147483630
    End If
'Set name for new active control
    NameOfActiveControl = Me(NameOfControl).Name
    Me(NameOfControl).ForeColor = ActiveForeColor
'Change color of contents
    Me(NameOfActiveControl).SetFocus
'Set focus on button

End Sub
 
Me(NameOfControl).Name is a valid construct, but...

Say the name of the control is txtMyControl, and you stuff that into the variable NameOfControl, then

? Me(NameOfControl).Name
? NameOfControl

wouldn't they be the same? It should be sufficient to test against the NameOfControl variable/parameter, and not necessary to go through the controls collection.

If you need to manipulate forms stuff in a general module, you'd need to reference the form somehow. Say, in stead of

Me(NameOfControl).ForeColor

You'd need

Forms(NameOfForm).Controls(NameOfControl).ForeColor

or similar contsructs, or, if you passed the form, you could use that

Public Sub MySub(ByRef frm as Form)
frm.visible = false ' just to do something
End sub
 
As Paul reminded us you can only use Me in a form/report module. Me is a shorthand way of referring to the current form/report. I have tested and it accepted Me when it was in the Form module but not in a general module.
 

Users who are viewing this thread

Back
Top Bottom