Vba sql query date type error

dalibord

New Member
Joined
Jan 8, 2015
Messages
7
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
 
strSql = "Select * from Quality where Date = cdate(format(" & dt & ", ""##.##.####""))"

OK, try this.

Or maybe even this?

strSql = "Select * from Quality where Date = cdate(format(" & dt & ", ""DD.MM.YYYY""))"

I am only guessing at this based on what i found in a google search though so it may or may not work.
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Also, humour me here and try changing Date to [Date]

You may not even need to format it, it might just be strSql = "Select * from Quality where [Date] = " & dt
 
Last edited:
Upvote 0
You shouldn't have a field named Date, Date is a VBA function that is used in Access.

If you do have a field named Date you should change it, but in the meantime you can try enclosing it in [ ].

You do need to have # around the date in Access, but I'm not sure 7.1.2015 is a valid date format.
 
Upvote 0
Both proposals with the same result - syntax error. I tried to change column name Date in Acess and get the result in Excell: data type mismatch criteria expression..
 
Upvote 0

Forum statistics

Threads
1,221,904
Messages
6,162,745
Members
451,785
Latest member
DanielCorn

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top