我如何以编程方式在表单/页面上找到控件的确切位置

时间:2017-06-07 14:01:49

标签: ms-access access-vba

Function RequiredFieldsMissing(frm As Form) As Boolean
Dim foundEmpty As Boolean
Dim txt As TextBox
Dim cmb As ComboBox
Dim pge As Page
Dim Ctrl As Control
Dim formName As String`

    foundEmpty = False
    formName = frm.Name
    For Each Ctrl In frm.Controls
        If Len(Ctrl.Name) > 6 Then
            If left(Ctrl.Name, 3) = "txt" And right(Ctrl.Name, 3) = "Req" Then
                Set txt = Ctrl
                If IsNull(txt.value) Then
                    foundEmpty = True
                    txt.BackColor = constLngRed
                    'Form_Clients.TabClientData.Pages(1).SetFocus
                    txt.SetFocus
                Else
                    If Len(txt.value) = 0 Then
                        foundEmpty = True
                        txt.BackColor = constLngRed
                    End If
                End If
            End If
            If left(Ctrl.Name, 3) = "cmb" And right(Ctrl.Name, 3) = "Req" Then
                Set cmb = Ctrl
                If IsNull(cmb.value) Then
                    foundEmpty = True
                    cmb.BackColor = constLngRed
                Else
                    If Len(cmb.value) = 0 Then
                        foundEmpty = True
                        cmb.BackColor = constLngRed
                    End If
                End If
            End If
        End If
    Next Ctrl

    RequiredFieldsMissing = foundEmpty
End Function

上面的函数将着色并突出显示没有数据的控件...问题是我需要将焦点设置到控件但有时控件驻留在子窗体和页面上。 如果我执行txt.setfocus,那么它会将焦点设置为该控件但不会移动到驻留页面。我传递的唯一变量是表格。

我的问题如下......

当我只传递表单名称时,如何将焦点设置为驻留在页面上的控件?

1 个答案:

答案 0 :(得分:0)

你必须调用SetFocus两次:一次将焦点设置为子窗体(在主窗体上),一次将焦点设置为子窗体上的控件。