Search code examples
ms-accessms-access-2016

make drop-down fields dependent of each other


I set-up a form with with project details and want to implement several drop-down options to filter project properties. These are "project-name", "customer" and "product". I want to make them dependent on each other. So when I select a particular customer, I only want to be able to choose from products which are related to the customer.

So far I was able to set-up each drop-down for each project property but they are not dependet of each other, but always show all available options. Can someone help me out with dependent drop-down fields?

Selection fields as they are now

Edit: Table layout upon request:

Rows/format
-Project ID/Number
-Customer/String
-Product/String
-Project-name/String
-Comment/String
-Feature 1/String


Solution

  • One way of doing this to to use "cascading" combo boxes, so the user moves from the top combo box towards the bottom one, each time narrowing the range available to them.

    Your VBA code would look something like this:

    Private Sub cboCustomer_AfterUpdate()
        Dim strSQL As String
        If Len(Me!cboCustomer) > 0 Then
            strSQL = "SELECT DISTINCT Product FROM tblProject " _
                & " WHERE Customer='" & Me!cboCustomer & "' " _
                & " ORDER BY Product ASC;"
            Me!cboProduct.RowSource = strSQL
        Else
            strSQL = "SELECT DISTINCT Product FROM tblProject ORDER BY Product ASC;"
        End If
    End Sub
    
    Private Sub cboProject_AfterUpdate()
        Dim strSQL As String
        If Len(Me!cboProject) > 0 Then
            strSQL = "SELECT DISTINCT Customer FROM tblProject " _
                & " WHERE ProjectID=" & Me!cboProject _
                & " ORDER BY Customer ASC;"
            Me!cboCustomer.RowSource = strSQL
            strSQL = "SELECT DISTINCT Product FROM tblProject " _
                & " WHERE ProjectID=" & Me!cboProject _
                & " ORDER BY Product ASC;"
            Me!cboProduct.RowSource = strSQL
        Else
            Me!cboCustomer.RowSource = "SELECT DISTINCT Customer FROM tblProject ORDER BY Customer ASC;"
            Me!cboProduct.RowSource = "SELECT DISTINCT Product FROM tblProject ORDER BY Product ASC;"
        End If
    End Sub
    

    Regards,