ADO WHERE question

BarryL

Well-known Member
Joined
Jan 20, 2014
Messages
1,421
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:

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,067
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Text values need to be enclosed in apostrophes like this:

Code:
sSQLSting = "SELECT * From [Sheet1$A1:Z2000] WHERE MASTER_PORTFOLIO_CODE = 'XXXXX'"
 

BarryL

Well-known Member
Joined
Jan 20, 2014
Messages
1,421
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,067
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

BarryL

Well-known Member
Joined
Jan 20, 2014
Messages
1,421
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:

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,067
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,561
Messages
5,596,853
Members
414,107
Latest member
Tigretto

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
Top