Lux Claridge
Lux Claridge

Reputation: 205

Type mismatch error in For Each loop looping through each control

I set up a userform that checks if checkbox control is checked and if so add the tag's value to a variable. Each control has a unique number in its tag.

Dim ctl As Control
Dim sum As Long

For Each ctl In Userform1.Controls
    If ctl.ControlType = acCheckBox Then
        If ctl.Value = True Then sum = sum + ctl.Tag
    End If
Next
Me.TextBox7 = sum

However, when the code gets to the for each statement, a type mismatch error is thrown and ctl = Nothing. I've added a break to see if this error is happening on the first iteration and that seems to be the case. Thoughts? This was supposed to be a simple little tool to help with another project...

Upvotes: 1

Views: 912

Answers (2)

Try this code

Dim ctl As CheckBox

Upvotes: 1

ccarpenter32
ccarpenter32

Reputation: 1077

I am guessing here, but is your error on If ctl.Value = True Then sum = sum + ctl.Tag? The value you are likely looking for is -1 not True as that is how check boxes evaluate in Access-VBA.

Edit:

Whoops, ignore that. I think I see the problem, change For Each ctl In Userform1.Controls to For Each ctl In Forms(Userform1).Controls

Upvotes: 0

Related Questions