Filter by containing string not = string, ADO

Paul Sansom

Board Regular
Joined
Jan 28, 2013
Messages
178
Office Version
  1. 2021
  2. 2016
Platform
  1. Windows
Hi I have a sub that is my query filter. It current filters bra on "=" to cell input. I cannot work out the syntax for a "containing" filter e.g contains bra.
I have tried a simple subtituion of = for contains but it does not filter correctly......
'myADO.RunQuery "Select [Photo Number], [Branch Ref], Comments, Place, [Viewing Order], Date, [Source Other Names], [Source Last Name] From [Photos$]" _
& " Where [Branch Ref] contains '" & bra & "'", shResults

Any guidance always gratefully accepted.. Paul

Code shown works well as = string!!!!
VBA Code:
Sub byBranchRef()
'   Filter by branch ref
    Dim myADO As New clsADO
    myADO.Connect ThisWorkbook.FullName
    
    bra = Output.Range("d5")
    
    myADO.RunQuery "Select [Photo Number], [Branch Ref], Comments, Place, [Viewing Order], Date, [Source Other Names], [Source Last Name] From [Photos$]" _
        & [B]" Where [Branch Ref] = '" & bra & "'"[/B], shResults
    
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try this:
VBA Code:
Where [Branch Ref] Like "'*" & bra & "*'"
 
Upvote 0
Try this:
VBA Code:
Where [Branch Ref] Like "'*" & bra & "*'"
No "Argument not optional", and I tried every combination of * inside or outside the quotes without success. The Like is accepted but is the same as = if no wildcard *
 
Last edited:
Upvote 0
OK, I was hoping that ADO syntax would be similar to SQL syntax, but it appears that might not always be the case.
I found this article here, and it seems to imply that ADO uses "%" as wildcards: Wildcard for SQL LIKE Using ADO

So maybe try:
VBA Code:
Where [Branch Ref] Like "'%" & bra & "%'"
 
Upvote 0

Forum statistics

Threads
1,215,257
Messages
6,123,916
Members
449,133
Latest member
rduffieldc

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