Link to home
Start Free TrialLog in
Avatar of EMCIT
EMCIT

asked on

How do I write a Module to be called when a control on a form is clicked?

I have a form with a  number of text boxes on it. There is a list box for "Task". Depending on the task selected I want any of the text boxes clicked to change its back color. How can I write a module that can be called when a text box is clicked rather than write code within every individual text box on the form?
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Easy.
Select all the text boxes at once, then enter the name of the Function on the On Click event on the property sheet - see image.

mx
Capture1.gif
Create your Function either in the Form module or a regular vba module ... either way works.

mx
Avatar of EMCIT
EMCIT

ASKER

In the On-Click of each text box I need a module to call that will identify which control has been clicked, determine the appropriate color and backcolor that control.
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EMCIT

ASKER

On my form the list box is named WhichTask. When clicked the color for that task (eg-255) is held in a field called HoldColor. Then I have text boxes A, B, C, D, E, F, G, H, I, J. Based on this:

Public Sub mMyFX()
Dim x As String
Dim y As Variant

x=Screen.ActiveControl.Name  ' name of control clicked
y=Screen.ActiveControl  ' returns an object of the control clicked.

x.backcolor = Forms!Form1.HoldColor ' other code for color

End Function

Then on the OnClick for all the textboxes I put = mMyFX()

This looks like it should work but I'm getting an error: The expression you entered has a function name that MS Access can't find.

Avatar of EMCIT

ASKER

I got it! I had to change "Public Sub mMyFX()" to "Function mMyFX()"

Thanks much for the help!
Yes ... no can do Sub ... on Function when calling from prop sheet.  Which is one of the many reasons I never use Subs ... I just see no need.

You are welcome.
mx