2015-04-03 64 views
0

我對Access VBA相對較新,並且有一個窗體,其上有大約30個複選框。保存表格時,我想確保所有複選框都被打勾(設置爲true)。 tickbox有所有名稱SC1,SC2 .... SCN有沒有辦法循環每個控件,看看它是否已被設置爲true? 這是我曾嘗試,但它似乎沒有閱讀tickbox -訪問VBA - 表單上的所有複選框已被檢查

Private Sub Validate_Data(rstTop) 
Dim n As Integer 
Dim count As Integer 
count = 0 

For n = 1 To rstTop 
    If Form.Controls("SC" & n).Value = False Then 
    count = count + 1 
    End If 
Next 
If count <> 0 Then 
MsgBox "Not all Questions have been ticked, please tick and add comments", vbInformation, _ 
     "More information Required" 

Else 
End If 


End Sub 

回答

0

讓這個嘗試,它爲我工作。

Option Compare Database 
Option Explicit 

Private Function Validate_Data() As Boolean 
    Dim ctl As Control 
    Validate_Data = True    'initialize 
    For Each ctl In Me.Form.Controls 
     If ctl.ControlType = acCheckBox Then 
      If (ctl.Name Like "SC*") And (ctl.Value = False) Then 
       MsgBox "Not all Questions have been ticked, please tick and add comments", vbInformation, _ 
         "More information Required" 
       Validate_Data = False 'something isnt checked 
       Exit Function 
      End If 
     End If 
    Next ctl 
End Function 

Private Sub cmdGo_Click() 
    Validate_Data 
End Sub 
+0

這很有用,謝謝PeterT – Gablet 2015-04-04 08:48:22