ADO WHERE question

BarryL

Well-known Member
Joined
Jan 20, 2014
Messages
1,436
Office Version
  1. 2019
Platform
  1. Windows
  2. MacOS
Hi All,

Trying to learn about ADO to fix my big file issues seeing as work wont let me use an SQL DB , R or Python. Can anyone tell me whats wrong with the below WHERE condition? I'm debugging at mrs.Open with an error of "Parameters expected 1"

Code:
Conn.Open sconnect
sSQLSting = "SELECT * From [Sheet1$A1:Z2000] WHERE MASTER_PORTFOLIO_CODE = " & "XXXXX"
Debug.Print sSQLSting
mrs.Open sSQLSting, Conn

cheers
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Text values need to be enclosed in apostrophes like this:

Code:
sSQLSting = "SELECT * From [Sheet1$A1:Z2000] WHERE MASTER_PORTFOLIO_CODE = 'XXXXX'"
 
Upvote 0
Yep, thats it. Cheers Rory.

One more question if I may, If I was working with a sheet that didnt have a table inserted how would I query that? as in trying

"SELECT * From [Sheet1$A1:Z2000] WHERE [Sheet1$A1:A2000] = 'XXXX'"

doesnt work.
 
Upvote 0
Having a table or not is actually irrelevant - ADO has no concept of them. As long as the column header is the same, and you specified that there are column headers in your connection string, the code will work as it was before.
 
Upvote 0
below is what im working with but it doesnt seem to work with specifying the columns

Code:
Sub sbADOExample()
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
DBPath = "zzzz.xls"
sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=NO';"
Conn.Open sconnect
sSQLSting = "SELECT * From [Sheet1$A1:Z20000] WHERE [Sheet1$A1:A20000] = 'XXXX'"
Debug.Print sSQLSting
mrs.Open sSQLSting, Conn
Sheet1.Range("A2").CopyFromRecordset mrs
mrs.Close
Conn.Close
End Sub
 
Last edited:
Upvote 0
Since you've specified that you don't have headers (which is a little odd in a worksheet), you have to use F1, F2 etc as the column names, so it would be

WHERE [F2] = 'XXXX'

for the criterion part.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,180
Members
452,615
Latest member
bogeys2birdies

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