2015-04-03 65 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