Search code examples
excelvbauserform

How to generate an error when important input text boxes are empty


I am creating a data entry userform that calculates loads (e.g.pipeloads, wind loads etc..). There are important text boxes that cannot be left unfilled. If the user clicks the command "Add Input" and there are some text boxes left unfilled, I want an error to be generated that forces the user to enter a value.

I've got as far as generating an error for one text box. This code will trigger an error alert when you try to leave the text box without filling it first.

    Private Sub txtdist_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Trim(txtdist.Value) = "" And Me.Visible Then
    MsgBox "Required Entry!", vbCritical, "Error"
    Cancel = True
    txtdist.BackColor = vbRed
    Else
    txtdist.BackColor = &HC0FFFF
    End If
    End Sub

What I'm trying to achieve is that when I click "Continue" command button, then the program will see if any textbox or combobox is left empty to force the user to enter a value. (Similar to when you try to buy online with credit card, the page will not submit if there is not name or credit card number etc...). Thank you.


Solution

  • Maybe you can try this

    Private Sub Continue_click()
    
    Dim c as Control
    
        For Each c In Me.Controls
            If TypeName(c) = "TextBox" Then
    
                If Trim(c.Value) = "" Then
                  MsgBox "Please fill out all required information", vbInformation
                Exit Sub
                End If
    
            End If
        Next c
    End Sub