borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2010 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (checking several columns to do data report..)Next Topic (VBA loop counter) New Topic New Poll Post Reply
AndreaVB Forum : VB General : Generic Statement for Linking CheckBoxes
Poster Message
Ulic
Level: Trainee

Registered: 15-04-2009
Posts: 2

Ads by Lake Quincy Media
icon Generic Statement for Linking CheckBoxes

I've created a huge checklist in Word 2007 using the Checkbox feature. Some of these checkboxes exist in a hierarchy, for example, say I have a one item shopping list with a checkbox named "Groceries" and one item under that named "Bananas".  If "Bananas" is checked the checkbox above it, "Groceries" should also be checked.
I have written a few simple If statement so when a box is clicked it will check or uncheck another box.  The second statement is slightly more complex marking as true and locking the checkbox above it in the hierarchy.  For the example above CheckBox1 would equate to "Groceries" and CheckBox1a would equate to "Bananas"
Private Sub CheckBox1_Click()
    If CheckBox1.Value = True Then
        CheckBox1a.Value = True
    End If
End Sub
Private Sub CheckBox1a_Click()
    If CheckBox1a.Value = True Then
        CheckBox1.Value = True
        CheckBox1.Locked = True
    Else
        CheckBox1.Value = False
        CheckBox1.Locked = False
    End If
End Sub

Now I'm literally rewriting this code over and over again for each and every one of these little checkbox hierarchies I have.  Can't I just write one If statement referencing a generic CheckBox and use the Call function under these Subs to run it?  My question is how do I do that?  I've tried a few things but keep running into errors.  Something along these lines?
Dim X As Variable
Dim Y As Variable
X = 1
Y = 1a
Private Sub CheckBoxX()
    If CheckBox(X).Value = True Then
        CheckBox(Y).Value = True
    End If
End Sub
Private Sub CheckBoxY()
    If CheckBox(Y).Value = True Then
        CheckBox(X).Value = True
        CheckBox(X).Locked = True
    Else
        CheckBox(X).Value = False
        CheckBox(X).Locked = False
    End If
End Sub

That doesn't work obviously because I have no idea what I'm doing but I hope you get the gist of what I'm trying to do.  Thanks in advance for any help.

15-04-2009 at 10:42 PM
View Profile Send Email to User Show All Posts | Quote Reply
Ulic
Level: Trainee

Registered: 15-04-2009
Posts: 2
icon Re: Generic Statement for Linking CheckBoxes

I figured out what I was trying to do but now that I look at it my original post may not have been as clear as I would have liked so I'll try and explain it more clearly for posterity here.
I have a list of things in a word document set up as a checklist using checkboxes. As an example let's say it's a to do list containing a shopping list with a main heading of Groceries, with items below that. It is set up as such:

[ ] Grocheries
     [ ] Bananas
     [ ] Apples

I wrote subs that fire on click that will check some of the boxes for me. Let's say for example I bought all my groceries but didn't check off all my items one at a time. I want to be able to click Groceries and have all the items under that heading marked as true. See code:

Private Sub Groceries_Click()
     If Groceries.Value = True Then
          Bananas.Value = True
          Apples.Value = True
     End If
End Sub

Now I don't have it set up to uncheck all the boxes if Groceries is marked as False out of personal preference. (If I marked it by accident and purchased some of the items I'm going to have to go back through the list and check things off manually anyway so what's the point of having them all unchecked?)
But if I am checking each item off one at a time as I grab them I have subs that fire on click for each of the items under then main heading to check if everything has been marked as True under that main heading and if that is the case mark the main heading as true.
Private Sub Grocery_Items()
     If Bananas.Value = True Then
          If Apples.Value = True Then
               Groceries.Value = True
               Groceries.Locked = True
          Else
               Groceries.Value = False
               Groceries.Locked = False
          End If
     End If
End Sub

Private Sub Bananas_Click()
     Call Grocery_Items
End Sub

Private Sub Apples_Click()
     Call Grocery_Items
End Sub

Lets say I have another packing list for Toiletries so I don’t forget them. This list includes Toothbrush and Toothpaste. You can see that its going to look almost exactly the same as my above example but the names will be different. Now I can write the code again for each checkboxes on click just changing the name but it would be much and more elegant if I had a generic sub I could call.
Here is what I ultimately came up with:
Private Sub CheckBox()
Dim X As Object
Dim Y As Object
Dim Z As Object
     If X.Value = True Then
          Y.Value = True Z.Value = True
     End If
End Sub

Private Sub CheckBoxa()
Dim X As Object
Dim Y As Object
Dim Z As Object
     If Y.Value = True Then
          If Z.Value = True Then
               X.Value = True
               X.Locked = True
          Else
               X.Value = False
               X.Locked = False
          End If
     Else
          X.Value = False
          X.Locked = False
     End If
End Sub

Private Sub Groceries_Click()
X = Groceries
Y = Bananas
Z = Apples
     Call CheckBox
End Sub Private

Sub Bananas_Click()
X = Groceries
Y = Bananas
Z = Apples
     Call CheckBoxa
End Sub
Private Sub Apples_Click()
X = Groceries
Y = Bananas
Z = Apples
     Call CheckBoxa
End Sub

I declear the varables in each sub rather than in the "Generic" one because they won't always equal those specific checkboxes. As you can see my problem was that I was trying to jam an object into the checkbox when I really just needed to create a placeholder for the checkbox as a whole. Odd that I didn't see that to begin with, it took me two days.

[Edited by Ulic on 17-04-2009 at 10:27 PM GMT]

17-04-2009 at 10:27 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : Generic Statement for Linking CheckBoxes
Previous Topic (checking several columns to do data report..)Next Topic (VBA loop counter) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2010 Andrea Tincaniborder