Connecting Tech Pros Worldwide Forums | Help | Site Map

Using a list box as input fields in a query

Newbie
 
Join Date: Apr 2007
Posts: 8
#1: Apr 4 '07
Hi There,

I have a table of employees and the certificates they have. The fields in the table are all the possible certificates one can have and the records are set up as yes/no fields for if they have the certificate or not. I am trying to create a query that selects multiple fields (certificates) from the table using a list box with all the possible certificates and show me which employees have the selected certificates.
I have setup a form with the populated listbox from the certificate fields I have, but I cannot seem to link it to the query I am trying to run. I want the query to only show the employees that have the certificates that the user selects from the listbox.

Please Help!

msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,884
#2: Apr 5 '07

re: Using a list box as input fields in a query


You will need to create the query programmatically. Create a query based on anything you like called qryDummy. Make sure that the Microsoft DAO library is checked in the references list. Then something like ...
Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim qdf As DAO.QueryDef
  3. Dim valSel As Variant
  4. Dim strSQL As String
  5.  
  6.    strSQL = "SELECT EmpID, EmpName FROM TableName WHERE "
  7.    For Each valSel In Me!ListboxName.ItemsSelected
  8.       strSQL = strSQL & "[" & Me!ListboxName.ItemData(valSel) & "]=True OR "
  9.    Next valSel
  10.  
  11.    strSQL = Left(strSQL, Len(strSQL)-3 ' remove last OR from statement
  12.  
  13.    Set db = CurrentDB
  14.    Set qdf = db.QueryDefs("qryDummy")
  15.     qdf.SQL = strSQL
  16.  
  17.    qdf.Close
  18.    Set db = Nothing
  19.  
  20. End Sub
  21.  
Mary
Newbie
 
Join Date: Apr 2007
Posts: 8
#3: Apr 5 '07

re: Using a list box as input fields in a query


Thanks Mary!

Now that the list box is working properly, do you know how to get the selected listbox fields to show in the results as well in the line:

strSQL = "SELECT Name, selected listbox items FROM tblEmployeeCertification WHERE "

Regards,

Jeff
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,884
#4: Apr 6 '07

re: Using a list box as input fields in a query


Try this ...
Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim qdf As DAO.QueryDef
  3. Dim valSel As Variant
  4. Dim strSQL As String
  5. Dim strCriteria As String
  6.  
  7.    strSQL = "SELECT Name, " 
  8.  
  9.    For Each valSel In Me!ListboxName.ItemsSelected
  10.      strSQL = strSQL & Me!ListboxName.ItemData(valSel) & ", "
  11.      strCriteria = strCriteria & "[" & Me!ListboxName.ItemData(valSel) & "]=True OR "
  12.    Next valSel
  13.  
  14.    strSQL = Left(strSQL, Len(strSQL)-2 ' remove last , from statement
  15.    strCriteria = Left(strCriteria, Len(strCriteria)-3 ' remove last OR from statement
  16.    strSQL = strSQL & " FROM tblEmployeeCertification WHERE " & strCriteria
  17.  
  18.    Set db = CurrentDB
  19.    Set qdf = db.QueryDefs("qryDummy")
  20.     qdf.SQL = strSQL
  21.  
  22.    qdf.Close
  23.    Set db = Nothing
  24.  
  25. End Sub
  26.  
Mary
Newbie
 
Join Date: Apr 2007
Posts: 8
#5: Apr 9 '07

re: Using a list box as input fields in a query


I seem to be getting a run-time error '3075' missing opererator in query expression. I can't seem to find it.
Newbie
 
Join Date: Apr 2007
Posts: 8
#6: Apr 9 '07

re: Using a list box as input fields in a query


Quote:

Originally Posted by Jeff9458526

I seem to be getting a run-time error '3075' missing opererator in query expression. I can't seem to find it.

I have spaces in some of my field names which seems to be messing it up.
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,884
#7: Apr 10 '07

re: Using a list box as input fields in a query


OK, this should work ...
Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim qdf As DAO.QueryDef
  3. Dim valSel As Variant
  4. Dim strSQL As String
  5. Dim strCriteria As String
  6.  
  7.    strSQL = "SELECT Name, " 
  8.  
  9.    For Each valSel In Me!ListboxName.ItemsSelected
  10.      strSQL = strSQL & "[" & Me!ListboxName.ItemData(valSel) & "], "
  11.      strCriteria = strCriteria & "[" & Me!ListboxName.ItemData(valSel) & "]=True OR "
  12.    Next valSel
  13.  
  14.    strSQL = Left(strSQL, Len(strSQL)-2 ' remove last , from statement
  15.    strCriteria = Left(strCriteria, Len(strCriteria)-3 ' remove last OR from statement
  16.    strSQL = strSQL & " FROM tblEmployeeCertification WHERE " & strCriteria
  17.  
  18.    Set db = CurrentDB
  19.    Set qdf = db.QueryDefs("qryDummy")
  20.    qdf.SQL = strSQL
  21.  
  22.    qdf.Close
  23.    Set db = Nothing
  24.  
  25. End Sub
  26.  
Mary
Newbie
 
Join Date: May 2009
Posts: 1
#8: May 14 '09

re: Using a list box as input fields in a query


Hey everyone,

This problem is similar to what I am trying to make. I have the same problem, but instead of using one table with yes/no records, I used a "many-to-many" relationship, with one table for the employees, one table for the certificates and a third table with records containing the employee ID and certificate ID (plus some extra information on when this certificate was obtained).
In this way, it is easier to update the list of certificates.

Is there a way to achieve the same thing? make a listbox containing the list of certificates and have a query display all the Employee names which are connected to at least all the certificates selected?

I don't know if it is similar in way of working, but it's worth the try.

Thanks in advance!
Reply