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 Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Embedded SQL
Don Gregory 
      
4 years ago
I having a lot of trouble trying to get my embedded SQL to accept and process two date fields on the form. I've tried several things and the the fields are just ignored. Here is the code:

MySQL = ""
MySQL = "SELECT [tbl_Combined].[Case#], [tbl_Combined].[RecDate] AS [SvcDate], [tbl_Combined].[WhoToCount] "
MySQL = MySQL + "AS [ServiceTo], [tbl_Combined].[key] AS [Ky] INTO [tbl_Combined_FamilySelect] "
MySQL = MySQL + "FROM [tbl_Combined] "
MySQL = MySQL + "WHERE [tbl_Combined].[RecDate]>= " & [Forms]![frm_Report_Selection]![StartDate] & "
MySQL = MySQL + "AND [tbl_Combined].[RecDate]<= " & [Forms]![frm_Report_Selection]![EndDate] & "
MySQL = MySQL + "AND [tbl_Combined].[WhoToCount]= 'Family' "
MySQL = MySQL + "ORDER BY [tbl_Combined].[Case#], [tbl_Combined].[RecDate];"


What syntax do I need to use to get SQL to read the start date and end date?
Adam Schwanz  @Reply  
           
4 years ago
Have you tried adding # around the date fields?
Adam Schwanz  @Reply  
           
4 years ago
MySQL = MySQL + "WHERE [tbl_Combined].[RecDate]>=#" & [Forms]![frm_Report_Selection]![StartDate] & "
MySQL = MySQL + "# AND [tbl_Combined].[RecDate]<=#" & [Forms]![frm_Report_Selection]![EndDate] & "
MySQL = MySQL + "# AND [tbl_Combined].[WhoToCount]= 'Family' "
Don Gregory OP  @Reply  
      
4 years ago
I tried it as you've shown above and it's still ignoring the start and end dates.
Alex Hedley  @Reply  
            
4 years ago
Can you Debug.Print MySQL
Adam Schwanz  @Reply  
           
4 years ago
Yea do a Debug.Print or add a line below that

Msgbox MySQL

and Snip & Sketch it here
Richard Rost  @Reply  
          
4 years ago
Yeah, looks OK. Gotta see it.
Don Gregory OP  @Reply  
      
4 years ago
The following code worked in the sense that VBA interpreted it correctly. The debug.print view confirmed it.
MySQL = ""
MySQL = "SELECT [tbl_Combined].[Case#], [tbl_Combined].[RecDate] AS [SvcDate], [tbl_Combined].[WhoToCount] "
MySQL = MySQL + "AS [ServiceTo], [tbl_Combined].[key] AS [Ky] INTO [tbl_Combined_FamilySelect] "
MySQL = MySQL + "FROM [tbl_Combined] "
MySQL = MySQL + "WHERE [tbl_Combined].[RecDate]>= #" & [Forms]![frm_Report_Selection]![StartDate] & "#" & " "
MySQL = MySQL + "AND [tbl_Combined].[RecDate]<= #" & [Forms]![frm_Report_Selection]![EndDate] & "#" & " "
MySQL = MySQL + "AND [tbl_Combined].[WhoToCount]= 'Family' "
MySQL = MySQL + "ORDER BY [tbl_Combined].[Case#], [tbl_Combined].[RecDate];"
DoCmd.RunSQL MySQL

The debug.print view won't fit here. I'll submit it next...
Don Gregory OP  @Reply  
      
4 years ago
debug.print view?
SELECT [tbl_Combined].[Case#], [tbl_Combined].[RecDate] AS [SvcDate], [tbl_Combined].[WhoToCount] AS [ServiceTo], [tbl_Combined].[key] AS [Ky] INTO [tbl_Combined_FamilySelect] FROM [tbl_Combined] WHERE [tbl_Combined].[RecDate]>= #1/1/2021# AND [tbl_Combined].[RecDate]<= #1/31/2021# AND [tbl_Combined].[WhoToCount]= 'Family' ORDER BY [tbl_Combined].[Case#], [tbl_Combined].[RecDate];

The problem I'm having now is inconsistent results. This thing uses Short Dates. Start and End Dates are defined on the form as short dates and all the dates in the db are short dates. Sometimes the query works and other times I get zero results, or the query spits out a table based on the previous start and end dates enterered. It's wierd.
Don Gregory OP  @Reply  
      
4 years ago
I'm wondering if I need to add something to the VB code to clear out a buffer or something so that the user can enter as many date criteria as they want and rerun the report.
Don Gregory OP  @Reply  
      
4 years ago
P.S. I run into the same problem when I run this with DoCmd.OpenQuery, so I'm thinking the problem has something to do with the dates.
Alex Hedley  @Reply  
            
4 years ago
If you copy that into a new Query SQL view and run it does it work?
Alex Hedley  @Reply  
            
4 years ago
Is your field really called Case#?
I'd rename that to CaseNo or CaseNum or CaseNumber
Richard Rost  @Reply  
          
4 years ago
Yeah, that's a good idea, Alex, but as long as he has the brackets, that wouldn't cause the problem. I dunno. Without seeing it in action, it's very hard to tell. Nothing is jumping out at me.
Don Gregory OP  @Reply  
      
4 years ago
When I run it from the Access query editor (right-clicking on the query and bringing up under SQL design view), I can run it every time correctly. I am prompted to enter the dates as I should and the results are correct entering different dates every time. It's only in VBA that I'm running into the problem, whether as an SQL sting or as DoCmd.OpenQuery.
I noticed when I used debug.print and looked at the SQL in the immediate window that I would see the SQL listed multiple times as I tested--entering different dates multiple times. That's why I was wondering if there was VBA code I have to add that would basically clear out any buffers or storage areas that VBA uses for SQL strings. I made sure MySQL = "". Is there something more I should be doing? Can't find anything about that as I Google for answers.
Richard Rost  @Reply  
          
4 years ago
Oh, OK... I see the disconnect here... you can't use DoCmd.RunSQL to run a SELECT query to view records. RunSQL is only good for action queries (insert, update, delete, etc.) If you want to VIEW these records, you'll need to make a form and then set the RecordSource property of your form to the SQL statement. I didn't realize that's what you were trying to do. Again, that's why it's easier if we can see screen shots of your database. See Record Source Property.
Richard Rost  @Reply  
          
4 years ago
And DoCmd.OpenQuery has to open an actual saved query.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access 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: 8/18/2026 8:38:46 AM. PLT: 0s