Computer Learning Zone CLZ Access Excel Word Windows

You know it's true... Every disaster movie begins with a scientist being ignored.

-Neil deGrasse Tyson
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Visitor Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
VBA Query Using IN
Jeff Dombach 

3 years ago
My VBA query:
SQL = "SELECT * FROM tblOptionBaseDataRules WHERE '" & selectedPart & "' IN (gExpandID([ID]))"

selectedpart is '18461'

[ID]  '18460-18462'

gExpandID function returns  '18460','18461','18462'

I expect a result record for this record and any record where [ID] = '18461' however it is only returning records where ID is '18461' and appearing to ignore '18460-18462.

Any thoughts?

Kevin Yip  @Reply  
     
3 years ago
If gExpandID() returns a string, then that's the problem right there -- when you use IN (s), s is *not* a string, but SQL syntax that represents a collection of items.  So the proper way to write that query is to use *string concatenation* to construct the proper SQL syntax with the result from gExpandID():

    Dim ID As String
    ID = "18460-18462"
    SQL = "SELECT * FROM tblOptionBaseDataRules WHERE '" & selectedPart & "' IN (" & gExpandID(ID) & ")"

Note that ID is now a VBA variable, not a field name in the query.
Kevin Yip  @Reply  
     
3 years ago
And if you must use your ID field in your query, consider splitting them into two fields, ID1 and ID2, representing the lower and upper limits ("18460" and "18462").  Then you can write:

    SQL = "SELECT * FROM tblOptionBaseDataRules WHERE '" & selectedPart & "' Between ID1 And ID2;"

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Visitor Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/8/2026 12:27:49 AM. PLT: 0s