SQL Statement Problem with List Box

seenfresh

Well-known Member
Joined
Jul 12, 2006
Messages
1,141
I have a Listbox with Extended selection enable and called List4

I also have a Query called UPLOAD Query and Table called UPLOAD

I have a code that will enter in a SQL statement into my UPLOAD Query based on the varItems selected in my List Box: List4

When I run this code the query pops up with nothing.... I noticed that the criteria in the query is referencing numbers like ex. IN('7','6') etc instead of the actual data of the items selected form the List Box...? ex IN("Specific Media","KSL")...

Here is my code:



Code:
Option Compare Database

Private Sub Command8_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("UPLOAD Query")


For Each varItem In Me!List4.ItemsSelected

    strCriteria = strCriteria & ",'" & Me!List4.ItemData(varItem) & "'"
    
Next varItem

If Len(strCriteria) = 0 Then

    MsgBox "You did not select anything from the list", vbExclamation, "Nothing to find!"

Exit Sub

End If

strCriteria = Right(strCriteria, Len(strCriteria) - 1)

strSQL = "SELECT * FROM UPLOAD " & "WHERE UPLOAD.[Payable To:] IN(" & strCriteria & ");"

qdf.SQL = strSQL

DoCmd.OpenQuery "UPLOAD Query"


End Sub

And this is the SQL statement that appears in the UPLOAD Query after I run the code:

Code:
SELECT *
FROM UPLOAD
WHERE UPLOAD.[Payable To:] IN('7','6');

Can anyone spot where my problem lies??

Any suggestions would be greatly appreciated :eek:
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I believe your problem is that you should not be putting the single quotes on each side of the numbers in your criteria. Therefore, the code to create your strCriteria field should look like this:

strCriteria = strCriteria & ", " & Me!List4.ItemData(varItem)
 
Upvote 0
Vic,

Access will accept single quotes in replace of double quotes (Char(34)) to enclose the criteria

The main problem is that my ItemData should be in the form of a string not a number... the number is prob the variable reference to the Data in the List box...

Changing the quotes would just result in IN(7,6) instead of IN('7','6')

Am I being unclear?


Thank you for your response
 
Upvote 0
No, you are being very clear. I believe the numbers that you are getting are the ID numbers to the records you want included in your query. Therefore, if you have the single quotes around them, it is not going to work. Have you tried it without the quotes? If you have, or do try it without the quotes, please let us know what error message you get.

Also, can you show us the SQL for the List Box? That would also help.
 
Upvote 0
Vic,

Your right... I noticed that my ListBox was column bound to column 1 instead of column 2, thus it was picking up the ID numbers instead of the actual data! I changed this and it works perfectly!

Sorry for the whole fiasco! The simplest thing sometimes is the hardest to find!

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top