Access SubForm选择取决于组合框

时间:2017-02-04 07:06:34

标签: access-vba ms-access-2010 access

我想基于我的组合框过滤器过滤我的子表单。我收到代码错误。我需要帮助。

更新后我写了一个事件:

Private Sub cboSelected_AfterUpdate()
Dim MyName As String
MyName = " select * from [ITP_Checklist Log] where ([ITP_Checklist Log].[Name] = " & Me.cboSelected & " )"

Me.ITP_Checklist_Log_subform.Form.RecordSource = MyName

Me.ITP_Checklist_Log_subform.Form.Requery

End Sub

错误:

Run-time error '3464'
Data Type Mismatch in Criteria expression.

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

为字符串值使用引号 - 只有在不更改记录源时才需要Requery:

Private Sub cboSelected_AfterUpdate()

    Dim MyName As String

    MyName = "select * from [ITP_Checklist Log] where ([ITP_Checklist Log].[Name] = '" & Me!cboSelected.Value & "')"

    Debug.Print MyName

    If Me!ITP_Checklist_Log_subform.Form.RecordSource = MyName Then
        Me!ITP_Checklist_Log_subform.Form.Requery
    Else
        Me!ITP_Checklist_Log_subform.Form.RecordSource = MyName
    End If

End Sub