Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using List Boxes to gather query parameters

Status
Not open for further replies.

SalGal

Technical User
Nov 22, 2002
24
US
New to VBA - I am working on a database to pull demographic information. This is what I have so far:

1. query to limit the selection of campuses (in other words, only include the campuses that are active)called qry_active_campus; this is the source for the list box
2. query that uses will use the information from the list box and a student_enroll table to give me student name, address, campus, grade level, etc. (query is called student_info)
3. query that uses the query from #2 to do an unmatched query against a list of students with information restrictions (query called student_info without matching restriction_code)
4. Form with two list boxes - one to choose one or more campuses and one to choose one or more grade levels (so they can choose High School #1, but only grades 10 and 11).
5. Code in the "OK" button on the form that reads like this:

Private Sub cmdOK_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("student_info")
For Each varItem In Me!cbocampus.ItemsSelected
strCriteria = strCriteria & ",'" & Me!cbocampus.ItemData(varItem) & "'"
Next varItem
If Len(strCriteria) = 0 Then
MsgBox "Please select one or more campus codes" _
, vbExclamation, "No campuses were selected!"
Exit Sub
End If
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
strSQL = "SELECT * FROM student_info" & _
"WHERE student_info.campus_id IN (" & strCriteria & ");"
qdf.SQL = strSQL
DoCmd.OpenQuery "student_info"
Set db = Nothing
Set qdf = Nothing
End Sub

I am getting a "circular reference caused by 'student_info'. I don't understand what's wrong! I did this from an online tutorial, and am trying to apply my names to this code. Somewhere I'm failing. Please let me know if I am not clear in my question.
 
I don't understand what's wrong!
A query named student_info can't have student_info in the FROM clause !

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top