djburnheim
Technical User
I have a spreadsheet that gets data from a SQL database. Each week I need to change the dates in the querys to update the spreadsheet. I'm trying to write a macro that will take the dates from a form and update the querys automatically. Below is what I have so far...
Dim StartDate As Date
Dim EndDate As Date
StartDate = dtpWeekStart.Value - 1
EndDate = dtpWeekStart.Value + 5
Range("N4"
.Select
With Selection.QueryTable
.Connection = _
"ODBC;DSN=SQLServer;UID=User;PWD=password;APP=Microsoft® Query;WSID=Workstation;DATABASE=Master"
.CommandText = Array( _
"SELECT Count(*) AS 'Cases Created'" & Chr(13) & "" & Chr(10) & "FROM Master.dbo.SW_HD_CASE SW_HD_CASE" & Chr(13) & "" & Chr(10) & "WHERE (SW_HD_CASE.swDateCreated Between {ts '2003-01-12 00:00:00'} And {ts '2003-01-18 00:00:00'}) AND (SW_HD_CASE.swCreated" _
, "By='john.doh')"![Wink ;) ;)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
.Refresh BackgroundQuery:=False
End With
I need to be able to insert my "StartDate" and "EndDate" into the CommandText array and then this needs to repeat for another 17 people.
Dim StartDate As Date
Dim EndDate As Date
StartDate = dtpWeekStart.Value - 1
EndDate = dtpWeekStart.Value + 5
Range("N4"
With Selection.QueryTable
.Connection = _
"ODBC;DSN=SQLServer;UID=User;PWD=password;APP=Microsoft® Query;WSID=Workstation;DATABASE=Master"
.CommandText = Array( _
"SELECT Count(*) AS 'Cases Created'" & Chr(13) & "" & Chr(10) & "FROM Master.dbo.SW_HD_CASE SW_HD_CASE" & Chr(13) & "" & Chr(10) & "WHERE (SW_HD_CASE.swDateCreated Between {ts '2003-01-12 00:00:00'} And {ts '2003-01-18 00:00:00'}) AND (SW_HD_CASE.swCreated" _
, "By='john.doh')"
.Refresh BackgroundQuery:=False
End With
I need to be able to insert my "StartDate" and "EndDate" into the CommandText array and then this needs to repeat for another 17 people.