Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Help   Contact   Merch   Join   Order   Logon   Forums   
 
Back to Access Developers    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Using a Variable in SQL Where Statement
Anthony DiVincenti 
    
17 months ago
I am trying to reference a variable in an SQL WHERE statement, but I know the syntax is not correct.  The syntax must be really goofy, but I can't figure it out.

Dim selectcourse As String
selectcourse = Me.CourseLb.Value

DoCmd.OpenForm "CourseTransF"
Me.RecordSource = "SELECT * FROM TransQ WHERE CourseID = selectcoure"





Sami Shamma  @Reply  
             
17 months ago
first is CourseID Numeric? it should be.

DetailsDim selectcourse As Long
selectcourse = Me.CourseLb.Value

DoCmd.OpenForm "CourseTransF"
Me.RecordSource = "SELECT * FROM TransQ WHERE CourseID = " & selectcoure

Anthony DiVincenti OP  @Reply  
    
17 months ago
CourseID is short text.  The legacy data that I will be importing requires a string.
Kevin Robertson  @Reply  
          
17 months ago
Sami Shamma  @Reply  
             
17 months ago
Dim selectcourse As String
selectcourse = Me.CourseLb.Value

DoCmd.OpenForm "CourseTransF"
Me.RecordSource = "SELECT * FROM TransQ WHERE CourseID = """ & selectcoure & """"
Adam Schwanz  @Reply  
           
17 months ago
Another thing is your doing
Dim selectcourse As String
selectcourse = Me.CourseLb.Value

Then & selectcoure (spelling), missing the s from the variable name
Anthony DiVincenti OP  @Reply  
    
17 months ago
Thanks Adam, I caught that too.
Anthony DiVincenti OP  @Reply  
    
17 months ago
Well the form now opens but the WHERE statement is not working.  The records are not being filtered according to the WHERE statement.  I have verified that the variable has the correct value with a message box.

Private Sub CourseLb_AfterUpdate()

Dim selectcourse As String
selectcourse = Me.CourseLb.Value
MsgBox selectcourse

DoCmd.OpenForm "CourseTransF"
Me.RecordSource = "SELECT * FROM TransQ WHERE CourseID = """ & selectcourse & """"
Kevin Robertson  @Reply  
          
17 months ago
CourseLB is a List Box. Correct?
You may need to access the Column property.
Anthony DiVincenti OP  @Reply  
    
17 months ago
Yes that is a list box.  I did verify that the variable is getting the value from the list box.  Column property?
Kevin Robertson  @Reply  
          
17 months ago
You're first column will be (usually) the ID. The value you a referring to may be in the second column.
    CourseLb.Column(1)

If that is not the case, please share the Row Source of your List Box.

Kevin Robertson  @Reply  
          
17 months ago
Just re-read your code.
You are setting the Record Source of the current Form, not the Form you are opening.

Try using one line of code:
    DoCmd.OpenForm "CourseTransF",,,"CourseID=""" & CourseLB & """"
Anthony DiVincenti OP  @Reply  
    
17 months ago
DoCmd.OpenForm "CourseTransF",,,"CourseID=""" & CourseLB & """"

Kevin this worked!  I really don't understand why it worked though.
John Davy  @Reply  
         
17 months ago
As Rick once said "the old saying 'I don't know why it is not working' and after it works 'I don't know why it is working'
Great call Kevin!   John

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

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: 8/24/2026 9:30:12 AM. PLT: 0s