Multiple Controls on one sub

kenneth.campos

Registered User.
Local time
Today, 17:24
Joined
Jan 2, 2010
Messages
11
I have 10 check boxes. I want all of them to run a function after update. Is there a way to put all 10 on one sub, instead of having to have a sub for each box?

Instead of

Code:
Sub Chk1_AfterUpdate()
Function
End Sub
Sub Chk2_AfterUpdate()
Function
End Sub
etc...
Could I have something like
Code:
Sub Chk1 through Chk10_AfterUpdate()
Function
End Sub
Thanks!
 
Well, not quite like that. But you can put a public function in a standard module and then in the form's On Open event you can assign that function to the checkboxes.

Code:
Public Function DoSomething(chk As Checkbox)
  ' do something here
End Function
Then in the On Open event of the form:
Code:
Private Sub Form_Open()
  Dim i As Integer
    For i = 1 To 10
       Me.Controls("chk" & i).AfterUpdate = "=DoSomething(chk" & i & ")"
    Next  
End Sub
 
Thank you for the reply. I ended up having to use the Load sub for the form. Also when referencing the controls I had to use
Code:
Me.Controls("chk" & Cstr(i)).Afterupdate
Other than that it works exactly as I wanted.
 
Forgot that I wanted to mention that I learned that method from our Moderator ChrisO.
 

Users who are viewing this thread

Back
Top Bottom