Running a subroutine from many events

  • Thread starter Thread starter Dennis Snelgrove
  • Start date Start date
D

Dennis Snelgrove

I've got a number of forms that are very dense with controls, so I made a
simple routine ("BackColours") that changes the backcolor of the currently
active control to Yellow to make it easy for the encoders to see where they
are. It also runs through all the controls and resets all others to a White
background. I need this routine to run on the GotFocus event of each
control, but there's go to be an easier way than to have 25 different events
all with the same subroutine call. Since we don't have a Control Array
available to us, can anyone make a suggestion to get this to work without
the 25 Event Procedures that I currently have to use?

Thanks...

Dennis
 
Change the sub to a function.

Select all the controls that you want to use this function (either by
dragging the mouse or with ctrl + click). With all the controls selected,
put the name of the function in the GotFocus event field in the format
"=NameOfFunction()" (without the quotation marks).
 
In Access, if i remember correctly, there is a way on the Property page of
the control to specifiy the sub for function you want to fire. You don't
have to take the default.
 
Set up your routine as a function in a standard module:
Function BackColors()
<<Do Your Thing Here>>
End Function

Then go to each form in design view and select all the controls at once by
holding down the shift key as you click on each control. Open properties and go
to the GotFocus event. Enter the following:
=BackColors()
Close the form.

Each control you selected will now run the routine.
 
Not sure if this helps, but you might also consider conditional formatting on the Main Menu -> Format option, and then everything is handled by acces without code.
 
Back
Top