MSAccess Query with VBA on Excel Spreadsheets

lucausa75

New Member
Joined
Oct 15, 2021
Messages
22
Office Version
  1. 2013
Platform
  1. Windows
Dear all,

by this function I move the content from an excel spreadsheet to an ADODB recordset:

VBA Code:
Public Function RecordSetFromSheet(SheetName As String)
    With Cnx
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .ConnectionString = "Data Source=" & ThisWorkbook.Path & "\" & ThisWorkbook.Name & ";" & _
        "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
        .Open
    End With
    Set Cmd.ActiveConnection = Cnx
    Cmd.CommandType = adCmdText
    Cmd.CommandText = "SELECT * FROM [" & SheetName & "$] WHERE [COUNTRY] LIKE '*ITALY*'"
    Rst.CursorLocation = adUseClient
    Rst.CursorType = adOpenDynamic
    Rst.LockType = adLockOptimistic
    Rst.Open Cmd
    Set Rst.ActiveConnection = Nothing
    If CBool(Cmd.State And adStateOpen) = True Then
        Set Cmd = Nothing
    End If
    If CBool(Cnx.State And adStateOpen) = True Then
        Cnx.Close
    End If
    Set Cnx = Nothing
    Set RecordSetFromSheet = Rst
End Function

The issue seems to be related to the below line which does not support the LIKE operator and gives zero record:
VBA Code:
Cmd.CommandText = "SELECT * FROM [" & SheetName & "$] WHERE [COUNTRY] LIKE '*ITALY*'"

If I use the equal operator I get the correct result:
VBA Code:
Cmd.CommandText = "SELECT * FROM [" & SheetName & "$] WHERE [COUNTRY] = 'ITALY'"

Please, any way to solve the issue?
I need to use the LIKE and NOT LIKE operator

Thanks
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi lucausa75,

As your function uses early binding via a reference to ActiveX Data Objects (ADO) you must change the wild card character from * to %. See here for more info.

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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