Solved Run function if all three controls have data

Try This:-
@Uncle Gizmo I tried and it didn't work like your video showed. I noticed in your video you typed text into the combo boxes and mine I select an option from the combo box. Maybe that's why mine don't work and your example did?
 
Add three GotFocus routines, like this:

@The_Doc_Man I tried your example and it didnt do anything?

Code:
Private Sub cboEmployee_LostFocus()
    CtDtFlag = nz(cboEmployee, "") <> ""
End Sub

Private Sub txtCountDate_LostFocus()
    CtDtFlag = nz(txtCountDate, "") <> ""
End Sub

Private Sub cboShift_LostFocus()
    CtDtFlag = nz(cboShift, "") <> ""
End Sub

Public Function CreateLabelList()
    Dim strSQL As String

    If IsNull(txtCountDate) Or (txtCountDate) = "" _
                   Or IsNull(cboShift) Or (cboShift) = "" _
                                Or IsNull(cboEmployee) Or (cboEmployee) = "" Then
        MsgBox " >>> " & "Do nothing"
    Else
    
    If (CtDtFlag And CShft And CEmpl) Then
        MsgBox " >>> " & "Do Something"

     End If
    End If
End Function
 
Yes, in the case of the Change event, we need to access the .Text property of the control for a real-timey feel, as shown in the edit of post 3. That post demonstrates an option to trigger the change event inspecting if any of the three controls had data. If all of them must have the data, then something like this could be applied for the Change event:

@Edgar_ Your example works some what. I tried your example, and it checks every time and gives a message BUT when all the controls have data it runs the code BUT it runs it 3 times? How do I get it to only run one time?

Code:
Dim arr(1 To 3) As Variant

Private Sub cboEmployee_Change()
    arr(1) = Me.cboEmployee.Text
    CreateLabelList
End Sub

Private Sub cboShift_Change()
    arr(2) = Me.cboShift.Text
    CreateLabelList
End Sub

Private Sub txtCountDate_Change()
    arr(3) = Me.txtCountDate.Text
    CreateLabelList
End Sub

Private Sub CreateLabelList()
    Dim item As Variant
    For Each item In arr
        If Len(item) = 0 Then
            MsgBox " >>> " & "Do nothing"
            Exit Sub
        Else
            MsgBox " >>> " & "Do Something"
        End If
    Next item
End Sub
 
Last edited:
@Edgar_ Your example works some what. I tried your example, and it checks every time and gives a message BUT when all the controls have data it runs the code BUT it runs it 3 times? How do I get it to only run one time?
Made the mistake of leaving the insert in the Else instruction. The insert should come after the validation of the array. It was doing this:
"For each item in the array, if its length is 0, exit. Oh, but if it's not 0, insert that sh*t" 🤷‍♂️

This should do it:
Code:
Private Sub CreateLabelList()
    Dim item As Variant
    For Each item In arr
        If Len(item) = 0 Then
            Debug.Print "nothing"
            Exit Sub
        End If
    Next item
    'Insert
    'Requery subform
End Sub

Sorry about that, should have tested.
 

Users who are viewing this thread

Back
Top Bottom