"Me" Variable in Property Sheet

  • Thread starter Thread starter Tom via AccessMonster.com
  • Start date Start date
T

Tom via AccessMonster.com

Is it possible to use the Me variable in the properties sheet for a command
button? For example: I want to use :

=ChangeFormColor([Me])

as the On Click event in the properties window instead of:

=ChangeFormColor([Forms]![frmMainForm]).

The function ChangeFormColor is:

Function ChangeFormColor(frm As Form)
On Error Resume Next
frm.Detail.BackColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256)
End Function

Thanks in advance.

Tom
 
Either use VBA code to call the function (without the square brackets around
Me), or try changing your function to:

Function ChangeFormColor(Optional frm As Variant)
On Error Resume Next
If IsMissing(frm) Then
Set frm = Screen.ActiveForm
End If
frm.Detail.BackColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256)
End Function

and simply use =ChangeFormColor() when you want to change the current form.
 
Yep, it works. Looks pretty cool. I did not put the square brackets around
Me. Curious as to why you made it a function if it returns nothing, why not
a Sub? It works fine as a sub.
 
Tom via AccessMonster.com said:
Is it possible to use the Me variable in the properties sheet for a
command button? For example: I want to use :

=ChangeFormColor([Me])

as the On Click event in the properties window instead of:

=ChangeFormColor([Forms]![frmMainForm]).

The function ChangeFormColor is:

Function ChangeFormColor(frm As Form)
On Error Resume Next
frm.Detail.BackColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256)
End Function

Thanks in advance.

Tom

I've always done this by referring to the form's Form property, as in:

=ChangeFormColor([Form])

If [Me] works, it's news to me.
 
You can't put a Sub as an Event property, whereas you can put a Function
there.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Klatuu said:
Yep, it works. Looks pretty cool. I did not put the square brackets around
Me. Curious as to why you made it a function if it returns nothing, why not
a Sub? It works fine as a sub.

Tom via AccessMonster.com said:
Is it possible to use the Me variable in the properties sheet for a command
button? For example: I want to use :

=ChangeFormColor([Me])

as the On Click event in the properties window instead of:

=ChangeFormColor([Forms]![frmMainForm]).

The function ChangeFormColor is:

Function ChangeFormColor(frm As Form)
On Error Resume Next
frm.Detail.BackColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256)
End Function

Thanks in advance.

Tom
 
Jeff: Using the Me variable returns a error message "The object doesn't
contain the Automation object 'Me'."

Dirk & Douglas: Both of your suggestions work. Thanks.

Tom



Dirk said:
Is it possible to use the Me variable in the properties sheet for a
command button? For example: I want to use :
[quoted text clipped - 15 lines]

I've always done this by referring to the form's Form property, as in:

=ChangeFormColor([Form])

If [Me] works, it's news to me.
 
ChangeFormColor would not be a valid name for an event function.

Douglas J Steele said:
You can't put a Sub as an Event property, whereas you can put a Function
there.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Klatuu said:
Yep, it works. Looks pretty cool. I did not put the square brackets around
Me. Curious as to why you made it a function if it returns nothing, why not
a Sub? It works fine as a sub.

Tom via AccessMonster.com said:
Is it possible to use the Me variable in the properties sheet for a command
button? For example: I want to use :

=ChangeFormColor([Me])

as the On Click event in the properties window instead of:

=ChangeFormColor([Forms]![frmMainForm]).

The function ChangeFormColor is:

Function ChangeFormColor(frm As Form)
On Error Resume Next
frm.Detail.BackColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256)
End Function

Thanks in advance.

Tom
 
I liked the routine you wrote and thought it might be interesting to use it
for getting a user's attention if there is an error, so I embellished it
little.

Sub FlashFormColor(frm As Form, lngDuration As Integer, lngSpeed As Long)
Dim lngOldColor As Long
Dim lngCtr As Long
On Error Resume Next
lngOldColor = frm.Detail.BackColor
For lngCtr = 0 To lngDuration
frm.Detail.BackColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256)
frm.Repaint
Call sSleep(lngSpeed)
Next lngCtr
frm.Detail.BackColor = lngOldColor

End Sub


Tom via AccessMonster.com said:
Jeff: Using the Me variable returns a error message "The object doesn't
contain the Automation object 'Me'."

Dirk & Douglas: Both of your suggestions work. Thanks.

Tom



Dirk said:
Is it possible to use the Me variable in the properties sheet for a
command button? For example: I want to use :
[quoted text clipped - 15 lines]

I've always done this by referring to the form's Form property, as in:

=ChangeFormColor([Form])

If [Me] works, it's news to me.
 
Why do you say that?

I tested the code I suggested to Tom, and it worked fine putting
=ChangeFormColor() in the form's Property Sheet for the Click event of a
button.

An event property can be set to any of:
1) Macro (use "macroname" as the property)
2) Event procedure (use "[Event Procedure]" as the property, and then have a
corresponding Sub in the form's class)
3) User-defined function (use something like "=functionname( )" as the
property)


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Klatuu said:
ChangeFormColor would not be a valid name for an event function.

Douglas J Steele said:
You can't put a Sub as an Event property, whereas you can put a Function
there.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Klatuu said:
Yep, it works. Looks pretty cool. I did not put the square brackets around
Me. Curious as to why you made it a function if it returns nothing,
why
not
a Sub? It works fine as a sub.

:

Is it possible to use the Me variable in the properties sheet for a command
button? For example: I want to use :

=ChangeFormColor([Me])

as the On Click event in the properties window instead of:

=ChangeFormColor([Forms]![frmMainForm]).

The function ChangeFormColor is:

Function ChangeFormColor(frm As Form)
On Error Resume Next
frm.Detail.BackColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256)
End Function

Thanks in advance.

Tom
 
Of course it will work if you use it the way you describe. Would you really
do it that way?

Douglas J Steele said:
Why do you say that?

I tested the code I suggested to Tom, and it worked fine putting
=ChangeFormColor() in the form's Property Sheet for the Click event of a
button.

An event property can be set to any of:
1) Macro (use "macroname" as the property)
2) Event procedure (use "[Event Procedure]" as the property, and then have a
corresponding Sub in the form's class)
3) User-defined function (use something like "=functionname( )" as the
property)


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Klatuu said:
ChangeFormColor would not be a valid name for an event function.

Douglas J Steele said:
You can't put a Sub as an Event property, whereas you can put a Function
there.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Yep, it works. Looks pretty cool. I did not put the square brackets
around
Me. Curious as to why you made it a function if it returns nothing, why
not
a Sub? It works fine as a sub.

:

Is it possible to use the Me variable in the properties sheet for a
command
button? For example: I want to use :

=ChangeFormColor([Me])

as the On Click event in the properties window instead of:

=ChangeFormColor([Forms]![frmMainForm]).

The function ChangeFormColor is:

Function ChangeFormColor(frm As Form)
On Error Resume Next
frm.Detail.BackColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256)
End Function

Thanks in advance.

Tom
 
Klatuu said:
Of course it will work if you use it the way you describe. Would you
really do it that way?

Sure, I do that quite often. It's a handy way to attach common code to
events, without having to build a separate event procedure for each
object event. And it's convenient to be able to select a bunch of
controls, open their joint property sheet, and enter a function
expression into the same event property for all the controls at once.
The technique also allows you to build "lightweight" (code-free) forms
that still have user-programmed responses to events, although the
practical value of lightweight forms is somewhat debatable.
 
Back
Top