Field Backcolor Changes

B

B F Cole

I have some forms with a large number of data entry fields. I'm using the
technique below to visually highlight where the user is on the form. This
highlights the label for the field and the data entry field. This is tedious
to code the On Enter and On Exit to turn on and off the highlighting for
every field. Is there a way to do this "globally" for the form? I'm using
default gray detail with white fields.

Public Const iCyan = 1234567 ' Whatever the number is for
cyan
Public Const iWhite = 12345678 ' Whatever the number is for
white
Public Const iGray = 123456789 ' Whatever the number is for
gray

' Set label and field to cyan
Private Sub Field1_Enter()
Me.LabelField1.Backcolor = iCyan
Me.Field1.Backcolor = iCyan
End Sub

' Return label and field to original colors
Private Sub Field1_Exit(Cancel as Integer)
Me.LabelField1.Backcolor = iGray
Me.Field1.Backcolor = iWhite
End Sub

Thanks for your usual fine suggestion,
Bill
 
G

Guest

This one is easy. You can put a slightly modified version of your code in a
standard module so it will be visible to all forms.

The way you call it is by putting the following directly in the Got Focus
and Lost Focus events of each control - Not in VBA, directly in the
Properties Box.

=EnterColors("lblMyLabel", True) 'Got Focus

=EnterColors("lblMyLabel", False) 'Lost Focus

You could eliminate the need to pass the name of the associated label
control if you are very consistent with your control naming. For example, if
each label control has exactly the same name except for the prefix, you could
tranform the name. The prefix for all controls have to be the same length.
My example shows 3, which is pretty usual.

txtLastName --- lblLastName
cboSocSec ---- lblSocSec
etc.

See below the way you can do that after the maine code

PublicFunction EnterColors(strLabel As String, blnEntering as Boolean)
Dim frm As Form
Dim ctl As Control
Dim ctlLbl As Control

Const iCyan As Long = 1234567 ' Whatever the number is for cyan
Const iWhite As Long = 12345678 ' Whatever the number is for white
Const iGray As Long = 123456789 ' Whatever the number is for gray

Set frm = Screen.ActiveForm
Set ctl = frm.ActiveControl
Set ctlLbl = frm.Controls(strLabel)

If blnEntering Then
ctl.Backcolor = iCyan
ctlLbl.Backcolor = iCyan
Else
ctl.Backcolor = iWhite
ctlLbl.Backcolor = iGrat
End If

Set frm = Nothing
Set ctl = Nothing
Set ctlLbl = Nothing

End Sub

' Return label and field to original colors
Private Sub Field1_Exit(Cancel as Integer)
Me.LabelField1.Backcolor = iGray
Me.Field1.Backcolor = iWhite
End Sub

Technique for transforming control name to get label name:

Set ctl = frm.ActiveControl
strLabel = ctl.Name
strLabel="lbl" & right(strLabel,len(strLabel)-3)
Set ctlLbl = frm.Controls(strLabel)
 
M

Marshall Barton

B said:
I have some forms with a large number of data entry fields. I'm using the
technique below to visually highlight where the user is on the form. This
highlights the label for the field and the data entry field. This is tedious
to code the On Enter and On Exit to turn on and off the highlighting for
every field. Is there a way to do this "globally" for the form? I'm using
default gray detail with white fields.

Public Const iCyan = 1234567 ' Whatever the number is for
cyan
Public Const iWhite = 12345678 ' Whatever the number is for
white
Public Const iGray = 123456789 ' Whatever the number is for
gray

' Set label and field to cyan
Private Sub Field1_Enter()
Me.LabelField1.Backcolor = iCyan
Me.Field1.Backcolor = iCyan
End Sub

' Return label and field to original colors
Private Sub Field1_Exit(Cancel as Integer)
Me.LabelField1.Backcolor = iGray
Me.Field1.Backcolor = iWhite
End Sub


You can specify a function in the OnEnter and OnExit
properties. The functions would look like:

Publc Function SetHighLight()
On Error ResumeNext
Me.ActiveControl.Backcolor = iCyan
Me.ActiveControl.Controls(0).Backcolor = iCyan
End Function

Publc Function ClearHighLight()
On Error ResumeNext
Me.ActiveControl.Backcolor = iWhite
Me.ActiveControl.Controls(0).Backcolor = iGray
End Function

Then the set the OnExit property of all data entry controls
(not labels, buttons, etc) to =SetHighLight() and OnExit to
=ClearHighLight()

There is a way to do this without using any code, but it is
not appropriate for a "large" number of controls.
 

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