VBA/SQL Between Dates

User avatar
Joseph
3StarLounger
Posts: 206
Joined: 31 Dec 2010, 22:23
Location: Columbia Falls, MT

VBA/SQL Between Dates

Post by Joseph »

Hi. Within VBA I am trying to pass through an easy query, but I am having a hell of a time with the between dates...

Here is the bit that is giving the business

Code: Select all

AND [datecreated] " & " Between #" & startdate & "#" & " And #" & enddate & "#;"

Code: Select all

sqlStr = "SELECT * FROM LO_ENDOFSHIFTSURVEY WHERE location_id='" & locationid & "' AND [datecreated] " & " Between #" & startdate & "#" & " And #" & enddate & "#;"
Error message:
Incorrect syntax near '#'

[datecreated] field is set to datetime property.

Any help would be greatly appreciated, I have looked all over. Hoping this is something simple for you guys!

Thanks!
Joe

User avatar
HansV
Administrator
Posts: 78394
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA/SQL Between Dates

Post by HansV »

By "pass through", do you mean that the query is to be executed in SQL Server?
Best wishes,
Hans

User avatar
Joseph
3StarLounger
Posts: 206
Joined: 31 Dec 2010, 22:23
Location: Columbia Falls, MT

Re: VBA/SQL Between Dates

Post by Joseph »

No, sorry Hans, bad choice of words. I'm using VBA within excel to query SQL server.

User avatar
HansV
Administrator
Posts: 78394
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA/SQL Between Dates

Post by HansV »

Does this work?

Code: Select all

    sqlStr = "SELECT * FROM LO_ENDOFSHIFTSURVEY WHERE location_id='" & locationid & _
        "' AND datecreated Between '" & Format(startdate, "yyyy-mm-dd") & _
        "' And '" & Format(enddate, "yyyy-mm-dd") & "'"
Best wishes,
Hans

User avatar
Joseph
3StarLounger
Posts: 206
Joined: 31 Dec 2010, 22:23
Location: Columbia Falls, MT

Re: VBA/SQL Between Dates

Post by Joseph »

Yes. It. Does!

Thanks Hans!!!!!

User avatar
HansV
Administrator
Posts: 78394
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA/SQL Between Dates

Post by HansV »

SQL Server treats dates as strings; it's different from Access in this respect.
Best wishes,
Hans

User avatar
Joseph
3StarLounger
Posts: 206
Joined: 31 Dec 2010, 22:23
Location: Columbia Falls, MT

Re: VBA/SQL Between Dates

Post by Joseph »

Seeing the correct syntax is putting this together for me.

How would I copy the recordset WITH the field names as column headers? Currently using this bit but does not include.

ws2.Range("a2").CopyFromRecordset myRecSet

User avatar
HansV
Administrator
Posts: 78394
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA/SQL Between Dates

Post by HansV »

You have to do that separately, after opening the recordset:

Code: Select all

    Dim i As Long
    For i = 1 to myRecSet.Fields.Count
        ws2.Cells(1, i).Value = myRecSet.Fields(i - 1).Name
    Next i
Best wishes,
Hans

User avatar
Joseph
3StarLounger
Posts: 206
Joined: 31 Dec 2010, 22:23
Location: Columbia Falls, MT

Re: VBA/SQL Between Dates

Post by Joseph »

Perfect, thanks again Hans. I've just started my attempt to learn the methods of querying this way, so I may be back soon...as google fails me.