SamwiseVB
SamwiseVB

Reputation: 51

How to detect if all checkboxes are disabled?

I have an access table that stores information about the steps completed on a product. When entering information, I have a form pop up asking which product they want to enter data for. Each record in the table gets its own checkbox (dynamically created). If information has already been recorded for that product, for that step then the checkbox is disabled. When all checkboxes are disabled I have a message box pop up saying all products for that step have been completed.

The issue: Say for whatever reason (operator choice, production reason, etc), work is done on product 4 for an order but not complete on 1, 2, 3. The code that I have says that all products have info entered just because that is the last record checked.

Dim Args As Variant
Dim i As Integer
Dim ctl As Control
Dim bCheck As Boolean

bCheck = False

If Not IsNull(Me.OpenArgs) Then
    Args = Split(Me.OpenArgs, ";")
        Me.txtForm = Args(0)
        Me.lblChoices.Caption = Args(1)
End If

For Each ctl In Forms(Me.Name).Controls
    If ctl.ControlType = acCheckBox Then
        ctl.Value = False
        If ctl.Enabled = True Then
            bCheck = False
        Else
            bCheck = True
        End If
    End If
Next

If bCheck = True Then
    fncMsgBox "Labor has been entered for all bundles on this step."
    DoCmd.Close acForm, Me.Name
End If

This IF statement is the problem and its obvious to me why it doesn't work. I'm curious as to how I can get around this?

If ctl.Enabled = True Then
    bCheck = False
Else
    bCheck = True
End If

Form Screenshot

Upvotes: 0

Views: 90

Answers (1)

Tim Williams
Tim Williams

Reputation: 166790

Exit the loop as soon as you find an enabled checkbox:

Dim Args As Variant
Dim i As Integer
Dim ctl As Control
Dim bCheck As Boolean

bCheck = False

If Not IsNull(Me.OpenArgs) Then
    Args = Split(Me.OpenArgs, ";")
        Me.txtForm = Args(0)
        Me.lblChoices.Caption = Args(1)
End If

For Each ctl In Forms(Me.Name).Controls
    If ctl.ControlType = acCheckBox Then
        ctl.Value = False
        If ctl.Enabled Then
            bCheck = True
            Exit For 'stop checking
        End If
    End If
Next

If bCheck Then
    fncMsgBox "Labor has been entered for all bundles on this step."
    DoCmd.Close acForm, Me.Name
End If

Note: you don't need = True in your If when the value you're checking already represents a boolean.

Upvotes: 2

Related Questions