473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

CheckBoxALL to Auto Check 52 other checkboxes Loop Question

kcdoell
230 100+
I have a list of states on a form, in which the user can click on various states that are applicable to the form. There is a "ALL State" option (CheckBoxALL) in which if selected all of the state checkboxes will be checked. Below is the following code:



Private Sub CheckBoxALL_Click()



If CheckBoxAll = True Then

CheckBoxState01 = True
CheckBoxState02 = True
CheckBoxState03 = True
CheckBoxState04 = True
etc.....
CheckBoxState52 = True

Else

CheckBoxState01 = False
CheckBoxState02 = False
CheckBoxState03 = False
CheckBoxState04 = False
CheckBoxState05 = False
CheckBoxState06 = False
etc.........CheckBoxState52 = False

End If

End Sub

_______________________

My question is if there is a cleaner way to write this code than the way I wrote it since I am still in VBA learning mode?

I tried something like this but it keeps error out:

Private Sub CheckBoxALL_Click()

Dim CheckALL As Boolean

Set CheckALL = ActiveDocument.FormFields("CheckBoxALL").CheckBox. Value

If CheckALL("CheckBoxALL").CheckBox.Value = True Then

For i = 1 To 52
CheckALL("CheckBoxState0" & i).CheckBox.Value = True
Next i

Else

For i = 1 To 52
CheckALL("CheckBoxState0" & i).CheckBox.Value = False
Next i

End If
End Sub

_________________


My result so far has been compile errors and coding that does not work. Does anybody have any ideas??

Thanks,



Keith.
Jan 14 '08 #1
13 3374
FishVal
2,653 Expert 2GB
Hi, Keith.

You may use the following code.
Expand|Select|Wrap|Line Numbers
  1. For i = 1 To 52
  2.     Me.Controls("CheckBox" & Format(i, "00")).Value = True
  3. Next i
  4.  
Just out of curiosity. Does that mean you have a table with 52 Yes/No fields?

Regards,
Fish
Jan 14 '08 #2
missinglinq
3,532 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. Private Sub CheckBoxAll_AfterUpdate()
  2.  
  3. Dim ctl As Control
  4.  
  5. If Me.CheckBoxAll = -1 Then
  6.  
  7.  For Each ctl In Me.Controls
  8.  
  9.    If ctl.ControlType = acCheckBox Then
  10.  
  11.      ctl = -1
  12.  
  13.    End If
  14. Next
  15.  
  16. Else
  17.  
  18. If Me.CheckBoxAll = -1 Then
  19.  
  20.  For Each ctl In Me.Controls
  21.  
  22.    If ctl.ControlType = acCheckBox Then
  23.  
  24.      ctl = 0
  25.  
  26.   End If
  27.   Next
  28. End If
  29. End Sub
Linq ;0)>
Jan 14 '08 #3
missinglinq
3,532 Expert 2GB
Even better, Fish, when the controls are sequentially named! But the checkbox names are "CheckBoxState" & i so

Expand|Select|Wrap|Line Numbers
  1. Me.Controls("CheckBoxState" & Format(i, "00")).Value = True
Linq ;0)>
Jan 14 '08 #4
FishVal
2,653 Expert 2GB
Even better, Fish, when the controls are sequentially named! But the checkbox names are "CheckBoxState" & i so

Expand|Select|Wrap|Line Numbers
  1. Me.Controls("CheckBoxState" & Format(i, "00")).Value = True
Linq ;0)>
Expand|Select|Wrap|Line Numbers
  1. Private Sub CheckBoxAll_AfterUpdate()
  2.  
  3.     Dim ctl As Control
  4.  
  5.     With Me
  6.         For Each ctl In .Controls
  7.            If ctl.ControlType = acCheckBox Then ctl = .CheckBoxAll
  8.         Next
  9.     End With
  10.  
  11.     Set ctl = Nothing
  12.  
  13. End Sub
  14.  
Eye for eye, Linq. LOL.

Best regards,
Fish
Jan 14 '08 #5
kcdoell
230 100+
Thanks for all the input... I just go back out of a meeting....


This is a form with a section in it for people to pick (CheckBoxes) the applicable state(s). I failed to mention that it was a form created out of Word 2003. That seems to make a difference because it does not recognize the command "Controls" (VBA error message:"Method or data member not found"). Does that mean that it can not be done if this form was created in Word or I would have to use some other command.....

Thanks,

Keith.
Jan 14 '08 #6
FishVal
2,653 Expert 2GB
Ok, Keith.

Try the following code.
Expand|Select|Wrap|Line Numbers
  1. Private Sub CheckBoxAll_Click()
  2.  
  3.     Dim fldField As Field
  4.  
  5.     For Each fldField In ThisDocument.Fields
  6.         If TypeName(fldField.OLEFormat.Object) = "CheckBox" Then _
  7.             fldField.OLEFormat.Object.Value = ThisDocument.CheckBoxAll
  8.     Next
  9.  
  10.     Set fldField = Nothing
  11.  
  12. End Sub
  13.  
Jan 14 '08 #7
kcdoell
230 100+
Hello:

I tried that and got the following error:

Run time 91 "Object Variable or with Block Variable not Set"

I don't really understand what the code is doing to interpret the error message. Any ideas?

Thanks,

Keith
Jan 14 '08 #8
FishVal
2,653 Expert 2GB
Well. You may send me it via email. I will PM you my email address.
Jan 15 '08 #9
kcdoell
230 100+
Well. You may send me it via email. I will PM you my email address.

Fish did you get it?? I sent it to you yesterday.

Thank,

Keith.
Jan 16 '08 #10
FishVal
2,653 Expert 2GB
Fish did you get it?? I sent it to you yesterday.

Thank,

Keith.
Hello, Keith.

Sorry for delay. I've send you a working copy of your file. The problem wasn't in coding but in the file itself - it was stucked in design mode in a hopeless try to create a control. So I've just copied the table to an empty word document and added the code posted above. Looks like it is working.

Regards,
Fish
Jan 16 '08 #11
kcdoell
230 100+
Thanks Fish for all your help. I thought something fishy was going on no pun intended.

Thanks again :-)

Keith.
Jan 16 '08 #12
I have a list of states on a form, in which the user can click on various states that are applicable to the form. There is a "ALL State" option (CheckBoxALL) in which if selected all of the state checkboxes will be checked. Below is the following code:



Private Sub CheckBoxALL_Click()



If CheckBoxAll = True Then

CheckBoxState01 = True
CheckBoxState02 = True
CheckBoxState03 = True
CheckBoxState04 = True
etc.....
CheckBoxState52 = True

Else

CheckBoxState01 = False
CheckBoxState02 = False
CheckBoxState03 = False
CheckBoxState04 = False
CheckBoxState05 = False
CheckBoxState06 = False
etc.........CheckBoxState52 = False

End If

End Sub

_______________________

My question is if there is a cleaner way to write this code than the way I wrote it since I am still in VBA learning mode?

I tried something like this but it keeps error out:

Private Sub CheckBoxALL_Click()

Dim CheckALL As Boolean

Set CheckALL = ActiveDocument.FormFields("CheckBoxALL").CheckBox. Value

If CheckALL("CheckBoxALL").CheckBox.Value = True Then

For i = 1 To 52
CheckALL("CheckBoxState0" & i).CheckBox.Value = True
Next i

Else

For i = 1 To 52
CheckALL("CheckBoxState0" & i).CheckBox.Value = False
Next i

End If
End Sub

_________________


My result so far has been compile errors and coding that does not work. Does anybody have any ideas??

Thanks,



Keith.
Hi Keith! Can I ask you something please?
Is the first code you suggested right??
Because I have only 5 checkboxes and it seems easyer to use it...

Thanks
maria
Jan 26 '08 #13
missinglinq
3,532 Expert 2GB
Yes, Maria! This will work fine:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CheckBoxALL_Click()
  2.  
  3. If CheckBoxAll = True Then
  4.  
  5. CheckBox1 = True
  6. CheckBox2 = True
  7. CheckBox3 = True
  8. CheckBox4 = True
  9. CheckBox5 = True
  10.  
  11. Else
  12.  
  13. CheckBox1 = False
  14. CheckBox2 = False
  15. CheckBox3 = False
  16. CheckBox4 = False
  17. CheckBox5 = False
  18.  
  19. End If
  20.  
  21. End Sub
  22.  
You just wouldn't want to have to do all this for 50+ checkboxes!


Welcome to TheScripts!

Linq ;0)>
Jan 26 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Adrian Parker | last post by:
I have a PHP generated page which displays X many records. Each record has a checkbox preceding it. The user checks several checkboxes, and hits a delete button. All the corresponding records...
3
by: Mohammed Mazid | last post by:
Hi folks! Can anyone please help me with this? I am developing a Quiz program but I am stuck with "multiple answers". Basically I need some sort of code that would select multiple answers...
2
by: Edward | last post by:
The following html / javascript code produces a simple form with check boxes. There is also a checkbox that 'checks all' form checkboxes hotmail style: <html> <head> <title></title> </head>...
6
by: terence.parker | last post by:
I currently have the following JS in my header: function checkall(thestate) { var checkboxes=eval("document.forms.EssayList.file_id") for (i=0;i<checkboxes.length;i++)...
2
by: jamesboulter | last post by:
Dear all, I can find plenty of functions that check all checkboxes with a certain HTML "name" attribute, but on the page in question, each is named slightly differently: Therapy_1 Therapy_2...
5
by: Mike Fellows | last post by:
I have created some checkboxes within a panel using the code below Dim NewCheckbox As New CheckBox Me.Panel2.Controls.Add(NewCheckbox) NewCheckbox.Location = New Point(XLocation, YLocation)...
10
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
13
by: PhpCool | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the...
1
by: Anuj | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.