Solved

Generic Onclick event for form of Checkboxes

Posted on 2011-09-08
10
497 Views
Last Modified: 2013-11-27
My vba skills are a little rusty these days so...

I have a form with 40 checkboxes. on update of any one of these I want to fire an event which runs a function

fnUpdateIngredAllergen Me.IngredientID, Me.ActiveControl.Name, Me.ActiveControl.value

which passes that info to a stored procedure.

Currently I have

Private Sub chkDairyCont_Click()
fnUpdateIngredAllergen Me.IngredientID, Me.ActiveControl.Name, Me.ActiveControl.value
end sub

for each checkbox. Well I've actually just got it set for the one right now..

My question is: is there a way to avoid coding that OnClick for every control. Is there something that can determine which control was clicked (ala Me.ActiveControl.Name) and fire a generic event.

ie
function clickListenerTriggeredbySomething()

fnUpdateIngredAllergen Me.IngredientID, magicallydeterminecontrolthatwasclicked, controlthatwasclicked.value

end function

I'm thinking no....
0
Comment
Question by:SidFishes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 75

Accepted Solution

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


Create a Function similar to this:

Public Function clickListenerTriggeredbySomething() As Boolean

    Dim x, y, z
    x = Screen.ActiveForm("IngredientID")
    y = Screen.ActiveControl.Name
    z = Screen.ActiveControl.value
   
End Function

Then, directly from the Event property sheet for each text box ... call the Function - see image.  You can select ALL checkboxes first, then paste in the Function name - see image.

mx


Capture1.gif
0
 
LVL 77

Expert Comment

by:peter57r
ID: 36504636
You can select all checkboxes at once (using shift key or by dragging the mouse) and display the properties- you can then enter the expression just once and it will be applied to all the selected controls.
0
 
LVL 75
ID: 36504672
more detail ...

mx
Capture1.gif
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 36

Author Comment

by:SidFishes
ID: 36504698
hmmm getting a byRef error on Public Function clickListener() As Boolean

0
 
LVL 75
ID: 36504740
Works for me ... and tested.
You can drop the As Boolean.  And post exactly what you have.  

mx
0
 
LVL 36

Author Comment

by:SidFishes
ID: 36504755
byref error on x to be precise
0
 
LVL 36

Author Comment

by:SidFishes
ID: 36504774
bah ok that's a byref passing to the function args... Looks like it will work... back in a few
0
 
LVL 75
ID: 36504783
Yes ... get rid of ByRef ...

mx
0
 
LVL 36

Author Comment

by:SidFishes
ID: 36504867
I had to declare x,y & z individually so they would pass to the function which fixed the byRef (or I guess I could have removed the declarations in the function but I'd rather be explicit)

Public Function clickListener()
   Forms.frmIngredientAllergens.SetFocus
    Dim x As Integer, y As String, z As Boolean
 
    x = Screen.ActiveForm("Ingredientid")
    y = Screen.ActiveControl.Name
    z = Screen.ActiveControl.value
   
   fnUpdateIngredAllergen x, y, z
   
End Function

good stuff

My ctrl-c & ctrl-v thank you ;)
0
 
LVL 75
ID: 36505008
"I had to declare x,y & z individually"

Sure ... what I did was just ... 'conceptual' :-)

mx
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

636 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