By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,285 Members | 1,619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,285 IT Pros & Developers. It's quick & easy.

Comparing dates in VBA / SQL

P: 19
Hi,

I'm trying to write some VBA script which uses date in an SQL statement:

Expand|Select|Wrap|Line Numbers
  1. SQL1 = "INSERT INTO Contractrealisatie (Medewerker,Uren) SELECT Planning.Werknemer, (15*(Planning.[Stoptijd] - Planning.[Aanvangstijd])) FROM Planning WHERE ([Datum] = " & CorrectedDate & ")"
  2.  
This however does not work; when the WHERE conditiion should be true, it always results in 0 appends.

What is wrong here?

CorrectDate is a date-typed variable.

Thanks!
Sep 8 '07 #1
Share this Question
Share on Google+
6 Replies


Jim Doherty
Expert 100+
P: 897
Hi,

I'm trying to write some VBA script which uses date in an SQL statement:

Expand|Select|Wrap|Line Numbers
  1. SQL1 = "INSERT INTO Contractrealisatie (Medewerker,Uren) SELECT Planning.Werknemer, (15*(Planning.[Stoptijd] - Planning.[Aanvangstijd])) FROM Planning WHERE ([Datum] = " & CorrectedDate & ")"
  2.  
This however does not work; when the WHERE conditiion should be true, it always results in 0 appends.

What is wrong here?

CorrectDate is a date-typed variable.

Thanks!
Look at the FORMAT function in how you should be dealing with your date formatting for your SELECTS and UPDATES compared with the internal storage of your data. You will find that you merely need to swap the month and day round when using code

Format(CorrectedDate, "mm/dd/yyyy")


Jim
Sep 8 '07 #2

Scott Price
Expert 100+
P: 1,384
Also, whenever comparing date literals, they need to be wrapped in #'s...

Regards,
Scott
Sep 8 '07 #3

P: 19
Look at the FORMAT function in how you should be dealing with your date formatting for your SELECTS and UPDATES compared with the internal storage of your data. You will find that you merely need to swap the month and day round when using code

Format(CorrectedDate, "mm/dd/yyyy")


Jim
Hi Jim,

Thanks for your help!
I fiddled around a bit with the format function, but, I cannot get it to work, whatever I try. I even tried to manually add a date (in different formats) in my SQL query, but I always get a empty result! When I replace the Datum field by another column, it all works well.

Any guesses?

Thanks agian!
Sep 9 '07 #4

Jim Doherty
Expert 100+
P: 897
Hi Jim,

Thanks for your help!
I fiddled around a bit with the format function, but, I cannot get it to work, whatever I try. I even tried to manually add a date (in different formats) in my SQL query, but I always get a empty result! When I replace the Datum field by another column, it all works well.

Any guesses?

Thanks agian!

Without analysing your table structure, data, and actually seeing what it is you are doing then my guess being blind here here is not much help unfortunately.
If you run the first bit of your query as a 'Select' I take it you do have records available in order to do the insert. Try running the select on its own to make sure the 'SELECT' is reading correctly. Failing that the only other option would be to post your table structure. Exact query syntax and a few records of sample data you have there and I could see if I can replicate it this end?

SELECT Planning.Werknemer, (15*(Planning.[Stoptijd] - Planning.[Aanvangstijd])) FROM Planning WHERE ([Datum] = #" & CorrectedDate & "#)"
Sep 9 '07 #5

Jim Doherty
Expert 100+
P: 897
Without analysing your table structure, data, and actually seeing what it is you are doing then my guess being blind here here is not much help unfortunately.
If you run the first bit of your query as a 'Select' I take it you do have records available in order to do the insert. Try running the select on its own to make sure the 'SELECT' is reading correctly. Failing that the only other option would be to post your table structure. Exact query syntax and a few records of sample data you have there and I could see if I can replicate it this end?

SELECT Planning.Werknemer, (15*(Planning.[Stoptijd] - Planning.[Aanvangstijd])) FROM Planning WHERE ([Datum] = #" & CorrectedDate & "#)"

As a PS I am on vacation as of today (off to sunny cyprus) so if you don't get a resolution you can PM me and I'll continue on return next week?

Jim
Sep 11 '07 #6

P: 19
Thanks guys!!! I solved it by adding the #'s! How stupid of me!

Cya!
Sep 14 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.