Using a Public Function to fire Enter and Exit Events

N

Nathan-bfhd

I have several forms in the same database where I am using Enter and Exit
events on the controls to show where the focus is. I'm guessing there is a
much easier/efficient way to do this. (Question - I inherited the database
and the original designer set the form up to show focus through the Enter and
Exit events. Is there a good reason to move the code to the GotFocus and
LostFocus events if the only thing they are doing is showing focus through a
font color and background change?)

I have looked into the 2 following methods of simplifying my task and making
my code more efficient, but have been unsuccessful so far. Since I don't
think the first option is likely available, I'm leaning toward accomplishing
the second.

1) Is there a simple way to assign a specific look to the font and
background of ALL text box and combo box controls upon getting focus?
Similar to the conditional formatting option, except in a way where I
wouldn't have to go through and touch each one?

2) What I have started to attempt but cannot figure out the syntax is to
write public functions (one for Enter and one for Exit OR for GotFocus and
LostFocus) in a module that allows me to make the font and background changes
I want to the control with the focus. The following is an example of the
code I am currently using a hundred different times in each of the Enter and
Exit events of my text boxes and combo boxes.

Private Sub Cell_txt_Enter()
Me![Cell_txt].ForeColor = 16777215
Me![Cell_txt].FontBold = True
Me![Cell_txt].BackColor = 16737843
End Sub

Private Sub Cell_txt_Exit(Cancel As Integer)
Me![Cell_txt].BackColor = 16777215
Me![Cell_txt].FontBold = False
Me![Cell_txt].ForeColor = 0
End Sub
 
M

Marshall Barton

Nathan-bfhd said:
I have several forms in the same database where I am using Enter and Exit
events on the controls to show where the focus is. I'm guessing there is a
much easier/efficient way to do this. (Question - I inherited the database
and the original designer set the form up to show focus through the Enter and
Exit events. Is there a good reason to move the code to the GotFocus and
LostFocus events if the only thing they are doing is showing focus through a
font color and background change?)

I have looked into the 2 following methods of simplifying my task and making
my code more efficient, but have been unsuccessful so far. Since I don't
think the first option is likely available, I'm leaning toward accomplishing
the second.

1) Is there a simple way to assign a specific look to the font and
background of ALL text box and combo box controls upon getting focus?
Similar to the conditional formatting option, except in a way where I
wouldn't have to go through and touch each one?

2) What I have started to attempt but cannot figure out the syntax is to
write public functions (one for Enter and one for Exit OR for GotFocus and
LostFocus) in a module that allows me to make the font and background changes
I want to the control with the focus. The following is an example of the
code I am currently using a hundred different times in each of the Enter and
Exit events of my text boxes and combo boxes.

Private Sub Cell_txt_Enter()
Me![Cell_txt].ForeColor = 16777215
Me![Cell_txt].FontBold = True
Me![Cell_txt].BackColor = 16737843
End Sub

Private Sub Cell_txt_Exit(Cancel As Integer)
Me![Cell_txt].BackColor = 16777215
Me![Cell_txt].FontBold = False
Me![Cell_txt].ForeColor = 0
End Sub


A fairly easy way to do this is to create the procedure's in
a standard module. Make them Public functions so thay can
be used anywhere.

Public Function CellEnter(frm As Form)
With frm!ActiveControl
.ForeColor = 16777215
.FontBold = True
.BackColor = 16737843
End With
End Function

Private Sub CellExit(frm As Form)
With frm!ActiveControl
.BackColor = 16777215
.FontBold = False
.ForeColor = 0
End With
End Function

With that done, then you can go back to a form and select
all the controls (by holding down the Shift key and clicking
each control). Then enter =CellEnter(Form) in the OnEnter
event property and =CellExit(Form) in the OnExit event
property to set all of them in one shot.
 

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