遍历文本框以确认已输入所有信息

Loop through TextBoxes to confirm all information has been entered

我浏览了这里的许多循环帖子,不幸的是,我无法修改我发现的内容来满足我的需要,所以我寻求一些帮助。

请注意,正如您在我的其他帖子中看到的那样,我对循环还比较陌生。

总而言之,我的问题是我需要 运行 检查标签值为 "Required" 的 TextBox 控件,它们位于同一个用户窗体上的三个不同框架中,以确保它们当用户尝试在同一个用户窗体的文本框中输入值时完成。如果任何带有 "Required" 标签的文本框没有值,那么我需要调试打印来显示这些文本框的名称。从那里我将能够弄清楚如何将打印输出添加到 msgbox。下面是我已经开始的代码,但我一直在研究如何将每个没有值的 TextBox 存储在一个我可以用于调试打印的变量中。

Private Sub yLPLendComp_Enter()
    Dim ctrl As Control
    Dim i As Integer

'   SET THIS TO STORE THE CTRL.NAME IF BLANK??
    i = 0

'   THIS LOOKS THROUGH EACH REQUIRED TEXTBOX (SET BY TAG VALUE)
    For Each ctrl In LP.LoanInfo.Controls
        If TypeName(ctrl) = "TextBox" Then
            If ctrl.Tag = "Required" And ctrl.Value = "" Then
                i = i + 1
                Debug.Print ctrl.Name
        End If
    Next ctrl
'   IF THE VALUES ARE BLANK THEN MSG BOX APPEARS AND PRINTS THE EMPTY TEXT BOX NAMES
'    If i = 1 Then
'        Debug.Print ctrl.Name
'        MsgBox "Not all fields that need to be completed are complete please complete your required fields.", vbCritical, UCase("error")
'    End If
End Sub

像这样:

Private Sub yLPLendComp_Enter()

    Dim ctrl As Control
    Dim msg As String, sep

    For Each ctrl In LP.LoanInfo.Controls
        If TypeName(ctrl) = "TextBox" Then
            If ctrl.Tag = "Required" And ctrl.Value = "" Then
                msg = msg & sep & ctrl.Name
                sep = vbLf
            End if
        End If
    Next ctrl

    If Len(msg) > 0 Then
        MsgBox "One or more required fields need to be completed" & vbLf & msg, _
                 vbCritical, "Missing Information"
    End If
End Sub

如果您的控件名称不是用户友好的,您可以使用 Tag 值,例如 "Required:User Friendly Name Here",然后检查

If ctrl.Tag Like "Required:*"

然后

msg = msg & sep & Replace(ctrl.Tag, "Required:","")

编辑:使用链接标签中的文本

msg = msg & sep & Me.Controls("Label_" & ctrl.Name).Caption