J
Joey C
Guest
Hi There,
I,ve searched past postings with a similar problem, but I have not find an answer to the following:
I have a multiselect listbox, and I want to use the items selected as criteria for a query.
I use a function with code like this to generate a SQL string with the values selected:
Dim strSQL As String
strSQL = "("
With Me!ListBox
For Each varItem In .ItemsSelected
strSQL = strSQL & """" & .ItemData(varItem) & """" & ")" & " Or (IdDoctor="
Next varItem
End With
'Trim the end of strSQL
strSQL = Left$(strSQL, Len(strSQL) - 15)
HiddenTextBox = strSQL
The function creates a SQL string like this: “01" Or IdDoctor ="02" Or IdDoctor ="03"
I can use this SQL string to filter a form or report (if applied to the form / report filter property in the On open method.
But I can NOT use the SQL string as criteria for a query.
I have a hidden textbox (in the same form) that receives the SQL string from the function described above, and I want to use this hidden textbox as criteria in a parameter query, but the query will not accept the string if I have more than 1 item selected in the listbox.
My query looks like this:
Select [Doctors].[DoctorName]
From Doctors
Where [IdDoctor]=[HiddenTextBox]
(TextBox = "01" Or IdDoctor ="02" Or IdDoctor ="03"
I,ve also tried the IN alternative (Where IdDoctor In(“01”, “02”, “03”), But still the query won´t take the criteria. (Only works with 1 item selected)
Creating the entire query with code won´t do, because I have another query based on this one.
I found an alternative way, which involves sending the items selected in the listbox to a table, and then I use that table as a join in my query. But this method is not the best solution, since in a multiuser environment, more than 1 person could be using the listbox to write to the table.
I would like to know if it is possible to use the hidden textbox method in a parameter query, or if this is a dead end.
Thanks in advance for your help,
Joey
I,ve searched past postings with a similar problem, but I have not find an answer to the following:
I have a multiselect listbox, and I want to use the items selected as criteria for a query.
I use a function with code like this to generate a SQL string with the values selected:
Dim strSQL As String
strSQL = "("
With Me!ListBox
For Each varItem In .ItemsSelected
strSQL = strSQL & """" & .ItemData(varItem) & """" & ")" & " Or (IdDoctor="
Next varItem
End With
'Trim the end of strSQL
strSQL = Left$(strSQL, Len(strSQL) - 15)
HiddenTextBox = strSQL
The function creates a SQL string like this: “01" Or IdDoctor ="02" Or IdDoctor ="03"
I can use this SQL string to filter a form or report (if applied to the form / report filter property in the On open method.
But I can NOT use the SQL string as criteria for a query.
I have a hidden textbox (in the same form) that receives the SQL string from the function described above, and I want to use this hidden textbox as criteria in a parameter query, but the query will not accept the string if I have more than 1 item selected in the listbox.
My query looks like this:
Select [Doctors].[DoctorName]
From Doctors
Where [IdDoctor]=[HiddenTextBox]
(TextBox = "01" Or IdDoctor ="02" Or IdDoctor ="03"
I,ve also tried the IN alternative (Where IdDoctor In(“01”, “02”, “03”), But still the query won´t take the criteria. (Only works with 1 item selected)
Creating the entire query with code won´t do, because I have another query based on this one.
I found an alternative way, which involves sending the items selected in the listbox to a table, and then I use that table as a join in my query. But this method is not the best solution, since in a multiuser environment, more than 1 person could be using the listbox to write to the table.
I would like to know if it is possible to use the hidden textbox method in a parameter query, or if this is a dead end.
Thanks in advance for your help,
Joey