验证用户窗体以使用循环检查空控件 [英] Validation for Userform to check for empty controls, using loops

查看:43
本文介绍了验证用户窗体以使用循环检查空控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户完成输入数据后.当用户单击提交"按钮时.我将检查所有文本框是否都不为空.用户将输入成员数,如果用户输入1个成员,我将检查member01是否为空,如果用户输入3个成员,我将检查member01,member02,member03是否为空它适用于If Else,但我想循环使用,因为对我来说重复10次很乏味.我不知道如何将它从If Else更改为For Loop.

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

推荐答案

您可以执行以下操作:

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

即使您仅将txtMemberNo03作为最大值,即使您在txtNoMember中输入4,此方法也有效.

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

这篇关于验证用户窗体以使用循环检查空控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆