Forum Discussion

cking1333's avatar
cking1333
Copper Contributor
Aug 01, 2024

Msgbox if all text boxes are blank

Good day, 

 

Is there a way I can generate a msgbox when a specific button is clicked if all text boxes in a userform have been left blank? I know I can create code for each box individually but I'm thinking there must be a way to combine everything together and generate the msgbox.

 

Thanks in advance for any assistance!!!

  • cking1333 

    For a command button named cmdCheck:

    Private Sub cmdCheck_Click()
        Dim ctl As Control
        Dim f As Boolean
        For Each ctl In Me.Controls
            If TypeName(ctl) = "TextBox" Then
                If ctl.Value <> "" Then
                    f = True
                    Exit For
                End If
            End If
        Next ctl
        If Not f Then
            MsgBox "All text boxes are empty!", vbInformation
        End If
    End Sub
  • cking1333 

    For a command button named cmdCheck:

    Private Sub cmdCheck_Click()
        Dim ctl As Control
        Dim f As Boolean
        For Each ctl In Me.Controls
            If TypeName(ctl) = "TextBox" Then
                If ctl.Value <> "" Then
                    f = True
                    Exit For
                End If
            End If
        Next ctl
        If Not f Then
            MsgBox "All text boxes are empty!", vbInformation
        End If
    End Sub
    • cking1333's avatar
      cking1333
      Copper Contributor
      Wonderful....thanks for a quick reply with code that works perfect!!!!!

Resources