By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,847 Members | 3,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,847 IT Pros & Developers. It's quick & easy.

VBA: Generic reference to ME

P: n/a
Greetings,

I would like to change the background color of fields as they are
entered for editing. (then, of course, change them back on exit)

My prototype consists of using a couple of global CONST called
EditColor and NormalColor.

I have event code as follows:

Private Sub LastName_Enter()
Me!LastName.BackColor = EditColor
End Sub

Private Sub LastName_Exit(Cancel as Integer)
Me!LastName.BackColor = NormalColor
End Sub

While this gives me the effect that Im looking for, it seems like there
has to be a more efficient way to do this. Im thinking that I should
be able to create two subroutines in a code module that can be called
from all the field events (Enter and Exit) and passing a reference to
the current field.

I would call them ColorMeEdit and ColorMeNormal for instance.

Then in the field Enter event, you would just call that subrountine.
That would dramtically reduce the amount of typing if nothing else.

But, I dont know how to pass a reference (to the current field) to the
subroutine.

Could someone show me code that would do this?

Thanks!

Nov 13 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
jv
The easiest way is to use confitional formating to change the
background when the control has focus.

But if you prefer a global subroutine then you can do something like:

Public sub ColorMeEdit()
With CodeContextObject
.ActiveControl.BackColor = EditColor
End With
End sub

Nov 13 '05 #2

P: n/a
Very cool. This is just what I was needing to know. Both suggestions
were right on target, and Im glad you pointed out the Conditional
Formatting. The only problem with this is that I cant get to the color
that Im wanting. It's suprising that you are only able to access the
basic colors. (Perhaps it is due to an interface preference that Im
not aware of.)

Anyway, Im off and running. Thanks again!

Nov 13 '05 #3

P: n/a
jv
No problem. You could also pass a reference of the form to the
subroutine.

Public sub ColorMe(frm as Form)
frm.ActiveControl.BackColor = Color
End sub

Call it with: Call ColorMeEdit(Me)

Nov 13 '05 #4

P: n/a
By modifying the FormatConditions object directly you can specify and
RGB color value you desire. Sample source code is here:
http://www.lebans.com/conditionalformatting.htm

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"Dave Klawiter" <kl******@nd.edu> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Very cool. This is just what I was needing to know. Both suggestions
were right on target, and Im glad you pointed out the Conditional
Formatting. The only problem with this is that I cant get to the color that Im wanting. It's suprising that you are only able to access the
basic colors. (Perhaps it is due to an interface preference that Im
not aware of.)

Anyway, Im off and running. Thanks again!


Nov 13 '05 #5

P: n/a
Another little hitch. All of this works fine on the main form, but
when I implemented it on a subform, it does not execute the field exit
event if I move out of the subform. Move to another field within the
subform and it works as expected. Do you have any idea why the field
exit event would run when moving from a subform to the main form? (or
to another subform... I have two subforms in the screen in question.)

Nov 13 '05 #6

P: n/a
Dave Klawiter wrote:
Another little hitch. All of this works fine on the main form, but
when I implemented it on a subform, it does not execute the field exit
event if I move out of the subform. Move to another field within the
subform and it works as expected. Do you have any idea why the field
exit event would run when moving from a subform to the main form? (or
to another subform... I have two subforms in the screen in question.)


Because the control in the subform still has the focus as far as that
form's concerned. Each form has a current control. You could try doing
something in the Exit event of the subform control.

--
[OO=00=OO]
Nov 13 '05 #7

P: n/a
jv
Try passing the control directly to the subroutine.
Public Sub ColorMe(ctl As Control)
ctl.BackColor = Color
End Sub

Call it with: Call ColorMe(ControlName).
Or you could change the above routine to a function and call by placing
the following in the OnGotFocus property: =ColorMe(ControlName)

Nov 13 '05 #8

P: n/a
jv,
I see this is yet another way to get the job done. Good alternatives
and I will keep in mind the idea that you can pass forms and controls
to routines in an argument.

I do understand what Trevor is saying. I also see that in fact, the
form's Exit event does occur so this might be the place to do that.
Using the code that jv has just above Call ColorMe(ControlName) I
tried this:

Call ColorMeNormal(Me.ActiveControl)

amoung other ways and just get an error 438: Object doesnt support this
property or method in the subroutine. That looks like this:

Public Sub ColorMeNormal(ctl as Control)
ctl.Backcolor = NormalColor
End Sub

So, obviously Im not referencing correctly. If I stop in the form Exit
event, how do I interrogate the system to determine what the
"ActiveControl" is?

I hope Im not missing something that's real obvious. I appreciate both
of your input on this.

Nov 13 '05 #9

P: n/a
jv
" how do I interrogate the system to determine what the ActiveControl
is? "

Place a breakpoint on the call line: Call
ColorMeNormal(Me.ActiveControl*)
When the breakpoint occur in runtime, open the Immediate Window
(Ctrl+G), and type in: ? me.activecontrol.name

Nov 13 '05 #10

P: n/a
jv
Or better yet, place a breakpoint on the line: ctl.Backcolor =
NormalColor
Then type the following in the immediate window: ? ctl.name

Nov 13 '05 #11

P: n/a
Hmm.... it returns the name of the form. I wonder if there is a way to
know where you came from? Trevor mentioned that the control still had
focus as far as that form is concerned so that is why it is still
colored in the Edit color. Bummer now...

Nov 13 '05 #12

P: n/a
Actually, there are so few controls on the subform, that maybe cycling
through them all and setting the color to Normal isnt a bad
alternative. It seems like you should have to do something like that
though.

Nov 13 '05 #13

P: n/a
jv
The only way that it would return the name if the form is if the form
was passed to the subroutine. Look in the main form to see if the
subroutine is called On Enter or On Exit of the subform.

Nov 13 '05 #14

P: n/a
Dave Klawiter wrote:

Call ColorMeNormal(Me.ActiveControl)

amoung other ways and just get an error 438: Object doesnt support this
property or method in the subroutine. That looks like this:


That's because the code is running in the main form, from there you'd
need Call ColorMeNormal(subformname.form.activecontrol) or something
like that.

--
[OO=00=OO]
Nov 13 '05 #15

P: n/a
Another way to acheive the different colour on the active control is
this (bit of bitch to set up in the first place though)

Set the backcolor to the colour you want to have when active.
Set backstyle to transparent
Put a rectangle behind the control in the colour you want the normal
background to be.
Nov 13 '05 #16

P: n/a
I got it working (sort of). I put code in the form exit event that
sets focus to a read-only field on the subform. This causes the
field's exit event to occur. It looks like it works right, but I
really wanted to have that read-only field disabled. As is, you can
click into the field as though you can edit the contents.

This brings up the question: How do I simply cause a field to exit,
leave (what?) active... Just the form, I guess. It would seem that is
not possible. In the GUI, you can not exit all fields, you have to
enter another field.

Nov 13 '05 #17

P: n/a
Dave Klawiter wrote:
I got it working (sort of). I put code in the form exit event that
sets focus to a read-only field on the subform. This causes the
field's exit event to occur. It looks like it works right, but I
really wanted to have that read-only field disabled. As is, you can
click into the field as though you can edit the contents.

This brings up the question: How do I simply cause a field to exit,
leave (what?) active... Just the form, I guess. It would seem that is
not possible. In the GUI, you can not exit all fields, you have to
enter another field.


Use a transparent button and set focus to that.

--
[OO=00=OO]
Nov 13 '05 #18

P: n/a
Bingo! Excellent idea. This solves the last issue... for now ;-)
Thanks for hanging in there with me Trevor and jv.

Nov 13 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.