Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Optimize Code - Part 2 (checkbox values) 1

Status
Not open for further replies.

supernewb

Technical User
Feb 7, 2005
18
US
while i'm on the subject of optimizing my code... i have tried to shorten this with a do loop (there are actually some with 50 checkboxes). when testing, i can f8 thru the thing, but it doesn't actually do anything on the form. i've since deleted my practice do loop code, but can recreate if needed. i just need to "check all" when the user checks the CheckBox_p2bprompt and "uncheck all" when the user unchecks it.

this is the original code i am trying to shorten:

Code:
Private Sub CheckBox_p2bprompt_Click()
If CheckBox_p2bprompt.Value = True Then
CheckBox_p2bprompt1.Value = True
CheckBox_p2bprompt2.Value = True
CheckBox_p2bprompt3.Value = True

End If
If CheckBox_p2bprompt.Value = False Then
CheckBox_p2bprompt1.Value = False
CheckBox_p2bprompt2.Value = False
CheckBox_p2bprompt3.Value = False

End If
End Sub

thanks!!
 
Code:
Private Sub CheckBox_p2bprompt_Click()
    Dim ctl As Control
    For Each ctl In Controls
        With ctl
            If Left(.Name, 5) = "Check" Then _
                ctl.Value = CheckBox_p2bprompt.Value
        End With
    Next
End Sub

Skip,

[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue]


 
this rocks!! the only thing is that my CheckBox_p2bprompt checks EVERY check box in the whole form. i do not understand all of the code, specifically [If Left(.Name, 5)]; so i just cut and pasted it into my form.

thank you so much!
 
yup, unless you add some other code to look at other parts of the control name.

Skip,

[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue]


 
gotcha. will work with this. thanks so much Skip!! i had no idea this could even be done.
 


...opens up whole new vistas, eh?

Skip,

[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue]


 


BTW, I like to use the Select Case...End Case statement. You might try some thin like this...
Code:
Private Sub CheckBox_p2bprompt_Click()
    Dim ctl As Control
    For Each ctl In Controls
        With ctl
            If Left(.Name, 5) = "Check" Then
               Select Case Split(.Name, "t")(1)
                  Case 1 to 5
                     ctl.Value = CheckBox_p2bprompt.Value
                  Case 6 to 10
                    
                  Case Else
 
               End Select
            End If
        End With
    Next
End Sub


Skip,

[glasses] [red]Be advised:[/red] When you ignite a firecracker in a bowl of vanilla, chocolate & strawberry ice cream, you get...
Neopolitan Blownapart! [tongue]


 
waaaaaaaaaaaah. no actually, i'm reading up on the select case structure now. i understand its overall purpose. i will have to practice with this one a bit. thanks again!
 
I have to ask....from a design point of view WHY do you have to have all the other checkboxes become checked (TRUE) when ONE checknox is true. This, from a design perspective seems a wate. Could you explain. It seems to be that if a cariable (your checlbox being True or False) the others could simply be an array of other boolean variables. These could be set (TRUE or False) simply by the result of ONE checkbox. The result could be displayed to the users by way of labels.

So again, WHY do you have all the other checkboxes, if they are not actually used as checkboxes. Checkboxes (from a design point of view are used for user inoput, user choices). If there is no chocies made, there is no point in using checkboxes.

Gerry
See my Paintings and Sculpture
 
Hi Gerry,
Thanks for posting. This is for a client review type thing, which will be used over and over, but will be diff. for each review.

For ease of use, the checkboxes have been grouped into categories, each box within each category corresponds to an option that may or may not need to be added to the finished document. Since there are so many choices for the user, she wanted to have the option of "checking all" in a particular category or just checking the one's that are to be included in the final doc.

Since the doc is about 60 pgs now, I have each checkbox that is left un-checked go and delete the corresponding paragraphs, bullet points etc.

It's almost done, but that's what I keep saying as another week goes by :)

D
 
Shrug. There is accounting for client desires. So, no offence to you at all, but it is still poor design. Again from a design point of view it could be a Check All (ONE checkbox), or Make Choices (which would then display choices). Unfortunately, it is very common for people to use both checkboxes, and textboxes, when what is actually proper use is labels.

Oh well.

Gerry
See my Paintings and Sculpture
 
thanks anyway. btw. you are quite an artist. love your work!
D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top