Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 18th, 2005, 09:37 AM
Registered User
 
Join Date: May 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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.

- 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

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


Alex
 
Old May 18th, 2005, 11:59 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

You may want to post this in the Excel VBA forum.

mmcdonal





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using ADO to Query Excel alex_w Excel VBA 1 October 19th, 2006 04:42 AM
Using ADO to query data in Excel sheet Fails with maaron Excel VBA 4 June 9th, 2005 01:56 PM
ado excel/access loane Excel VBA 5 August 17th, 2004 10:18 AM
Delphi ADO Query acabrera Oracle 2 February 2nd, 2004 12:49 PM
ADO Dynamic Query - Please help rudidoku Access 1 July 23rd, 2003 06:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.