ADO where function syntax

Paul Sansom

Board Regular
Joined
Jan 28, 2013
Messages
178
Office Version
  1. 2021
  2. 2016
Platform
  1. Windows
I have an ADO connection to and table and i cannot see what is wrong with the reference to a sheet cell
If i change br for a simple text e.g. 'h' the filter works correctly.
What is the correct syntax for a refence to a cell on a sheet (Output). FYI Output is not the sheet where the results are shown
Many Thanks Paul

VBA Code:
Private Sub byBranchRef()
'   Filter by branch ref
    Dim myADO As New clsADO
    myADO.Connect ThisWorkbook.FullName
    
    Dim br As String
    br = Output.Range("d5")
    
    myADO.RunQuery "Select Photonumber,BranchRef,Comments From [Photos$]" _
        & " Where BranchRef = br ", 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.
If br is a variable, it needs to be outside of the double-quotes, or else it is treated as literal text.

Also, If the BranchRef field is indeed a string, you may need text qualifiers around it.
If single quotes are valid text qualifiers (Iike 'h' seems to suggest), then try:
VBA Code:
    myADO.RunQuery "Select Photonumber,BranchRef,Comments From [Photos$]" _
        & " Where BranchRef = '" & br & "'", shResults
 
Upvote 0
Solution
If br is a variable, it needs to be outside of the double-quotes, or else it is treated as literal text.

Also, If the BranchRef field is indeed a string, you may need text qualifiers around it.
If single quotes are valid text qualifiers (Iike 'h' seems to suggest), then try:
VBA Code:
    myADO.RunQuery "Select Photonumber,BranchRef,Comments From [Photos$]" _
        & " Where BranchRef = '" & br & "'", shResults
Many thanks, sorry for slow response , works well :)
 
Upvote 0
You are welcome.

Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,216,212
Messages
6,129,533
Members
449,515
Latest member
lukaderanged

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