views:

147

answers:

1

Hi there,

I'm having problems with a cascading combo box. Everything works fine with the combo boxes and the values get populated correctly.

Private Sub cmbAdjComp_AfterUpdate()
Me.cboAdjOff.RowSource = "SELECT AdjusterCompanyOffice.ID, 
AdjusterCompanyOffice.Address1, 
AdjusterCompanyOffice.Address2, 
AdjusterCompanyOffice.Address3, 
AdjusterCompanyOffice.Address4, 
AdjusterCompanyOffice.Address5 FROM" & _
" AdjusterCompanyOffice WHERE 
AdjusterCompanyOffice.AdjCompID = " & Me.cmbAdjComp.Column(1) & _
" ORDER BY AdjusterCompanyOffice.Address1"
Me.cboAdjOff = Me.cboAdjOff.ItemData(0)
End Sub

The secondary combo box has a row source query:

SELECT AdjusterCompanyOffice.ID, AdjusterCompanyOffice.Address1,    
AdjusterCompanyOffice.Address2, AdjusterCompanyOffice.Address3,
 AdjusterCompanyOffice.Address4, AdjusterCompanyOffice.Address5 FROM 
 AdjusterCompanyOffice ORDER BY AdjusterCompanyOffice.Address1;

Both comboboxes have the same controlsource.

Everything works fine and dandy moving between records and the boxes show the correct fields for each record.

When i use the first combo box, and then select the appropriate option in the second combo box, everything works great on the specific record.

However when I move to the next record, the values in the second combo box are all empty. If i close the form and reopen it, and avoid using the cascading combo boxes all the values are all correct when i move between records.

Somehow using the cascading combo boxes creates a conflict with the row source of the secondary combo box.

Hope that is clear! Have been rummaging around for an answer but cant find anything.

any help would be greatly appreciated.

Thanks

Noel

A: 

The reason why the combo box is blank when you navigate to the next record is because you have NotInList set to TRUE (which is what you want), but when you arrive on the record, the rowsource has been filtered to not include the value stored in the field the combo box is bound to. Thus, it's blank -- the value is there, but it can't be displayed, since it's not in the list.

To fix this, you need to clear the filter on the second combo box. To do that, in the OnCurrent event of your form, set the rowsource of the filtered combo box to be unfiltered:

  Me!cboAdjOff.RowSource = "SELECT AdjusterCompanyOffice.ID, AdjusterCompanyOffice.Address1, AdjusterCompanyOffice.Address2, AdjusterCompanyOffice.Address3, AdjusterCompanyOffice.Address4, AdjusterCompanyOffice.Address5 FROM AdjusterCompanyOffice ORDER BY AdjusterCompanyOffice.Address1"

I usually handle this by creating two constants at the top of the form's module, one for the SELECT statement and one for the ORDER BY:

  cstrRecordsourceSelect = "SELECT AdjusterCompanyOffice.ID, AdjusterCompanyOffice.Address1, AdjusterCompanyOffice.Address2, AdjusterCompanyOffice.Address3, AdjusterCompanyOffice.Address4, AdjusterCompanyOffice.Address5 FROM AdjusterCompanyOffice"
  cstrRecordsourceOrderBy = "ORDER BY AdjusterCompanyOffice.Address1"

Then it's much easier to handle. In the OnCurrent it looks like this:

  Me!cboAdjOff.RowSource = cstrRecordsourceSelect & " " & cstrRecordsourceSelect 

...and in the AfterUpdate of your first combo box:

  Me!cboAdjOff.RowSource = cstrRecordsourceSelect & _
    "WHERE AdjusterCompanyOffice.AdjCompID = " & Me!cmbAdjComp.Column(1) & _
    " " & cstrRecordsourceSelect

This makes the code easier to read, and it also makes it easier to alter the rowsource if you need to, since you have to edit only the constant.

David-W-Fenton
Hi David, this is exactly what I was trying to understand. I didn't think it would require as much effort, so to speak, but along with your help I have got it working. Thanks a lot for taking the time to answer, greatly appreciated. Noel
glinch