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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array as query parameter 1

Status
Not open for further replies.

Seeff

Programmer
Dec 2, 2002
33
IL
Hi....I have an array populated with values. I would like to use these values as criteria for a query. How do I do this?

eg. My table (300 records)has a Code field with 20 different unique values. The user has picked from a listbox 3 different values (eg. 5,8 and 15). I have them in an array. Now, what is the code I use to select all the records where code is 5 or 8 or 15???

Thanks...L
 
Don't know if this will help. Here is an example of building an sql string from selected items in a list box.

Dim varItem
Dim strSQL As String
Dim strSelection As String

'First part of SQL string
strSQL = "SELECT * FROM tblName "

'Loop through all selected items in listbox
For Each varItem In lstList.ItemsSelected
'if first item
If strSelection = vbNullString Then
'add WHERE table name, field name and data from column one in listbox
strSelection = strSelection & " WHERE tblName.Name = '" & lstList.Column(1, varItem) & "'"
Else
'add AND table name, field name and data from column one in listbox
strSelection = strSelection & " AND tblName.Name = '" & lstList.Column(1, varItem) & "'"
End If
Next varItem

MsgBox strSQL & strSelection

There are two ways to write error-free programs; only the third one works.
 
ok...that is great, I have the correct SQL statement. Now, how and where do I run it to be able to view the resulting recordset.

Thanks
 
It kind of depends on how you want to view it and what you want to do with it..??

There are two ways to write error-free programs; only the third one works.
 
BTW

strSelection = strSelection & " AND tblName.Name = '" & lstList.Column(1, varItem) & "'"

should be

strSelection = strSelection & " OR tblName.Name = '" & lstList.Column(1, varItem) & "'"

There are two ways to write error-free programs; only the third one works.
 
Yes, I picked up on the OR instead of AND.

I want to, at this stage perform the SQL on the table, simply like an ordinary query with a datasheet view.
 
If you just want to view the results, probably the easiest way would be to have another listbox and use the SQL to set the rowsource

eg.

lstResults.RowSource = strSQL & strSelection

There are two ways to write error-free programs; only the third one works.
 
OK!!

That is it, works perectly. Thanks for your Gholden help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top