TheSwamp
Code Red => VB(A) => Topic started by: ML on October 24, 2007, 03:49:03 PM
-
Hi
Admitedly, I am not that good at writing functions; actually I suck at it :)
However, we needed to uncheck all checkboxs in a form at once, so instead of doing each one, one by one,
I managed to write this bit of code:
Dim Ctrl As Control
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is CheckBox Then
Ctrl.Value = False
End If
Next Ctrl
Now, realizing that I may use that several times over in this project, I thought it might be a good idea to try to write a little function for this.
Here is my first attempt
Private Function IsCheckBoxChecked(ByRef Checkbox As Control) As Boolean
Dim Ctrl As Control
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is Checkbox Then
IsCheckBoxChecked = True
Exit Function
End If
Next Ctrl
IsCheckBoxChecked = False
End Function
I am not sure if this is written correctly???
If not, can someone please help me out?
Also, how would I call it up in a Sub??
Thank you
Mark
-
First, I would make that a sub not a function - ONLY because I try to stick to subs do not return a value, functions do. Since you are simply doing something, not lookinig for an answer, I would use a sub.
Now on to the code, are you trying to clear the values by calling this? if so, I would just use what you posted first adding in the sub declaration
Private Sub Uncheck()
Dim Ctrl As Control
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is CheckBox Then
Ctrl.Value = False
End If
Next Ctrl
End Sub
I didn't test this, but if it worked on you other form it should work
-
As for calling it from a different sub,
Call Uncheck
-
Hey CM
I could use that code 20 times over but I would rather just make a call to the sub.
I guess I could make a seperate sub and say
RunMacro ("UncheckAll")
Thanks
Mark
-
And I would make the uncheck sub private to "hide" it from the outside world. (Not that you dont want anyone to know, just you cant call it from the command line so to speak)
-
... but I would rather just make a call to the sub.
Hence -> Call Uncheck
that is the call to the sub
-
put this
Private Sub Uncheck()
Dim Ctrl As Control
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is CheckBox Then
Ctrl.Value = False
End If
Next Ctrl
End Sub
at the bottom of your form code module, and call it from where ever you need from within the form code
-
or from Button code
Button1 click event might fire the uncheck, then do something else, kind of like a reset form button
-
HI CM
Ye, that is a good idea.
I will just create the module, make it Private and use Call when I need it.
Still, since, the checkbox value is based on a Boolean type (true or false) I would still be curious as to how to get the function workling. I think it would be great for future needs.
Also, it could be used for ALL (or most) controls
For example, if we want all textboxs enabled so that the users can not input data at a given time, we can say:
Private Sub Uncheck()
Dim Ctrl As Control
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is TextBox Then
Ctrl.Enabled = False
End If
Next Ctrl
End Sub
I have not tried that yet but common sense is telling me that it should work.
So again, the function would be cool and a lot of the controls are based on Boolean Values.
Mark