Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2011-10-05
7
Medium Priority
?
297 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:EMCIT
  • 4
  • 3
7 Comments
 
LVL 75
ID: 36918569
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
0
 
LVL 75
ID: 36918578
Create your Function either in the Form module or a regular vba module ... either way works.

mx
0
 
LVL 11

Author Comment

by:EMCIT
ID: 36918592
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 2000 total points
ID: 36918644

Sure.  Lets say this is your Function (you don't 'call' modules, you call Functions in Modules).

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.

' other code for color

End Function

mx
0
 
LVL 11

Author Comment

by:EMCIT
ID: 36918819
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.

0
 
LVL 11

Author Comment

by:EMCIT
ID: 36919384
I got it! I had to change "Public Sub mMyFX()" to "Function mMyFX()"

Thanks much for the help!
0
 
LVL 75
ID: 36919449
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
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question