473,394 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Using a list box as input fields in a query

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!
Apr 4 '07 #1
7 5481
MMcCarthy
14,534 Expert Mod 8TB
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
Apr 4 '07 #2
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
Apr 5 '07 #3
MMcCarthy
14,534 Expert Mod 8TB
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
Apr 6 '07 #4
I seem to be getting a run-time error '3075' missing opererator in query expression. I can't seem to find it.
Apr 9 '07 #5
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.
Apr 9 '07 #6
MMcCarthy
14,534 Expert Mod 8TB
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
Apr 10 '07 #7
Alosja
1
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!
May 14 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Skully Matjas | last post by:
I am using the following code (created by the wizard) to allow to bring my form to a particular entery. But when I edit the entery (ex: put new information into a blank cell), it puts that record...
2
by: Tim Pollard | last post by:
Hi I'm hoping someone can help me with an access problem I just can't get my head around. I normally use access as a back end for asp pages, just to hold data in tables, so queries within access...
16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
1
by: xcelmind | last post by:
Hello Dev. Guru, I want to at this time introduce myself. I am Stanley Ojadovwa by name. I’m a freelance and a newbie in web application development. I’m currently using ASP as my application...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
1
by: bimeldip | last post by:
Hi, I have managed to create codes to display data from the database in a html page.I have gone on to create a page to allow users to manipulate the table via a html page. For instance users will be...
0
by: bimeldip | last post by:
Hi, I have managed to create codes to display data from the database in a html page.I have gone on to create a page to allow users to manipulate the table via a html page. For instance users will be...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
0
MMcCarthy
by: MMcCarthy | last post by:
Rather than using the Access design view change the view to SQL. I am going to attempt to outline the general syntax used for SQL queries in Access. Angle brackets <> are used in place of some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.