Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel 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:16 PM
Registered User
 
Join Date: May 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using ADO to Query Excel

[I posted this on the Access VBA forum by mistake]

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E, 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 October 19th, 2006, 04:42 AM
Registered User
 
Join Date: Oct 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK, I came on the problem recently and even if I do not know how to handle it, I know where it comes from : the file seems to open in readOnly when the file is already opened by another user (or so it seems by you too). If the file isn't opened, everything seems to work fine ... and much faster





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using ADO to query data in Excel sheet Fails with maaron Excel VBA 4 June 9th, 2005 01:56 PM
Using ADO to Query Excel alex_w Access VBA 1 May 18th, 2005 11:59 AM
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.