Search code examples
vbams-accesscomboboxfilteringcascadingdropdown

Can't get the cascading combo boxes to work


I am trying to make cascading combo boxes but I can't seem to get it to work. For example, if I select a specific computer in the first combo box, then the second combo box should only show the HDD that is compatible with that computer. I also provided a link to the database that I have created. Can anyone help me out with this?

I have 2 tables with the fields:

  • tblComputer(Computer)
  • tblHDD(HDD, Computer)

cboxComputer Row Source: SELECT tblComputer.Computer FROM tblComputer;

cboxHDD Row Source: SELECT tblHDD.HDD, tblHDD.Computer FROM tblHDD;

Private Sub cboxComputer_AfterUpdate()
    Me.cboxHDD.RowSource = "SELECT HDD " & _
                           "FROM tblHDD " & _
                           "WHERE Computer = " & Nz(Me.cboxComputer) & _
                           "ORDER BY HDD"
End Sub

https://drive.google.com/file/d/0Bye-M8FI1tRURmQ0MEFzRjBCdWM/view?usp=sharing


Solution

  • The Computer field in the database is a string datatype. Try putting apostrophes around the name like this:

    Private Sub cboxComputer_AfterUpdate()
        Me.cboxHDD.RowSource = "SELECT HDD " & _
                           "FROM tblHDD " & _
                           "WHERE Computer = '" & Nz(Me.cboxComputer) & "' " & _
                           "ORDER BY HDD"
    End Sub