Query Design - Select Items Not in Array

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
I need to query table contents where some general items are met, and item id's are not contained in an array.

Example:
Code:
SELECT LOOKUP.*, LOOKUP.Var FROM LOOKUP WHERE ((LOOKUP.Name)="test" Or (LOOKUP.Name)="test2" AND ((LOOKUP.Type)=3) AND ((LOOKUP.Var) <> ("array", "of", "items")))
Does this make any sense? I need to know how to add an array to a query since I can't hard-code the list each time, and the array can be a different size each time, so using array variables doesn't make sense here. Thanks
 
What was your SQL without variables? And what's assigned to txtLeaseList2?

Yeah, I got the query to execute without the variable. But when I add it, then it says type mismatch.

heres the code for my variable:

Code:
wrkRowCount = 9  'start row    
    Do While wrkRowCount <= 40 'loop until last        
          'txtLeaseList = txtLeaseList & """" & Application.Sheets("MASTER").Range("A" & wrkRowCount).Value & ""","
          txtLeaseList = txtLeaseList & "'" & Application.Sheets("MASTER").Range("A" & wrkRowCount).Value & "',"
        wrkRowCount = wrkRowCount + 1 'increment
    Loop

I've been trying a few different ways of including items in the string. Examples:

each, item, no, quotes
"each", "item", "with", "quotes"
'each', 'item', ...etc.

none of these work.
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You didn't answer my questions.

SQL without the variable (executes successfully) =
Code:
        "SELECT LOOKUP.Name, LOOKUP.Type, LOOKUP.Var " _
        , " FROM ABC.LOOKUP LOOKUP" _
        , " WHERE (LOOKUP.Type=3) AND (LOOKUP.Name In ('CAR', 'BIKE', 'SKATE')) " _
        , " ORDER BY LOOKUP.Var")

Variable contains 6 character numbers formatted as text, like: 001122, 334455, 556677 ...
 
Upvote 0
I don't see 'Not in' there.

Yeah, the above is the SQL that works (before adding in any additional conditions to the query). The additional condition adds : AND (LOOKUP.Var Not In (" & txtLeaseList2 & ")) ... but VBA doesn't like this. If I add static values to the additional condition mentioned above, it works too. The problem is when I try to incorporate the string of values.

My variable contains: "001122, 334455, 667788" and so on. So you would think it makese sense you could stick that inside this: Not In ( ) and it would work.

The error changes depending on how I separate each variable within the string. If I include quotes, and ' , then VBA kicks out a Run-time error '13' - Type mismatch.

If I have no " or ' separating them, then VBA kicks out a "General ODBC Error."
 
Upvote 0
Yes I can. The variable or how I'm including the variable seems to be the problem. I don't know for sure. Can't crack this one so far.
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,099
Members
452,379
Latest member
IainTru

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