Link to home
Start Free TrialLog in
Avatar of SidFishes
SidFishesFlag for Canada

asked on

Generic Onclick event for form of Checkboxes

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....
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
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.
Avatar of SidFishes

ASKER

hmmm getting a byRef error on Public Function clickListener() As Boolean

Works for me ... and tested.
You can drop the As Boolean.  And post exactly what you have.  

mx
byref error on x to be precise
bah ok that's a byref passing to the function args... Looks like it will work... back in a few
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 ;)
"I had to declare x,y & z individually"

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

mx