Hi all,
I have a problem to get the data into Excel Sheet from Acess database when using Date as a variable. Do not know if I have correct SQL syntax when Date is variable. When I tried '" & dt & "', I got data type mismatch error. For #" & dt & "# which is in the code below, I got syntax error instead.
Thanks for support.
Sub DowloadDate()
Dim cn As Object
Dim i As Integer
Dim rs As Object
Dim strSql As String
Dim strConnection As String
Dim dt As Date
dt = Range("G2").Value
Set cn = CreateObject("ADODB.Connection")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=\Database.accdb"
strSql = "SELECT * FROM Quality WHERE Date = #" & dt & "# ;"
cn.Open strConnection
Set rs = cn.Execute(strSql)
'Copy table headers
For i = 0 To rs.Fields.Count - 1
Sheets("Summary").Cells(1, i + 1) = rs.Fields(i).Name
Next i
'copy database values
Range("A2").CopyFromRecordset rs
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
I have a problem to get the data into Excel Sheet from Acess database when using Date as a variable. Do not know if I have correct SQL syntax when Date is variable. When I tried '" & dt & "', I got data type mismatch error. For #" & dt & "# which is in the code below, I got syntax error instead.
Thanks for support.
Sub DowloadDate()
Dim cn As Object
Dim i As Integer
Dim rs As Object
Dim strSql As String
Dim strConnection As String
Dim dt As Date
dt = Range("G2").Value
Set cn = CreateObject("ADODB.Connection")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=\Database.accdb"
strSql = "SELECT * FROM Quality WHERE Date = #" & dt & "# ;"
cn.Open strConnection
Set rs = cn.Execute(strSql)
'Copy table headers
For i = 0 To rs.Fields.Count - 1
Sheets("Summary").Cells(1, i + 1) = rs.Fields(i).Name
Next i
'copy database values
Range("A2").CopyFromRecordset rs
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub