I have an unbound multi-select list box that is populated from a query that displays types of buildings. I have two check boxes on the form that change the items in the list box when one or the other is set to true, only one check box can be selected at a time. One check box, when selected, displays all types of buildings except for multi-family, while the other check box displays only multi-family buildings.
My problem is if the user selects items in the listbox and then toggles one of the check boxes, the listbox is reset because of the changing of the items available to select. The challenge is to remember what the user selected in the listbox prior to toggling either one of the check boxes. For the most part, my code works to handle this but it only works when one checkbox is toggled on or off and not either or checkbox. I end up with an error that says run-time error 9: subscript out of range in my ReStoreListSele ction function. I suspect this has to do with my array and upon debugging I discovered that the correct value is not being passed back to the listbox that I desire depending on which checkbox is selected. I can't figure out what I need to change in my code to make this happen.
I call a function from a module in the after update of each checkbox to handle this scenario. My code is as follows:
Also here is the code for the AfterUpdate event of one of my checkboxes:
Any help would be greatly appreciated.
My problem is if the user selects items in the listbox and then toggles one of the check boxes, the listbox is reset because of the changing of the items available to select. The challenge is to remember what the user selected in the listbox prior to toggling either one of the check boxes. For the most part, my code works to handle this but it only works when one checkbox is toggled on or off and not either or checkbox. I end up with an error that says run-time error 9: subscript out of range in my ReStoreListSele ction function. I suspect this has to do with my array and upon debugging I discovered that the correct value is not being passed back to the listbox that I desire depending on which checkbox is selected. I can't figure out what I need to change in my code to make this happen.
I call a function from a module in the after update of each checkbox to handle this scenario. My code is as follows:
Code:
Function StoreListSelection(Lst As ListBox) As Boolean() Lst = Forms![frmSearch]![lstType] Dim lngIndex As Long Dim blnTemp() As Boolean ReDim blnTemp(Lst.ListCount - 1) For lngIndex = 0 To Lst.ListCount - 1 blnTemp(lngIndex) = Lst.Selected(lngIndex) Next StoreListSelection = blnTemp End Function Sub ReStoreListSelection(Lst As ListBox, Selected() As Boolean) Dim lngIndex As Long For lngIndex = 0 To Lst.ListCount - 1 Lst.Selected(lngIndex) = Selected(lngIndex) Next End Sub Public Function fMultiSort() Dim strSQL As String Dim Lst As ListBox Dim chkMulti, chkLease As CheckBox Dim blnSelectLease() As Boolean Dim blnSelectMulti() As Boolean 'See if we have actual Records If DCount("[Building_Type]", "tblBuilding_Type_List") = 0 Then Exit Function 'Initialize list box Set Lst = Forms![frmSearch]![lstType] 'Initialize check boxes Set chkMulti = Forms![frmSearch]![chkMulti] Set chkLease = Forms![frmSearch]![chkLease] If chkMulti = False Then strSQL = "SELECT DISTINCT [Building_Type] FROM tblBuilding_Type_list WHERE [Enable_Units] = False ORDER BY [Building_Type];" blnSelectLease = StoreListSelection(Lst) Lst.RowSource = strSQL If blnSelectLease(False) Then 'do nothing Else ReStoreListSelection Lst, blnSelectLease End If Else If chkMulti = True Then strSQL = "SELECT DISTINCT [Building_Type] FROM tblBuilding_Type_list WHERE [Enable_Units] = True ORDER BY [Building_Type];" blnSelectMulti = StoreListSelection(Lst) Lst.RowSource = strSQL If blnSelectMulti(False) Then 'do nothing Else ReStoreListSelection Lst, blnSelectMulti End If End If End If End Function
Code:
Private Sub chkMulti_AfterUpdate() If chkMulti = True Then chkLease = False Call fMultiSort cmdLease_Single.Enabled = False cmdLease_Detail.Enabled = False cmdSingle.Enabled = True cmdSummaryDetail.Enabled = True sfrmSearch.Enabled = False sfrmSearch.Visible = False sfrmLease.Visible = False sfrmLease.Enabled = False sfrmMulti.Enabled = True sfrmMulti.Visible = True If sfrmMulti.Form.RecordSource = "" Then 'Make the multi subform controls invisible sfrmMulti![txtLocation].Visible = False sfrmMulti![txtAddress].Visible = False sfrmMulti![txtType].Visible = False sfrmMulti![txtPrice].Visible = False sfrmMulti![txtDate].Visible = False sfrmMulti![txtUnits].Visible = False sfrmMulti![txtPrice_Unit].Visible = False sfrmMulti![txtGIM].Visible = False sfrmMulti![txtID].Visible = False sfrmMulti!cmdMore.Visible = False End If Else Call fMultiSort cmdSingle.Enabled = True cmdSummaryDetail.Enabled = True cmdLease_Single.Enabled = False cmdLease_Detail.Enabled = False sfrmSearch.Enabled = True sfrmSearch.Visible = True sfrmMulti.Enabled = False sfrmMulti.Visible = False sfrmLease.Visible = False sfrmLease.Enabled = False End If End Sub
Comment