Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Sql query on a range in the opened worksheet

I would like to know if it is possible to run Sql query on a range in the
opened worksheet and if it is, what is the syntax.
A code sample will be appreciated.

TIA

Shaul Bel


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 599
Default Sql query on a range in the opened worksheet

Shaul

You can use ADO to do that. Set a reference to the ActiveX Data Objects
Library. Here's some example code

Sub GetXLRecords()

Dim Cn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim Rng As Range
Dim Constr As String
Dim Sqlstr As String

Constr = "DSN=Excel Files;DBQ=C:\Dick\Tester\Exceldb.xls;" _
& "DefaultDir=C:\Dick\Tester;DriverId=22;MaxBufferSi ze=2048" _
& ";PageTimeout=5;"

Sqlstr = "SELECT MyTable.Name, MyTable.Number1, MyTable.Number2 " _
& "FROM `C:\Dick\Tester\Exceldb`.MyTable MyTable"

Set Cn = New ADODB.Connection

Cn.Open Constr

Set Rs = Cn.Execute(Sqlstr)

ActiveSheet.Cells(1, 1).CopyFromRecordset Rs

Rs.Close
Cn.Close

Set Rs = Nothing
Set Cn = Nothing

End Sub

--
Dick Kusleika
MVP - Excel
www.dicks-clicks.com
Post all replies to the newsgroup.

"Shaul Bel" wrote in message
...
I would like to know if it is possible to run Sql query on a range in the
opened worksheet and if it is, what is the syntax.
A code sample will be appreciated.

TIA

Shaul Bel




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Range Changing Each Time Worksheet Is Opened Lance Hebert Excel Discussion (Misc queries) 2 January 23rd 09 01:31 PM
Go to cell with maximum value when worksheet opened pignick Excel Discussion (Misc queries) 6 July 17th 08 06:58 PM
QUERY data range to populate separate worksheet? Greg Purnell[_2_] Excel Worksheet Functions 3 April 5th 07 01:49 PM
another macro query - deleting a worksheet within a query DavidHawes Excel Discussion (Misc queries) 2 February 26th 07 10:05 AM
Need to Query Excel File Opened by Another User Paul Excel Discussion (Misc queries) 0 December 13th 06 05:57 PM


All times are GMT +1. The time now is 08:14 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"