2014-09-22 78 views
2

我製作了一個用戶表單,其中所有字段都是必填項。此用戶表單上有5個頁面。我需要把重點放在驗證空間的領域。我想這樣做,與ISERROR聲明在多頁面中動態設置焦點

暗淡我作爲整數

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 

我也嘗試沒有成功

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 

任何幫助將不勝感激

做着同樣的一個錯誤處理程序
+0

是否所有的字段文本框/列表框/組合框或者你有其他類型的控件? – Rory 2014-09-22 11:17:37

+0

不僅僅是checkbox/testbox/listbox內部框架[某些情況下] – Sm1 2014-09-22 11:31:48

回答

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 
  • 然後使用下面的代碼在各的textBox _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

你只能SetFocus的一個功能的控制是當前選擇多頁。如果您在嘗試使用SetFocus之前檢查MultiPage.Value,則只有在相關的MultiPage頁面處於焦點狀態時纔可以選擇設置焦點,或者可以將MultiPage.Value更改爲控件所在的頁面,然後使用SetFocus。您可能還必須先將SetFocus設置爲控件所在的任何框架。

您也可能要考慮讓簡單地使用

Controls(<nameofcontrol>).SetFocus 

這裏,而你正在執行的有效性,然後該驗證失敗的第一個控件的名稱是一些示例代碼 - 我使用一個公共枚舉了多頁的價值觀和mvarlMeasuresControlSets是採取措施控制集,這是動態創建的每一個用戶需要進入一個新的測量時間的計數器:

' 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