Comparing dates in VBA / SQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobinDiederen
    New Member
    • Sep 2006
    • 19

    Comparing dates in VBA / SQL

    Hi,

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

    Code:
    SQL1 = "INSERT INTO Contractrealisatie (Medewerker,Uren) SELECT Planning.Werknemer, (15*(Planning.[Stoptijd] - Planning.[Aanvangstijd])) FROM Planning WHERE ([Datum] = " & CorrectedDate & ")"
    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!
  • Jim Doherty
    Recognized Expert Contributor
    • Aug 2007
    • 897

    #2
    Originally posted by RobinDiederen
    Hi,

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

    Code:
    SQL1 = "INSERT INTO Contractrealisatie (Medewerker,Uren) SELECT Planning.Werknemer, (15*(Planning.[Stoptijd] - Planning.[Aanvangstijd])) FROM Planning WHERE ([Datum] = " & CorrectedDate & ")"
    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(Correcte dDate, "mm/dd/yyyy")


    Jim

    Comment

    • Scott Price
      Recognized Expert Top Contributor
      • Jul 2007
      • 1384

      #3
      Also, whenever comparing date literals, they need to be wrapped in #'s...

      Regards,
      Scott

      Comment

      • RobinDiederen
        New Member
        • Sep 2006
        • 19

        #4
        Originally posted by Jim Doherty
        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(Correcte dDate, "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!

        Comment

        • Jim Doherty
          Recognized Expert Contributor
          • Aug 2007
          • 897

          #5
          Originally posted by RobinDiederen
          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.Werkne mer, (15*(Planning.[Stoptijd] - Planning.[Aanvangstijd])) FROM Planning WHERE ([Datum] = #" & CorrectedDate & "#)"

          Comment

          • Jim Doherty
            Recognized Expert Contributor
            • Aug 2007
            • 897

            #6
            Originally posted by Jim Doherty
            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.Werkne mer, (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

            Comment

            • RobinDiederen
              New Member
              • Sep 2006
              • 19

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

              Cya!

              Comment

              Working...