过滤选择了孩子的父母

时间:2015-01-12 13:17:53

标签: ms-access ms-access-2007 ms-access-2010

我有一个包含产品的表和一个包含组件的表。每个产品都有很多组件,所以它们都是通过捆绑产品加入的。表

tblProducts
PID,描述
1,阿尔法 2,布拉沃
3,查理

tblComponents
CID,描述,类别
11,Apple,Cat1
12,香蕉,Cat2
13,Orange,Cat3

tblBundles
PID,CID
1,11
1,12 1,13
2,12

我需要创建一个包含多个列表框(基于tblComponents.Category)的表单,这将允许我最终得到过滤的产品列表。例如选择Banana并留下产品1和2.然后选择Orange并留下产品1。

我怎样才能得到这个?

1 个答案:

答案 0 :(得分:0)

如果你想创建级联组合框,你可以这样做:

cmbComp是父组合框,cmbProd是基于所选组件填充的子项。

在cmbComp的AfterUpdate事件中,您必须创建Record源字符串以填充第二个组合。

Private Sub cmbComp_AfterUpdate()
    Dim qdf As QueryDef, strSQL As String
    Dim i As Integer
    Dim qryName As String

    '-------------------------------------------------
    ' Delete all combobox items
    '-------------------------------------------------
    Me.cmbProd.RowSource = ""
    Me.cmbProd.Requery

    '-------------------------------------------------
    ' Define a name for the query
    '-------------------------------------------------
    qryName = "qryProducts"

    '-------------------------------------------------
    ' Delete query if already existing
    '-------------------------------------------------
    For Each qdf In CurrentDb.QueryDefs
        If qdf.Name = qryName Then
            CurrentDb.QueryDefs.Delete qryName
        End If
    Next qdf

    '-------------------------------------------------
    ' Create a query to use as a RowSource
    '-------------------------------------------------
    strSQL = "SELECT tbBundles.PID, " & _
             "DlookUp(""Product"",""tbProducts"",""PID=""+cstr([tbBundles.PID])) " & _
             "AS Product FROM tbBundles WHERE CID = " + CStr(Me.cmbComp)

    Set qdf = CurrentDb.CreateQueryDef(qryName, strSQL)     ' Create a query (check in the Navigation Pane)

    Set qdf = Nothing                                       ' Destroy the object qdf

    '-------------------------------------------------
    ' Set up the child combo-box
    '-------------------------------------------------
    With Me.cmbProd
        .ColumnCount = 2
        .ColumnWidths = "0;2,54"        ' Show Product as item of the list
        .RowSource = "qryProducts"      ' Set query as rowsource
        .Requery
    End With

End Sub