djburnheim
Technical User
I need to convert a date from dd/mm/yy to yyyy-mm-dd HH:MM:SS so I can insert it into a sql query. I've worked a pretty rude way of doing it using comboboxes for day, month & year and then creating a string in the format I want but I would prefer to use a calendar control for the user to enter the date. The code I'm using at the moment is below.
Dim Day As Variant
Dim Month As Variant
Dim Year As Variant
Day = Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"
Month = Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"
Year = Array("2001", "2002", "2003", "2004", "2005"
cboDay.List = Day
cboMonth.List = Month
cboYear.List = Year
Dim Query As String
Dim StartDate As String
Dim EndDate As String
StartDate = cboYear.Value & "-" & cboMonth.Value & "-" & cboDay.Value - 1 & Chr(32) & "00:00:00"
EndDate = cboYear.Value & "-" & cboMonth.Value & "-" & cboDay.Value + 5 & Chr(32) & "00:00:00"
DisplayDate = cboDay.Value & "/" & cboMonth.Value & "/" & cboYear.Value
Worksheets("Sheet1".Range("B1".Value = DisplayDate
Query = "SELECT Count(*) AS 'Cases Created' FROM Vantive.dbo.SW_HD_CASE SW_HD_CASE WHERE (SW_HD_CASE.swDateCreated Between {ts '" & StartDate & "'} And {ts '" & EndDate & "'}) AND (SW_HD_CASE.swCreatedBy='" & Cells(2, 1) & "')"
With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=Database;UID=user;PWD=password;APP=Microsoft® Query;WSID=QBRIK5;DATABASE=Master" _
, Destination:=Range("A4")
.CommandText = Array(Query)
.Name = "Name"
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
As an a side...I'm sure there has to be an easier way to populate arrays with days of the week, months etc etc.
Any help would be greatly appreciated.
Dave
Dim Day As Variant
Dim Month As Variant
Dim Year As Variant
Day = Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"
Month = Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"
Year = Array("2001", "2002", "2003", "2004", "2005"
cboDay.List = Day
cboMonth.List = Month
cboYear.List = Year
Dim Query As String
Dim StartDate As String
Dim EndDate As String
StartDate = cboYear.Value & "-" & cboMonth.Value & "-" & cboDay.Value - 1 & Chr(32) & "00:00:00"
EndDate = cboYear.Value & "-" & cboMonth.Value & "-" & cboDay.Value + 5 & Chr(32) & "00:00:00"
DisplayDate = cboDay.Value & "/" & cboMonth.Value & "/" & cboYear.Value
Worksheets("Sheet1".Range("B1".Value = DisplayDate
Query = "SELECT Count(*) AS 'Cases Created' FROM Vantive.dbo.SW_HD_CASE SW_HD_CASE WHERE (SW_HD_CASE.swDateCreated Between {ts '" & StartDate & "'} And {ts '" & EndDate & "'}) AND (SW_HD_CASE.swCreatedBy='" & Cells(2, 1) & "')"
With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=Database;UID=user;PWD=password;APP=Microsoft® Query;WSID=QBRIK5;DATABASE=Master" _
, Destination:=Range("A4")
.CommandText = Array(Query)
.Name = "Name"
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
As an a side...I'm sure there has to be an easier way to populate arrays with days of the week, months etc etc.
Any help would be greatly appreciated.
Dave