Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Home > TechHelp > Directory > Access > Delete Sample Data < Select in Subform | Import w Followup >
Back to Delete Sample Data    Comments List
Upload Images   @Reply   Bookmark    Link   Email  
WHERE with Date criteria
Daniel de Koning 
     
2 years ago
I've been trying to create a delete query using the SQL statement, but for some reason it's not working. I initially set the criteria with a field on my MainMenu, but since that didn't work, I entered the date directly into the code (see below) and it's still not working correctly/as intended. I've entered lines with an Expected date of 11/Mar and 25/Feb, but the 1st line is not deleted and when I change the criteria to 2023-03-01 all lines are deleted, instead of only the March data.
     (NOTE: English vba code and ISO Date format.)
    CurrentDb.Execute "DELETE * FROM PurchaseDetailT WHERE DateExpected > #2023-03-10#"
I've searched for videos for the last couple of hours to see an example with 'vba date criteria' and I'm now giving up. Creating the Delete Query and modifiying it into an RunSQL statement works, but I was hoping to use the new currentDB.execute.
Kevin Yip  @Reply  
     
2 years ago
If you need to delete only March data, then your criteria should be:

   DateExpected >= #2023-03-01# And DateExpected <= #2023-03-31#

In other words, you need to specify a range of dates (and use inclusive operators: >= and <=) instead of a single cut-off date.

Whether you use CurrentDb.Execute or DoCmd.RunSQL should not matter; the only difference is that the former runs silently and the latter doesn't.  So I think the problem lies somewhere else.
Daniel de Koning OP  @Reply  
     
2 years ago
Thanks for the quick reply. I was actually hoping to make the vba code easier by only referencing to 1 date field criteria. {{Note: I have a 2 date field criteria setup for running reports from my MainMenuF, but those fields are used in a standard Select Query (using Forms!MainMenuF!StartDate and ...!EndDate). Adding the 'Forms!* in vba didn't help either.}}
Hereby my original vba code, which I thought had setup wrong, but I don't think I have a Double Double Quote issue:
       CurrentDb.Execute "DELETE * FROM PurchaseDetailT WHERE DateExpected > #" & DeleteDate & "#"
DeleteDate is a field on the AdminMenuF with a DefaultValue of =Date()-1, so that I'm still able to change the field in the form when I want to delete more days (when testing for a longer period). Therefore, if I'm not mistaken, this should technically delete all lines as of yesterday morning and the future.
Will this (the 1 date criteria) not work and should I therefore always use a Start and End date in the vba SQL criteria?

Something else I was thinking about: The field DeleteDate is formatted as a date without time and the actual data in the PurchaseDetails is setup with a time portion.  
Don't know how to setup in VBA, but do I need to worry or think about the Format settings of the various date fields since they are not the same (the Data vs Criteria field)?
Kevin Yip  @Reply  
     
2 years ago
Your query has the correct syntax, and Date()-1 should yield the intended result.  I would look at the dates in your PurchaseDetailT table.  You said when the criteria selected dates after #2023-03-01#, the query somehow deleted dates before 2023-03-01 (you said it deleted 25-Feb-2023).  So something was obviously amiss there.  Was "25-Feb-2023" actually "25-Feb-2023" in your table?  I would check that first.  I don't have a definite idea of what's going on here, and can only help you eliminate possibilities one at a time.
Daniel de Koning OP  @Reply  
     
2 years ago
It seems there is something wrong with the date settings, but not sure how to pinpoint. First of all, my PC settings are set to Dutch format, which is "dd-mm-yyyy". The DateExpected field is setup as "ddd dd-mmm-yy". When I want to delete everything > 13/Mar, no problem. When I lower the date to 6/Mar nothing gets deleted that thas has a later date, but when I update it to 2/Mar or 1/Mar, than it appears the date value is considered to be the month value, so also Feb data is deleted with 2/Mar and with 1/Mar also Jan data is deleted. Latest setup in vba code:
     CurrentDb.Execute "DELETE * FROM PurchaseDetailT WHERE DateExpected> #" & CDate(Format(DeleteDate, "dd-mmm-yy")) & "#"
Any further tips in troubleshooting this issue? How can I get
Daniel de Koning OP  @Reply  
     
2 years ago
I see I forgot to finish my sentence &#128527;
I've got a status box on that Form, how can I use that to somehow see what the code is doing? The SQL convert video uses a string (Dim S), but I don't know how to use that here...
Kevin Robertson  @Reply  
          
2 years ago
Try this:
CurrentDb.Execute "DELETE * FROM PurchaseDetailT WHERE DateExpected> #" & Format(DeleteDate, "dd-mm-yyyy") & "#"

Make a copy of the table BEFORE running the Delete statement.
Kevin Yip  @Reply  
     
2 years ago
Are "2/Mar", "1/Mar", etc., how you literally type in dates on your form?  

You may do the following to see if Access interprets them correctly.  Go to the VBA editor, go down to the immediate window, and type (including the question mark in front):

     ?Format(CDate("1/Mar"), "mmmm dd yyyy")

Press Enter.  If you see March 01 2023, that means Access is able to interpret "1/Mar" correctly.

Try the above again, but with "Mar/1", and you should also see March 01 2023.  Even though your region uses day-month format instead of month-day, you unambiguously specify the month as "Mar", so Access is still able to interpret "Mar/1" as 01-Mar-2023 in your region.  In other words, there is no way Access would interpret "2/Mar" as a "February" date.  So my guess is something else is wrong.

If you get the correct results above, then the next thing is to look at are the actual date values stored in the DateExpected field.
Daniel de Koning OP  @Reply  
     
2 years ago
@Kevin R: I tried it and all possible variations (triple mmm, 2 yy), but still the same issue. I did receive an error with "mmm" in the code. I will add screenshots and all my settings, hopefully you can see the glitch or something else that i missed to mention. Although menu is in Dutch, it's still in the same place and should be familiar to you.

@Kevin Y: I my previous posts I've written the month in 3 letters in regular text to avoid confusion, but in vba it's actually in the format I mentioned before (example: "dd-mm-yyyy"). But yes, it appears it's definitely working (using a different abbreviation in Dutch of course, so the month name spelled below is correct ;-) ).
   ?format(CDate("1/mrt"), "mmmm dd yyyy")
   maart 01 2023
Daniel de Koning OP  @Reply  
     
2 years ago

Daniel de Koning OP  @Reply  
     
2 years ago

Daniel de Koning OP  @Reply  
     
2 years ago
In the screenshot you'll see the latest setup of the format and Default Values of the applicable fields after many trial-and-error attempts (+70 lines created in the last couple of days and still no luck in understanding what's going on). In the 1st screenshot you can see error 3075 'syntaxiserror in date'. The pound sign (#) is only mentioned ones, so maybe this is giving a clue of the issue?
The table is still under construction, so no Table backup made (except the daily Save As Back-up Database).
(translations: InkoopDetailT = PurchaseDetailT and DatumVerwacht = DateExpected)
Kevin Yip  @Reply  
     
2 years ago
You may have to use American date format in SQL.  To confirm this, you can do a little test.  In the query designer, create any test query and type in >#14-mrt-2023# in the criteria box (see picture below).  Your date should be accepted by the criteria box.  But when you switch to SQL view, the date may be turned into the American date format #3/14/2023# in the SQL.  This may be the reason for all this trouble: Access SQL statements can only use dates in the US format.  And that may be why you got a syntax error for that.  If that is the case, then all your SQL statements would have to use American dates.  Let me know how this goes.  I've never used international versions of Access so I don't know about this myself.
Kevin Yip  @Reply  
     
2 years ago

Daniel de Koning OP  @Reply  
     
2 years ago
Hi Kevin, yes you are correct, many thanks for the tip. See below the actions taken and a final question when transitioning to ISO Date format.

With my settings in Dutch format, when I enter the criteria as circled in your screenshot, it immediately changes from >#14-mrt-23# into >#14-03-2023#. And when I switch to the SQL View I see the following in the WHERE clause:
   WHERE (((InkoopDetailT.DatumVerwacht)>#3/14/2023#));

I therefore updated my vba code accordingly and it seems everything is working as it should, so I've also added a comment behind that code that it should be stated in US format, so I know it's setup correctly (for now):
   CurrentDb.Execute "DELETE * FROM InkoopDetailT WHERE DatumVerwacht > #" & Format(DeleteDate, "mm-dd-yyyy") & "#"

ISO Date Format:
My Excel date formats were always setup as dd-mmm-yyyy to avoid confusion when sharing files and (more importantly) when taking screenshots (to my USA colleagues). When I finally decide to also transition to ISO Date Format (personal and work), I will probably have to adjust this code again and maybe even don't have to use the format function at all?

And again: many thanks for the help!!!
Kevin Yip  @Reply  
     
2 years ago
Thank you for confirming, because I wasn't entirely sure myself, as I've never used any international versions of Office.  This is definitely one of the lesser-known gotchas of Access.  Maybe Richard could do a Tech Help video on this, since there seem to be quite a few international posters here who might benefit from this.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Delete Sample Data.
 

 
 
 

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 2025 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/23/2025 2:52:18 PM. PLT: 1s