1

Good afternoon all

I have this piece of code to check if the checkboxes of my form are not checked, but each time I have this error message: runtime error '438'

Private Sub Btn_Send_Click()
Dim ctl As Control
Dim iX As Integer

    For Each ctl In Me.Controls
        If TypeName(ctl) = "CheckBox" And ctl.Value = False Then iX = iX + 1
    Next ctl

If iX = 0 Then MsgBox "No checkBox Selected", vbExclamation, "Title"

End Sub

Debbuging error :

If TypeName(ctl) = "CheckBox" And ctl.Value = False Then

Run-Time Error #438: Object doesn't support this property or method

Thank you for your help

braX
  • 11,506
  • 5
  • 20
  • 33
  • 1
    Either your message box is wrong or your logic is wrong. `iX` counts the number of checkboxes that are *not* checked. So when `iX = 0`, that means all checkboxes *are* checked and yet: `MsgBox "No checkbox Selected."` – MoondogsMaDawg Nov 22 '17 at 17:31
  • @MoondogsMaDawg Bravooooooooo, I did not even notice this logic error. | Big Up for you. | it solved the problem entirely !! – Houcine Adsens Nov 22 '17 at 18:33

1 Answers1

2

Try to do it like this:

For Each ctl In Me.Controls
    If TypeName(ctl) = "CheckBox" Then
        If ctl.Value = False Then
             iX = iX + 1
        End If
    End If
Next ctl

The reason is that VBA does not support short-circuit evaluation - Does the VBA "And" operator evaluate the second argument when the first is false?

Thus, even that your first condition is False it nicely goes and checks for your second condition. And if it is not a CheckBox control, then it throws an error, because it does not have a .Value property.

Vityata
  • 42,633
  • 8
  • 55
  • 100