PDA

View Full Version : Solved: vba code to link two check boxes.



coach_z
10-17-2008, 06:52 AM
i have yet to implement this and my knowlege of vba code is basically zero.
I would like to have a series of check boxes that offer a quick reference to the status of a project. this check boxes are linked to boxes int eh rest of the document

what i want is that a user can fill out a form by either going through step by step and selecting each check box indicating completion. or, if someone has been doing this for 20 years they shouldnt have to read anything and just select the check boxes at the top of the screen.

i have found the following code which appears like it will work:

Private Sub CheckBox1_Click()
CheckBox2.Value = False
End Sub

Private Sub CheckBox2_Click()
CheckBox1.Value = False
End Sub

but will this code allow the user to check either box and then have the other updated?

thanks for youur help!

Nelviticus
10-17-2008, 08:49 AM
Yes it should work, you could modify it to be something like this:
Private Sub CheckBox1AtTopOfDocument_Click()
CheckBox1LaterInDocument.Value = CheckBox1AtTopOfDocument.Value
End Sub

Private Sub CheckBox1LaterInDocument_Click()
CheckBox1AtTopOfDocument.Value = CheckBox1LaterInDocument.Value
End Sub
... so for each pair of check-boxes, when one changes the other one changes to the same value.

coach_z
10-20-2008, 06:41 AM
edit: i need to learn to read!...lol
thanks a lot!!!!

coach_z
10-21-2008, 08:14 AM
i am having a terrible time implementing this...can you walk me through this ?? ill be looking for a good tutorial until then

lucas
10-21-2008, 10:01 AM
Hi coach,
A symplified example is attached. Let us know if we understand the problem.


Option Explicit
Private Sub CheckBox1_Click()
CheckBox2.Value = CheckBox1.Value
If CheckBox1.Value = True Then TextBox1.Value = "Completed"
If CheckBox1.Value = False Then TextBox1.Value = "Incomplete"
End Sub
Private Sub CheckBox2_Click()
CheckBox1.Value = CheckBox2.Value
If CheckBox2.Value = True Then TextBox2.Value = "Completed"
If CheckBox2.Value = False Then TextBox2.Value = "Incomplete"
End Sub

fumei
10-21-2008, 01:50 PM
I strongly suggest you do this kind of logic with a CommandButton (say an "OK" button). Put your full logic in THAT event.

Why? You must remember that all instructions INSIDE control events - such as Click - can fire other events. For example, the Click event is followed by the Exit event. Because having the logic structure outside of the specific controls (the checkboxes) means you have full and complete logic control (i.e. the meanings or Values) outside of the events of those controls.

This can be very important if there is complex logic involving multiple controls.

coach_z
10-22-2008, 07:47 AM
fumei,

thank you for that suggestion, but to be honest it does not make any sense to me because i do not know much about vb at all.

what i have right now works very well. my problem that i stated earlier was solved by using the boxes in the design mode and not the forms