473,396 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

VBA: Generic reference to ME

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
18 4000
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Corepaul | last post by:
I am new to Access 2000. My operating system is Windows 2000. In the early stage of development I noticed something weird. On my form, I have a Command Button named "btnAlbumUp". The first time...
4
by: Ed Landau | last post by:
In VB6.0, I can put an image on a form and assign it's .picture property at run-time. In VBA (within MS Access), when I put down an image control on a form, it asks me for the source to the image...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
1
by: S. van Beek | last post by:
Dear reader, I can "able" and "unable" (disable) reference libraries in the reference form of VBA with Tools/References.. But is there code available in VBA to disable a missing...
3
by: S. van Beek | last post by:
Dear reader, I still have a problem with my reference libraries. In my frond end application a check procedure for missing references is available. The problem I confronted with is that...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
3
by: zufie | last post by:
I am trying to fix the following VBA code from an MS Access database I inherited. I conclude with an example of one of the symptoms resulting from this VBA code: Option Compare Database...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.