views:

1213

answers:

1

I'm fairly new at this (VBA programming) so pls forgive me if my coding seems simplistic.

I have a Word UserForm (Word 2007) with some checkboxes and 2 command controls - Ok and Cancel. When the form is activated from the macro menu or from an assigned icon the checkboxes don't work. However, the 2 command controls work.

What I mean by not working is that when I click on the checkboxes nothing happen - they don't respond. When I click on the OK button a message appears telling me that I haven't selected anything! When I click on the Cancel button the form unloads.

The checkboxes consist of the Click event which toggles from checked to uncheck. There's also a SelectAll checkbox. When clicked all the other checkboxes are checked or uncheck. When one of the other checkbox is uncheck the SelectAll checkbox is also uncheck.

When I activate the form from VBE everything works exactly the way I want them to.

Here's a sample of what I'm talking about:

Sub Loadform()
Load UserForm1
UserForm1.Show
End Sub

Private Sub btnCancel_Click()
Unload Me
End Sub

Private Sub btnOK_Click()
If Me.CheckBox2.Value = True And Me.CheckBox3.Value = True Then
    MsgBox "All checkboxes are checked"
ElseIf Me.CheckBox2.Value = True Then
    MsgBox Me.CheckBox2.Name & " is checked"
ElseIf Me.CheckBox3.Value = True Then
    MsgBox Me.CheckBox3.Name & " is checked"
ElseIf Me.CheckBox2.Value = False And Me.CheckBox3.Value = False Then
    MsgBox "You haven't selected any checkboxes."
End If

End Sub

Private Sub CheckBox2_Click()
If Me.CheckBox2.Value = True Then
    Me.CheckBox2.Value = False
    Me.ckbSelectAll.Value = False
Else
    Me.CheckBox2.Value = True
End If
End Sub

Private Sub CheckBox3_Click()
If Me.CheckBox3.Value = True Then
    Me.CheckBox3.Value = False
    Me.ckbSelectAll.Value = False
Else
    Me.CheckBox3.Value = True
End If
End Sub

Private Sub ckbSelectAll_Click()
If Me.ckbSelectAll.Value = True Then
    Me.ckbSelectAll.Value = False
Else
    Me.ckbSelectAll.Value = True
End If
If ckbSelectAll.Value = False Then
    Me.CheckBox2.Value = False
    Me.CheckBox3.Value = False
Else
    Me.CheckBox2.Value = True
    Me.CheckBox3.Value = True
End If
End Sub

I've been looking at this code for so long and I still can't find where the problem is. Any assistance in solving this problem is greatly appreciated.

+2  A: 

In your click event handlers, such as

Private Sub CheckBox3_Click()

...

End Sub

you check to see if the checkbox is checked (i.e. the value is true) and if it is, set the value to false. This means that a checkbox will always be unchecked as soon as it is checked (or always checked as soon as it is unchecked), giving the appearance that checkboxes don't work properly.

Here's an example click event handler to start (I haven't written VBA for a long time, but I think the following is fine. Will test now...)

Private Sub CheckBox2_Click()
    ' If checkbox2 is checked but checkbox3 is not, ' 
    ' uncheck the select all checkbox '
    If CheckBox2 And Not CheckBox3Then
        ckbSelectAll = False
    End If
End Sub

If you need any further help or tips, then please leave a comment

Russ Cam
Thank you Russ for answering so quickly. The code that you've given me doesn't work.What I'd really like to happen is if I click on SelectAll both checkbox2 and checkbox3 are selected. If I uncheck either checkbox2 or checkbox3 then the SelectAll gets unchecked.My real form has about 17 checkboxes.Thank you for your help.RegardsDao
It does work, but it will not work without changing the rest of your code (I resisted giving you more complete code as I think that it is a good idea to really grok the code given :) ). A couple of pointers on VBA - clicking on a checkbox will toggle its value and therefore it doesn't need to be done manually in your code. Secondly, controls have a default property which for a checkbox is the value. Therefore checking "If checkbox.Value = false" is the same as writing "If Not checkbox".
Russ Cam
Thirdly, IIRC, this ones a real pain! in VBA, if you set the checkbox value in code, it will raise the click event for that checkbox. Therefore if you set the SelectAll checkbox value in code, it will raise the click event of the SelectAll checkbox and execute the code in the event handler, which may have some unexpected behaviours, depending on what you;re doing in the handler. There are ways around this, which I can go into once you're at that point :) Hope that this helps
Russ Cam
Thank you. I will have another crack and let you know how I go. Dao