Creating Listbox into "search form" that generates query - see code - almost there
Hello,
What I would like to do is create a new form which will listboxes that will allow the user to pass their multiple selections into a query. So far I have the following code however this code generates an IN criteria. What I would like for it to do is generate an LIKE AND criteria. See below for details:
Current code (which generates IN criteria) - which generates the following SQL
SELECT *
FROM Master
WHERE Master.ValueTypes IN('Economic - Market Value','Economic -Non-market Value');
VB code used:
Option Compare Database
Option Explicit
Private Sub cmdOK_Click()
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
' Get the database and stored query
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryMultiSelect")
' Loop through the selected items in the list box and build a text string
For Each varItem In Me!mslbValueTypesQry.ItemsSelected
strCriteria = strCriteria & ",'" & Me!mslbValueTypesQry.ItemData(varItem) & "'"
Next varItem
' Check that user selected something
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation, "Nothing to find!"
Exit Sub
End If
' Remove the leading comma from the string
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
' Build the new SQL statement incorporating the string
strSQL = "SELECT * FROM Master " & _
"WHERE Master.ValueTypes IN(" & strCriteria & ")"
' Apply the new SQL statement to the query
qdf.SQL = strSQL
' Open the query
DoCmd.OpenQuery "qryMultiSelect"
' Empty the memory
Set db = Nothing
Set qdf = Nothing
End Sub
--What I would like to do is generate a LIKE AND criteria instead (i.e: Like "*Aesthetic*" And Like "*Existence*") which generates the following SQL:
SELECT Master.FileID, Master.ValueTypes, Master.Focus, Master.Method, Master.Region, Master.YearID, Master.NumOfRespondents, Master.YearID2, Master.Title, Master.Comment, Master.QuestionAsked, Master.YearID3, Master.Author, Master.Results
FROM Master
WHERE (((Master.ValueTypes) Like "*Aesthetic*" And (Master.ValueTypes) Like "*Existence*"));
I should mention that this query has all the other fields I want to query based on this criteria (i.e Im looking for all the fileID's that relate to Aesthetic & existence)
I'm guessing I just have to change the SQL string in the code, but I've been unsuccessful at doing so.
Any Ideas?
Thank you!!!!
Hello,
What I would like to do is create a new form which will listboxes that will allow the user to pass their multiple selections into a query. So far I have the following code however this code generates an IN criteria. What I would like for it to do is generate an LIKE AND criteria. See below for details:
Current code (which generates IN criteria) - which generates the following SQL
SELECT *
FROM Master
WHERE Master.ValueTypes IN('Economic - Market Value','Economic -Non-market Value');
VB code used:
Option Compare Database
Option Explicit
Private Sub cmdOK_Click()
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
' Get the database and stored query
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryMultiSelect")
' Loop through the selected items in the list box and build a text string
For Each varItem In Me!mslbValueTypesQry.ItemsSelected
strCriteria = strCriteria & ",'" & Me!mslbValueTypesQry.ItemData(varItem) & "'"
Next varItem
' Check that user selected something
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation, "Nothing to find!"
Exit Sub
End If
' Remove the leading comma from the string
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
' Build the new SQL statement incorporating the string
strSQL = "SELECT * FROM Master " & _
"WHERE Master.ValueTypes IN(" & strCriteria & ")"
' Apply the new SQL statement to the query
qdf.SQL = strSQL
' Open the query
DoCmd.OpenQuery "qryMultiSelect"
' Empty the memory
Set db = Nothing
Set qdf = Nothing
End Sub
--What I would like to do is generate a LIKE AND criteria instead (i.e: Like "*Aesthetic*" And Like "*Existence*") which generates the following SQL:
SELECT Master.FileID, Master.ValueTypes, Master.Focus, Master.Method, Master.Region, Master.YearID, Master.NumOfRespondents, Master.YearID2, Master.Title, Master.Comment, Master.QuestionAsked, Master.YearID3, Master.Author, Master.Results
FROM Master
WHERE (((Master.ValueTypes) Like "*Aesthetic*" And (Master.ValueTypes) Like "*Existence*"));
I should mention that this query has all the other fields I want to query based on this criteria (i.e Im looking for all the fileID's that relate to Aesthetic & existence)
I'm guessing I just have to change the SQL string in the code, but I've been unsuccessful at doing so.
Any Ideas?
Thank you!!!!