I want to run an SQL query from excel to Access as shown below. When I run the code, I get an "Incompatible data type in criterion expression" error. Do you have a tip, please, to get me out of this?
Regards
Sub test()
Dim myDate As Date
Dim myCUID As String
Dim i As Long
Dim cnt As Object, rst As Object
Set rst = CreateObject("ADODB.Recordset")
Set cnt = CreateObject("ADODB.Connection")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & ThisWorkbook.Path & "\ATD_monitoring_basePro.accdb';"
'Open connection to the database
cnt.Open strConnection
'Begin transaction processing
cnt.BeginTrans
myDate = WorkSheets("Bla").Range("A1").Value
myCUID = "XXXX1234"
sqlCmd = "SELECT COUNT(*) FROM Table1 WHERE Date= '" & myDate & "' AND CUID='" & myCUID & "';"
rst.Open sqlCmd, cnt
'Close the ADO objects
cnt.Close
Set rst = Nothing
Set cnt = Nothing
On Error GoTo 0
End Sub
WHERE [Date]Also, what is myDate being returned as?