访问VBA - 已检查表单上的所有复选框

时间:2015-04-03 17:02:51

标签: vba access-vba

我对Access VBA相对较新,并且有一个表格上有大约30个复选框。保存表单时,我想确保已勾选所有复选框(设置为true)。复选框都有名称SC1,SC2 .... SCN有没有办法循环每个控件,看看它是否已设置为true? 这是我尝试过但它似乎没有读取复选框 -

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

1 个答案:

答案 0 :(得分: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