Author Topic: If Checkbox is Checked Function  (Read 1383 times)

0 Members and 1 Guest are viewing this topic.

ML

  • Guest
If Checkbox is Checked Function
« 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:
Code: [Select]
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
Code: [Select]
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


CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: If Checkbox is Checked Function
« Reply #1 on: October 24, 2007, 03:56:55 PM »
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
Code: [Select]
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
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: If Checkbox is Checked Function
« Reply #2 on: October 24, 2007, 03:57:47 PM »
As for calling it from a different sub,   
Code: [Select]
Call Uncheck
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

ML

  • Guest
Re: If Checkbox is Checked Function
« Reply #3 on: October 24, 2007, 04:00:00 PM »

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

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: If Checkbox is Checked Function
« Reply #4 on: October 24, 2007, 04:00:50 PM »
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)
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: If Checkbox is Checked Function
« Reply #5 on: October 24, 2007, 04:02:04 PM »
... but I would rather just make a call to the sub.
Hence -> Call Uncheck
that is the call  to the sub
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: If Checkbox is Checked Function
« Reply #6 on: October 24, 2007, 04:03:19 PM »
put this
Code: [Select]
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
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: If Checkbox is Checked Function
« Reply #7 on: October 24, 2007, 04:04:06 PM »
or from Button code
Button1 click event might fire the uncheck, then do something else, kind of like a reset form button
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

ML

  • Guest
Re: If Checkbox is Checked Function
« Reply #8 on: October 25, 2007, 10:22:44 AM »

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:

Code: [Select]
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