Validation for Userform to check for empty controls, using loops

风流意气都作罢 提交于 2019-12-24 12:14:05

问题


After the user done entering the data. When the user click Submit button. I will check if all the textbox is not empty. The user will enter number of member, if the user enter 1 member I will check if member01 is empty, if the user enter 3 member I will check if member01, member02, member03 is empty It works for If Else but I would like to do it for loop, as it is tedious for me to do it 10 times. I don't know how do I change it from If Else to For Loop.

 ‘Using If Else
  If txtNoMember.Value = "" Then
        MsgBox "Please enter the Number of Member.", vbExclamation, "Input Data"
        txtNoMember.SetFocus
        Exit Sub
    End If

    If txtNoMember.Value = 1 And txtMember01.Value = "" Then
        MsgBox "Member cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
    End If

    If txtNoMember.Value = 2 And txtMember01.Value = "" And txtMember02.Value = "" Then
        MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
    ElseIf txtNoMember.Value = 2 And txtMember01.Value = "" Then
        MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
    ElseIf txtNoMember.Value = 2 And txtMember02.Value = "" Then
        MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
    End If

    If txtNoMember.Value = 3 And txtMember01.Value = "" Then
        MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
    ElseIf txtNoMember.Value = 3 And txtMember02.Value = "" Then
        MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
    ElseIf txtNoMember.Value = 3 And txtMember03.Value = "" Then
        MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
        MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        Exit Sub
    End If

‘Using Loop
Dim Ctrl As Control
Dim CtrlNum As Long

For Each Ctrl In Me.Controls
    If Ctrl.Name Like "txtMember##" Then
        CtrlNum = CLng(Right$(Ctrl.Name, 2))
        If CtrlNum.Value = "" Then
            MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
        End If
    End If
Next

回答1:


You could do something like this:

Private Sub CommandButton1_Click()
If txtNoMember.Value = "" Then
    MsgBox "Please enter the Number of Member.", vbExclamation, "Input Data"
    Exit Sub
Else
    Dim v As Long, ctrl As Control
    v = txtNoMember.Value
    For i = 1 To v
        For Each ctrl In Controls ' loop through Controls and search for Control with the right name
            If ctrl.Name = "txtMember" & Format(i, "0#") Then
                If ctrl.Value = "" Then
                    MsgBox "Member(s) cannot be empty.", vbExclamation, "Input Data"
                    Exit Sub
                End If
                Exit For
            End If
        Next
    Next
End Sub

This works even if you enter 4 in txtNoMember, when you only have txtMemberNo03 as max.



来源:https://stackoverflow.com/questions/20370769/validation-for-userform-to-check-for-empty-controls-using-loops

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!