2014-09-22 4 views
2

모든 필드가 필수 인 userform을 만들었습니다. 이 userform에는 5 페이지가 있습니다. 비어있는 필드에 유효성 검사에 중점을 둘 필요가 있습니다. 나는 ISERROR 문으로 그 일을 시도다중 페이지에서 동적으로 포커스 설정

희미한 내가 정수 나는 또한 어떤 도움

을 이해할 수있을 것이다

Dim i As Integer 

     For Each ctrl In Controls 'loop through Controls and search for Control with the right name 
      i=0 

       If ctrl.Value = "" Then 
        MsgBox ctrl.Name, vbExclamation, "Input Data" 
        On Error GoTo ErrHandler: 

        ctrl.SetFocus 
        ErrHandler: 
        UserForm1.MultiPage1.Value = i 
        i = (i + 1) Mod 5 
        Resume 
        Exit Sub 
       End If 
     Next 

성공하지 오류 처리기와 같은 일을 시도

For Each ctrl In Controls 'loop through Controls and search for Control with the right name 

i = 0 
      If ctrl.Value = "" Then 
       MsgBox ctrl.Name, vbExclamation, "Input Data" 


       While IsError(ctrl.SetFocus) 
       UserForm1.MultiPage1.Value = i 
       i = (i + 1) Mod 5 
       Wend 
       ctrl.SetFocus 
       Exit Sub 

      End If 
      Next 

+0

모든 필드의 텍스트 상자 /리스트 박스/콤보 또는 당신이 컨트롤의 다른 유형이 수행을 MultiPage 가치와 mvarlMeasuresControlSets 동적으로 사용자가 새로운 측정을 입력해야 할 때마다 생성되는 조치 제어 설정을위한 카운터 ? – Rory

+0

프레임 안의 확인란/testbox/listbox 없음 [일부 경우] – Sm1

답변

0

아래를보십시오. 그것을하는 한 방법입니다.

Sub example() 
Dim pageIndx As Integer 
Dim firstPage As String 
pageIndx = 0 'leave at 0, first page 
CurrentPage = "Page1" 'Make sure all pages are named the same, 
        'with an increasing number at the end(it is like this by default) 

For Each ctrl In UserForm1.Controls 
    On Error Resume Next 
    Debug.Print ctrl.Name 
    Debug.Print ctrl.Parent.Name 
    If (ctrl.Parent.Name Like "Page*" And ctrl.Parent.Name > CurrentPage) Then 
     pageIndx = pageIndx + 1 
    End If 
    If ctrl.Value = "" Then 
     UserForm1.MultiPage1.Value = pageIndx: UserForm1.Show 
     MsgBox ctrl.Name, vbExclamation, "Input Data" 
     ctrl.Value = "Input Data" 
     Exit Sub 
    End If 
Next ctrl 

End Sub 
  • 그럼 텍스트 상자 _MouseDown 서브을 아래의 코드를 사용하여, 사용자 정의 폼 코드 섹션 (이 예에서, 그것은 TextBox3이다). 이렇게하면 사용자가 텍스트 상자를 클릭하여 입력 할 때 텍스트 상자가 지워집니다.

    If (Me.TextBox3.Value = "Input Data") Then 
        Me.TextBox3.Value = "" 
    End If 
    
0

여기에 한 가지 방법 - 그것은 어떤리스트 박스/콤보 자신의 목록에서 선택 필요가 있다고 가정합니다

Private Sub CommandButton1_Click() 
    Dim ctl     As MSForms.Control 
    Dim pg     As MSForms.Page 
    Dim bFoundOne    As Boolean 

    For Each pg In Me.MultiPage1.Pages 
     For Each ctl In pg.Controls 
      Select Case TypeName(ctl) 
       Case "TextBox" 
        If ctl.Value = vbNullString Then 
         bFoundOne = True 
         FlagInvalid pg.Index, ctl 
         Exit For 
        End If 
       Case "ListBox", "ComboBox" 
        If ctl.ListIndex = -1 Then 
         FlagInvalid pg.Index, ctl 
         bFoundOne = True 
         Exit For 
        End If 

      End Select 
     Next ctl 
     If bFoundOne Then Exit For 
    Next pg 
End Sub 
Sub FlagInvalid(lngIndex As Long, ctl As MSForms.Control) 
    MsgBox "Please fill out ALL controls" 
    Me.MultiPage1.Value = lngIndex 
    ctl.SetFocus 
End Sub 
0

을 할 수 있습니다 현재 선택을 MultiPage에있는 사용 제어 만 SetFocus 함수 . SetFocus를 시도하기 전에 MultiPage.Value를 확인하면 관련 MultiPage 페이지가 포커스가있는 경우에만 포커스를 설정하도록 선택하거나 MultiPage.Value를 컨트롤이있는 페이지로 변경 한 다음 SetFocus를 사용할 수 있습니다. 컨트롤이있는 모든 프레임에 대해 SetFocus를 먼저 수행해야 할 수도 있습니다.

또한 단순히 여기

Controls(<nameofcontrol>).SetFocus 

을 사용하면 다음 유효성 검사를 수행하고있는 동안 검증에 실패한 첫 번째 컨트롤의 이름을 받고 고려하는 것이 좋습니다 것은 몇 가지 예제 코드입니다 - 나는에 대한 공개 열거를 사용

' Ensure that fraMeasures has focus and then SetFocus to txtName: 
If MultiPage.value = VMT_DASHBOARD_PAGE_MEASURES Then 
    If Controls("txtMeasureName" & mvarlMeasuresControlSets).Enabled Then 
    Controls("fraMeasures").SetFocus 
    Controls("txtMeasureName" & mvarlMeasuresControlSets).SetFocus 
    End If 
End If