Corect use of SQL

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,099
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hi,

I want to query some data in my worksheet using SQL. This may not be a good idea as I recall some problem with memory leaks or similar, but regardless I can't get it working and would like to

I'm using ADODB and I have a working connection (to the current workbook) and can get results using "Select * From...". But I don't want "*" instead I want to specify a column (or columns)

A few things found online didn't work here e.g. "Select [Sheetname$.Col1]... or 'select [F1], [F2] etc.
Please, what is the correct working syntax after SELECT ?

Perhaps part of the problem is the extended header? I'm using ='Excel 8.0;HDR=NO'. There were conflicting answers found for HDR, some said use NO even if there are headers. And to use "IMEX=1" but that just seemed to make the connection take longer.

Any advice most welcome, thanks !
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
He says'All the column in the worksheet Sheet1 have fieldnames in the first row
'which we all use here.
stSQL = "SELECT DISTINCT Data FROM [Sheet1$]"
Suggesting Data is one of his column names.
But With or Without DISTINCT (and HDR= Yes or No) still doesn't work here.Also tried JET OLEDB4/Ace12 and Excel 8.0/12.0 in the Connection string.
It only works if I use "Select *". Without a way to address the columns you can't set any WHERE criteria. Frustrating !
 
Upvote 0
You really haven't given enough information. What code are you using to connect to it? What format is your workbook? Does it have headers? If so, are they in row 1? And also, what exactly do you mean when you say the code you have doesn't work - what happens?
 
Upvote 0
Sorry Rory, I was focussing on just the composition of the SQL as the problem, but you're quite right, it may be something else.
Here's the whole thing (having tried slight changes as per Msg 3).
Code:
Sub RsTest()
    Dim myConnection As New ADODB.Connection
    Dim myRecordset As ADODB.Recordset
    With myConnection
        .Open "Provider=Microsoft.Ace.OLEDB.12.0;" & _
        "Data Source=Y:\Test3.xlsm;" & _
        "Extended Properties='Excel 12.0;HDR=YES';"
            Set myRecordset = New ADODB.Recordset
            myRecordset.Open "Select * from [EP65$]", myConnection, adOpenStatic
                Do Until myRecordset.EOF
                    Debug.Print myRecordset(0) ', myRecordset(1)
                    myRecordset.MoveNext
                Loop
            myRecordset.Close
            Set myRecordset = Nothing
        .Close
    End With
End Sub

And the problem is Want "Select MyColumn2, MyColum3 From.. rather that everything.
 
Last edited:
Upvote 0
What are the headers in row 1 for the columns you want?
 
Upvote 0
I've just set up two for testing and called them 'ColA' and 'ColB'
I don't necessarily need headers, but can do if it won't work without them.
 
Upvote 0
SQL wants to know headers without spaces, when querying you can use '' for a blank column, given that

SELECT
ColA
,ColB
FROM
Data
WHERE

should return the columns
 
Upvote 0
Yes Mole. thank you !! I could have sworn I'd tried every possibility. It all makes sense, with HDR=NO fields are F1, F2 etc
and HDR=YES it's the column names on Row 1. As expected... but I couldn't make it work until now! Weird...

Anyway do you know what the story is on "memory leaks" when doing this (to the current workbook)? I read using a closed workbook is recccomended (presumably a copy)?

Can the memory leaks be plugged or otherwise handled?
 
Upvote 0
I have no knowledge on memory leaks, guessing that the connection wasn't being closed in some methods
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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