Control Background Color

J

John Lane

Is there a way entirely change the background color when a control has focus?
That is, the field I have is green backgound with black forecolor. I changed
the background on the fly to red, but the when the field has focus, data in
it was surrounded by black, with red characters. The rest of the field is
red. What I want is the black to go away and leave the white letters.
 
S

Scott Whetsell, A.S. - WVSP

Using the GotFocus and LostFocus events would be my suggestion.

Private Sub ControlName_GotFocus()
Me.ControlName.BackColor = vbBlack
Me.ControlName.ForeColor = vbRed
End Sub

Private Sub ControlName_LostFocus()
Me.ControlName.BackColor = vbGreen
Me.ControlName.ForeColor = vbBlack
End Sub

Change ControlName as appropriate and you can use any numeric color value in
place of the vb colors. I believe the most common colors and be used as I
showed above.
 
L

Linq Adams via AccessMonster.com

Scott's code will work for Single View forms, but not for Datasheet or
Continuous View forms. Simpler to do, and workable for all types of forms is
to use Conditional Formatting off the toolbar.

In Design View, select the control, then goto Format - Conditional Formatting

Under Condition select "Field has focus"

Now simply set the fore and background colors you want.
 
J

John Lane

The only thing wrong with these solutions is what happens when you put the
cursor (ie., has focus) in the control - you get three colors - the color you
set it to, then a reverse set of colors of the actual string of data - I
guess the form software is trying to highlight and reverse video the
character string - so you can end up with three colors!
 
L

Linq Adams via AccessMonster.com

So you don't want the data to be selected on entering the field. There's a
couple of ways you can handle this.

If you want this to be the default behavior for all fields on all forms, in
Design View you can

Goto Tools - Options - Keyboard and set "Behavior on entering field" to
either go to the beginning of the field or go to the end of the field.

If this doesn't suit, you can do it on a field-by-field basis:

To set it to the end of the data:

Private Sub YourTextboxName_GotFocus()
YourTextboxName.SelStart = Len(Me.YourTextboxName)
End Sub

and

Private Sub YourTextboxName_Click()
YourTextboxName.SelStart = Len(Me.YourTextboxName)
End Sub

To set it to the beginning of the dat, replace

YourTextboxName.SelStart = Len(Me.YourTextboxName)

with

YourTextboxName.SelStart = 0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top