MS Access VBA表单类型在控件上不匹配

时间:2019-06-06 20:00:16

标签: vba ms-access access-vba

我正在使用Access中的一个弹出框(用户窗体),其想法是用户将检查他们希望每天执行的工作在一周中的几天。但是我在行For Each c In Me.Controls上遇到类型不匹配错误,我不知道为什么。 我可以在Excel VBA中将相同的代码与另一个用户窗体一起使用而不会出现问题,但是我猜翻译中会丢失一些内容?

这是完整的代码...

Private Sub CheckBox1_Click()
End Sub

Private Sub CheckBox2_Click()
End Sub

Private Sub CheckBox3_Click()
End Sub

Private Sub CheckBox4_Click()
End Sub

Private Sub CheckBox5_Click()
End Sub

Private Sub CheckBox6_Click()
End Sub

Private Sub CheckBox7_Click()
End Sub

Public Property Get IsCancelled() As Boolean
IsCancelled = cancelled
End Property

Private Sub OkButton_Click()
Dim c As Control
Dim StrSQL As String

For Each c In Me.Controls '<----- Errors out with Type mismatch
If TypeOf c Is MSForms.CheckBox Then
If c Then
Select Case c.Name
    Case "CheckBox1"
        'Monday
        Hide
        'Change Notice
        CurrentDb.Execute ("INSERT INTO tblTasks ([Task Name], [Task Description], [Company], [Priority], [Status], [DueDate], [User ID]) VALUES ('Change Notice', 'Daily Task', 'Ginny''s', '(2) Normal', '0', DateAdd('d',8-Weekday(Date(),2),Date()), 'Frontiera')")
        'Daily Checks
        CurrentDb.Execute ("INSERT INTO tblTasks ([Task Name], [Task Description], [Company], [Priority], [Status], [DueDate], [User ID]) VALUES ('Daily Checks', 'Daily Task', 'Ginny''s', '(2) Normal', '0', DateAdd('d',8-Weekday(Date(),2),Date()), 'Frontiera')")

    Case "CheckBox2"
        'Tuesday
        Hide
        'Change Notice
        CurrentDb.Execute ("INSERT INTO tblTasks ([Task Name], [Task Description], [Company], [Priority], [Status], [DueDate], [User ID]) VALUES ('Change Notice', 'Daily Task', 'Ginny''s', '(2) Normal', '0', DateAdd('d',8-Weekday(Date(),3),Date()), 'Frontiera')")
        'Daily Checks
        CurrentDb.Execute ("INSERT INTO tblTasks ([Task Name], [Task Description], [Company], [Priority], [Status], [DueDate], [User ID]) VALUES ('Daily Checks', 'Daily Task', 'Ginny''s', '(2) Normal', '0', DateAdd('d',8-Weekday(Date(),3),Date()), 'Frontiera')")


    Case "CheckBox3"
        'Wednesday
        Hide
        'Change Notice
        CurrentDb.Execute ("INSERT INTO tblTasks ([Task Name], [Task Description], [Company], [Priority], [Status], [DueDate], [User ID]) VALUES ('Change Notice', 'Daily Task', 'Ginny''s', '(2) Normal', '0', DateAdd('d',8-Weekday(Date(),4),Date()), 'Frontiera')")
        'Daily Checks
        CurrentDb.Execute ("INSERT INTO tblTasks ([Task Name], [Task Description], [Company], [Priority], [Status], [DueDate], [User ID]) VALUES ('Daily Checks', 'Daily Task', 'Ginny''s', '(2) Normal', '0', DateAdd('d',8-Weekday(Date(),4),Date()), 'Frontiera')")


    Case "CheckBox4"
        'Thursday
        Hide
        'Change Notice
        CurrentDb.Execute ("INSERT INTO tblTasks ([Task Name], [Task Description], [Company], [Priority], [Status], [DueDate], [User ID]) VALUES ('Change Notice', 'Daily Task', 'Ginny''s', '(2) Normal', '0', DateAdd('d',8-Weekday(Date(),5),Date()), 'Frontiera')")
        'Daily Checks
        CurrentDb.Execute ("INSERT INTO tblTasks ([Task Name], [Task Description], [Company], [Priority], [Status], [DueDate], [User ID]) VALUES ('Daily Checks', 'Daily Task', 'Ginny''s', '(2) Normal', '0', DateAdd('d',8-Weekday(Date(),5),Date()), 'Frontiera')")


    Case "CheckBox5"
        'Friday
        Hide
        'Change Notice
        CurrentDb.Execute ("INSERT INTO tblTasks ([Task Name], [Task Description], [Company], [Priority], [Status], [DueDate], [User ID]) VALUES ('Change Notice', 'Daily Task', 'Ginny''s', '(2) Normal', '0', DateAdd('d',8-Weekday(Date(),6),Date()), 'Frontiera')")
        'Daily Checks
        CurrentDb.Execute ("INSERT INTO tblTasks ([Task Name], [Task Description], [Company], [Priority], [Status], [DueDate], [User ID]) VALUES ('Daily Checks', 'Daily Task', 'Ginny''s', '(2) Normal', '0', DateAdd('d',8-Weekday(Date(),6),Date()), 'Frontiera')")


    Case "CheckBox6"
        'Saturday
        Hide
         'Change Notice
        CurrentDb.Execute ("INSERT INTO tblTasks ([Task Name], [Task Description], [Company], [Priority], [Status], [DueDate], [User ID]) VALUES ('Change Notice', 'Daily Task', 'Ginny''s', '(2) Normal', '0', DateAdd('d',8-Weekday(Date(),7),Date()), 'Frontiera')")
        'Daily Checks
        CurrentDb.Execute ("INSERT INTO tblTasks ([Task Name], [Task Description], [Company], [Priority], [Status], [DueDate], [User ID]) VALUES ('Daily Checks', 'Daily Task', 'Ginny''s', '(2) Normal', '0', DateAdd('d',8-Weekday(Date(),7),Date()), 'Frontiera')")


    Case "CheckBox7"
        'Sunday
        Hide
         'Change Notice
        CurrentDb.Execute ("INSERT INTO tblTasks ([Task Name], [Task Description], [Company], [Priority], [Status], [DueDate], [User ID]) VALUES ('Change Notice', 'Daily Task', 'Ginny''s', '(2) Normal', '0', DateAdd('d',8-Weekday(Date(),8),Date()), 'Frontiera')")
        'Daily Checks
        CurrentDb.Execute ("INSERT INTO tblTasks ([Task Name], [Task Description], [Company], [Priority], [Status], [DueDate], [User ID]) VALUES ('Daily Checks', 'Daily Task', 'Ginny''s', '(2) Normal', '0', DateAdd('d',8-Weekday(Date(),8),Date()), 'Frontiera')")

    Case Else
    End Select
    End If
    End If
  Next c
End Sub

Private Sub CancelButton_Click()
  OnCancel
  End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
    Cancel = True
    OnCancel
End If
End Sub

Private Sub OnCancel()
cancelled = True
Hide
End Sub

1 个答案:

答案 0 :(得分:2)

访问具有自己的控件,该控件与MSForms无关。

如果该表单是MSForms表单,请使用

Dim c As MSForms.Control
...
If TypeOf c Is MSForms.CheckBox Then
...

如果该表单是Access表单

Dim c As Control
...
If TypeOf c Is CheckBox Then
...

此外,如果仅使用Access表单,则可以在菜单工具>引用中的VBA代码编辑器中删除对MSForms的引用。