Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Using ADO to Query Excel

Hello,

The code and page references that I give here are from "Excel 2003 VBA,
Wrox" - very useful book for the non-professional programmer that I am.

My question is the following :

- I use VBA in Excel, and would like to use Excel itself as a data source
("Using ADO with Non-Standard Data Sources, Querying Microsoft Excel
Workbooks", p239). I'd like to keep a table in excel and use SQL expression
power to manipulate the data in the table (selection, grouping,
filtering...), rather than using the usual Excel lookups. I need to keep my
Excel session since I get real-time feeds, make calulations, and they
manipulate the calculated data with SQL.

- The table to query is located in the active workbook, as opposed to being
saved in another, inactive, file. I use a range name "TESTRNG" to define
this table in the Sheet1.

- I then dump the SQL result in a range located in the Sheet1, cell C10,
with the CopyFromRecordset statement.

- This does work, but I have an annoying secondary effect with the
following environment :

1) I have an (unrelated) excel session already open (Session 1) on my
Windows desktop.

2) I open a new, fresh, Excel session (Session2) and open my file, with the
table, and the VBA code.

3) I run the code in my file, Session2 : it does work, but the unwanted
effect is that my file opens itself again, as Read-Only, in the other
Session1!

I do not know what went wrong. It could be great if you could put me on the
right tracks.

Regards
Alex.

Using a very slightly modified version of the code provided p241 :

================================================== ========

Sub QueryWorkSheet()
Dim Recordset As ADODB.Recordset
Dim ConnectionString As String

ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=Excel 8.0;"

Dim SQL As String

'TESTRNG is an excel range name, which defines the table to query, with
field names in the first, header row, and records in other rows.

SQL = "SELECT * FROM TESTRNG;"

Set Recordset = New ADODB.Recordset

On Error GoTo Cleanup

Call Recordset.Open(SQL, ConnectionString,
CursorTypeEnum.adOpenForwardOnly, LockTypeEnum.adLockReadOnly,
CommandTypeEnum.adCmdText)

Call Range("C10").CopyFromRecordset(Recordset)

Cleanup:
Debug.Print Err.Description
If (Recordset.State = ObjectStateEnum.adStateOpen) Then
Recordset.Close
End If

Set Recordset = Nothing

End Sub

================================================== ========
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 318
Default Using ADO to Query Excel

Using ADOR and ADO you can create a recordset in memory and do simple
manipulation like sort, filter and find.
The solution with ADOR and ADO will then involve the step of first reading
the tabular data in your workbook into the ADO recordset. As far as I know
you will not be able to give SQL commands to the ADO recordset that will be
created but you will need to examine if filter functionality along with the
Sort will work for you.

Alok Joshi

"alexis suzat via OfficeKB.com" wrote:

Hello,

The code and page references that I give here are from "Excel 2003 VBA,
Wrox" - very useful book for the non-professional programmer that I am.

My question is the following :

- I use VBA in Excel, and would like to use Excel itself as a data source
("Using ADO with Non-Standard Data Sources, Querying Microsoft Excel
Workbooks", p239). I'd like to keep a table in excel and use SQL expression
power to manipulate the data in the table (selection, grouping,
filtering...), rather than using the usual Excel lookups. I need to keep my
Excel session since I get real-time feeds, make calulations, and they
manipulate the calculated data with SQL.

- The table to query is located in the active workbook, as opposed to being
saved in another, inactive, file. I use a range name "TESTRNG" to define
this table in the Sheet1.

- I then dump the SQL result in a range located in the Sheet1, cell C10,
with the CopyFromRecordset statement.

- This does work, but I have an annoying secondary effect with the
following environment :

1) I have an (unrelated) excel session already open (Session 1) on my
Windows desktop.

2) I open a new, fresh, Excel session (Session2) and open my file, with the
table, and the VBA code.

3) I run the code in my file, Session2 : it does work, but the unwanted
effect is that my file opens itself again, as Read-Only, in the other
Session1!

I do not know what went wrong. It could be great if you could put me on the
right tracks.

Regards
Alex.

Using a very slightly modified version of the code provided p241 :

================================================== ========

Sub QueryWorkSheet()
Dim Recordset As ADODB.Recordset
Dim ConnectionString As String

ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=Excel 8.0;"

Dim SQL As String

'TESTRNG is an excel range name, which defines the table to query, with
field names in the first, header row, and records in other rows.

SQL = "SELECT * FROM TESTRNG;"

Set Recordset = New ADODB.Recordset

On Error GoTo Cleanup

Call Recordset.Open(SQL, ConnectionString,
CursorTypeEnum.adOpenForwardOnly, LockTypeEnum.adLockReadOnly,
CommandTypeEnum.adCmdText)

Call Range("C10").CopyFromRecordset(Recordset)

Cleanup:
Debug.Print Err.Description
If (Recordset.State = ObjectStateEnum.adStateOpen) Then
Recordset.Close
End If

Set Recordset = Nothing

End Sub

================================================== ========

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Using ADO to Query Excel



very annoying but a fact of life:

you cannot use ADO to query an OPEN workbook

see:
http://support.microsoft.com/default...9998&Product=x
lw

BUG: Memory leak occurs when you query an open Excel worksheet by using
ActiveX Data Objects (ADO)


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


alexis suzat via OfficeKB.com wrote :

The

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
Excel 2007 / MS Query - editing existing query to another sheet Hotpepperz Excel Discussion (Misc queries) 0 June 13th 08 06:53 PM
Anyone Else Use Database Query to Query Another Sheet in the Same Excel Workbook? jocke Excel Discussion (Misc queries) 1 November 29th 05 01:44 PM
Anyone Else Use Database Query to Query Another Sheet in the Same Excel Workbook? jocke Excel Discussion (Misc queries) 0 November 28th 05 06:37 PM
Edit Query from Excel will not open query in MSQuery Michelle Excel Programming 0 February 21st 05 03:59 PM
How to use a Access Query that as a parameter into Excel database query Karen Middleton Excel Discussion (Misc queries) 1 December 13th 04 07:54 PM


All times are GMT +1. The time now is 01:16 PM.

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"