G
Guest
Hi all,
I'm trying to connect to an access database to return a recordset. the only way i can query the database is using sql statements but i can't seem to pass dates in the sql string. I was wondering if it was possible with parameters.
My code is as follows:
Dim rec As Recordset
Dim cnn As New ADODB.Connection
Dim cmd As New ADODB.Command
cnn.CursorLocation = adUseClient
cnn.Open sConnection
cmd.ActiveConnection = cnn
cmd.CommandText = sSQL
sSQL = "select Format(Date_Time, 'dd-mmm-yyyy hh:mm') as When, Format(" & sUnit & "," & sFormat
sSQL = sSQL & ") as Reading from " & Range("DataTable")
-- the dates are on this line as sDate1 and 2, they are stored as strings at the moment..
sSQL = sSQL & " where (Date_Time between '" & sDate1 & "' and '" & sDate2 & "')"
sSQL = sSQL & " and (Format(" & sUnit & "," & sFormat & ") < " & "'" & Range("SpecifiedMin") & "'"
sSQL = sSQL & " or Format(" & sUnit & "," & sFormat & ") > " & "'" & Range("SpecifiedMax") & "'"
sSQL = sSQL & ") order by Date_Time"
Set rec = cmd.Execute
Thanks for any help,
Regards.
Steven
I'm trying to connect to an access database to return a recordset. the only way i can query the database is using sql statements but i can't seem to pass dates in the sql string. I was wondering if it was possible with parameters.
My code is as follows:
Dim rec As Recordset
Dim cnn As New ADODB.Connection
Dim cmd As New ADODB.Command
cnn.CursorLocation = adUseClient
cnn.Open sConnection
cmd.ActiveConnection = cnn
cmd.CommandText = sSQL
sSQL = "select Format(Date_Time, 'dd-mmm-yyyy hh:mm') as When, Format(" & sUnit & "," & sFormat
sSQL = sSQL & ") as Reading from " & Range("DataTable")
-- the dates are on this line as sDate1 and 2, they are stored as strings at the moment..
sSQL = sSQL & " where (Date_Time between '" & sDate1 & "' and '" & sDate2 & "')"
sSQL = sSQL & " and (Format(" & sUnit & "," & sFormat & ") < " & "'" & Range("SpecifiedMin") & "'"
sSQL = sSQL & " or Format(" & sUnit & "," & sFormat & ") > " & "'" & Range("SpecifiedMax") & "'"
sSQL = sSQL & ") order by Date_Time"
Set rec = cmd.Execute
Thanks for any help,
Regards.
Steven