0

I am trying to insert records to a table from another using sql in access through vba. everything works but I want a field that contains a date to be greater than the first of the year that the user inputs. When I added that command into my statement no records are inserted and I'm pretty sure it is because I am not comparing the date right. I am rather new to sql so any help on this issue and how to fix it would be greatly appreciated! I have posted the statement below.

verifHPMS = "INSERT INTO HPMSVerified([CLIENT ID], [CLIENT NAME], [MARKET SEGMENT], [FORMULARY ID], [FORMULARY NAME], [FORMULARY VERSION],[HPMS EXPORTED],[HPMS EXPORT DATE],[APPROVED DATE]) " & _
            "SELECT DD.[CLIENT ID],DD.[CLIENT NAME],DD.[MARKET SEGMENT],DD.[FORMULARY ID],DD.[FORMULARY NAME],DD.[FORMULARY VERSION],DD.[HPMS EXPORTED],DD.[HPMS EXPORT DATE],DD.[APPROVED DATE] " & _
            "FROM " & tableName & " as DD " & _
            "Where DD.[HPMS EXPORTED] IN (""Yes"") " & _
            "AND DD.[HPMS EXPORT DATE] >= [APPROVED DATE] " & _
            "AND DD.[CLIENT ID] NOT IN(SELECT Exclusions.[Client ID] FROM Exclusions)" & _
            "AND DD.[APPROVED DATE]>01/01/" & year
2
  • Are the fields with the date value also of type date? Otherwise it should work Commented Dec 4, 2014 at 21:19
  • Yes they are of type date/time. Commented Dec 4, 2014 at 21:21

2 Answers 2

1

Try replacing the final line with this:

"AND YEAR(DD.[APPROVED DATE]) >= " & year & " AND MONTH(DD.[APPROVED DATE]) > 1 AND DAY(DD.[APPROVED DATE]) > 1"

This compares each component of the date individually, to avoid issues with date literals and your manual method of string interpolation.

Sign up to request clarification or add additional context in comments.

Comments

0

Replace the final line with:

"AND DD.[APPROVED DATE] > #01/01/" & year & "#"

Note that VBA and "Jet" SQL always uses #m/d/y# format for date literals, regardless of regional settings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.