Hi,
I can't for the life of me see what is wrong with my code as to my mind it is the same as another chunk of code i have that works so thought I would put it here and hope someone can point out what is going on.
I am trying to filter records on a subform through 2 multi select list boxes. (actually there are more than two, but for the purpose of this post the rest can be ignored)
The list boxes are both 2 columns pouplated from indevidual simple tables, the first (bound column) containing the item ID and the second the info to display. The field in the subform are comboboxes which are puopulated from the same tables that populate the listboxes.
The code I am using is as follows.
The section below works and gives the recod source statement beow when only one option is selected:
The section below now, does not work, giving me a runtime error 2001
It gives the record source statement below when only one option is selected
I can't see how this latter statement is any different to the one above that functions perfectly.
What I have done/tried:
I checked that the format of the listboxes on the main form and the combo boxes on the subform are the same, same cound column etc.
Checked all my spelling
Compact and Repair
modified
to
to return the ID numbers in the string instead of the text
Checked the data type of the underlying fields.
I can't for the life of me see why one works perfectally and the other keeps having fits. If anyone could suggest anything else I may have missed it would be greatly appreciated
Obviously using the IN statement is a bit redundant when only a single option is selected, however I went this route as it was suggested as working well for multipule selections in another post I was viewing.
I can't for the life of me see what is wrong with my code as to my mind it is the same as another chunk of code i have that works so thought I would put it here and hope someone can point out what is going on.
I am trying to filter records on a subform through 2 multi select list boxes. (actually there are more than two, but for the purpose of this post the rest can be ignored)
The list boxes are both 2 columns pouplated from indevidual simple tables, the first (bound column) containing the item ID and the second the info to display. The field in the subform are comboboxes which are puopulated from the same tables that populate the listboxes.
The code I am using is as follows.
The section below works and gives the recod source statement beow when only one option is selected:
Code:
SELECT * FROM qryFiltered WHERE [Business_Sector] IN ("EO");
Code:
strIn = ""
If Me.lsBusArea.ItemsSelected.count > 0 Then
For Each iSelected In Me.lsBusArea.ItemsSelected
strIn = strIn & """" & Me.lsBusArea.Column(1, iSelected) & """, "
Next iSelected
lngLen = Len(strIn) - 2 'Without trailing comma and space.
strWhere = strWhere & "[Business_Sector] IN (" & Left$(strIn, lngLen) & ") AND "
End If
strWhere = Left(strWhere, Len(strWhere) - 5) ' & ")"
Me![sfrmFlexibleComparisms].Form.RecordSource = "SELECT * FROM qryFiltered WHERE " & strWhere & ";"
The section below now, does not work, giving me a runtime error 2001
Code:
strIn = ""
If Me.lsStatus.ItemsSelected.count > 0 Then
For Each iSelected In Me.lsStatus.ItemsSelected
strIn = strIn & """" & Me.lsStatus.Column(1, iSelected) & """, "
Next iSelected
lngLen = Len(strIn) - 2 'Without trailing comma and space.
strWhere = strWhere & "[Status] IN (" & Left$(strIn, lngLen) & ") AND "
End If
strWhere = Left(strWhere, Len(strWhere) - 5) ' & ")"
Me![sfrmFlexibleComparisms].Form.RecordSource = "SELECT * FROM qryFiltered WHERE " & strWhere & ";"
It gives the record source statement below when only one option is selected
Code:
SELECT * FROM qryFiltered WHERE [Status] IN ("Prospect");
I can't see how this latter statement is any different to the one above that functions perfectly.
What I have done/tried:
I checked that the format of the listboxes on the main form and the combo boxes on the subform are the same, same cound column etc.
Checked all my spelling
Compact and Repair
modified
Code:
strIn = strIn & """" & Me.lsStatus.Column(1, iSelected) & """, "
Code:
strIn = strIn & """" & Me.lsStatus.itemdata(iSelected) & """, "
Code:
SELECT * FROM qryFiltered WHERE [Status] IN ("1");
Checked the data type of the underlying fields.
I can't for the life of me see why one works perfectally and the other keeps having fits. If anyone could suggest anything else I may have missed it would be greatly appreciated
Obviously using the IN statement is a bit redundant when only a single option is selected, however I went this route as it was suggested as working well for multipule selections in another post I was viewing.