1

I'm trying to get some data with sql-filter WHERE inside Excel Macros. But I get some errors with data-types:

Dim cn As Object, rs As Object, output As String, sql As String

Set cn = CreateObject("ADODB.Connection")
With cn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=" & ThisWorkbook.Path & "\" & ThisWorkbook.Name & ";" & "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
    .Open
End With

Dim Value As Date
Value = .Range("B4").Value ' 30.10.2020

sql = "SELECT * FROM [Page 1$] WHERE DateTime = #" & Format(Value, "dd\.mm\.yyyy") & "#"
Set rs = cn.Execute(sql) 'here I get error

DateTime cells have format "Date". I get Run-time error Syntax error in date in query expression 'DateTime = #30.10.2020'.

Original file is here.

What am I doing wrong with format?

2
  • have you tried Format(Value, "dd.mm.yyyy")? Commented Oct 30, 2020 at 14:06
  • Yes, the same error Commented Oct 30, 2020 at 14:07

1 Answer 1

1

Consider parameterization using the ADO Command object and specify your input parameter as adDate type. Also, usually, proper dates are separated by hyphens and not periods between date parts (day, month, and year). But your system region/locale can be different assuming the cell is not a Custom format.

Dim cn As Object, cmd As Object, rs As Object
Dim output As String, sql As String
Dim dt As Date
Const adCmdText As Integer = 1, adDate As Integer = 7, adParamInput As Integer = 1

'dt = .Range("B4").Value                              ' IF REGION USES PERIODS IN DATES
dt = CDate(Replace(.Range("B4").Value, ".", "-"))     ' IF REGION USES HYPHENS IN DATES

Set cn = CreateObject("ADODB.Connection")
With cn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=" _
                        & ThisWorkbook.FullName & ";" _
                        & "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
    .Open
End With

' PREPARED STATEMENT WITH QMARK ? PLACEHOLDER
sql = "SELECT * FROM [Page 1$] WHERE DateTime = ?"

Set cmd = CreateObject("ADODB.Command")

With cmd
  .ActiveConnection = cn
  .CommandText = sql
  .CommandType = adCmdText

  ' BIND PARAM AND DEFINE TYPE AND LENGTH
  .Parameters.Append .CreateParameter("prm", adDate, adParamInput, , dt)

  ' CREATE RECORDSET
  Set rs = .Execute
End With
Sign up to request clarification or add additional context in comments.

3 Comments

for your code I get "Compile error: Expected: If or Select or Sub or Function ir Property or Type ir With or Enum or end of statement" on End cmd
I changed End cmd on End With. Now I have error with .CommandType = adCmdText: "Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another"
Sorry for those syntax errors. Yes, it should be End With and because your are running late binding (not early binding) of ADO objects, the constants adCmdText, adDate, adParamInput are not known. So, you need to manually define their values. See Constant declaration at top.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.