|
|
| Next: DSN's and MSQuery |
| Author |
Message |
Alex External

Since: Jul 19, 2004 Posts: 161
|
Posted: Wed May 18, 2005 9:33 am Post subject: Using ADO to Query Excel Archived from groups: microsoft>public>excel>querydao (more info?) |
|
|
Hello,
The code and page references that I give here are from "Excel 2003 VBA, Wrox".
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 |
|
| Back to top |
|
 |
Guillermo Rebaza External

Since: May 27, 2005 Posts: 4
|
Posted: Fri May 27, 2005 11:21 pm Post subject: Re: Using ADO to Query Excel [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Alex wrote:
> Hello,
>
> The code and page references that I give here are from "Excel 2003 VBA, Wrox".
>
> 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
>
> ==========================================================
Hi,
I built macros using the MS ODBC Excel driver for doing query on the
same Excel Workbook the effects were: i) non secondary excel session,
but ii) memory exhausted (large amount of consumed memory as each query
is done). Now, I'm testing a version with QueryTables object it seems to
be more efficient in memory. Pls let me to know if you can resolve your
troubles.
Guillermo. |
|
| Back to top |
|
 |
Alex External

Since: Jul 19, 2004 Posts: 161
|
Posted: Sat Oct 08, 2005 12:31 am Post subject: Re: Using ADO to Query Excel [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Hello Guillermo
Thanks for your answer, apologies for the delay in replying.
I am not familiar with QueryTable objects, and they appear to be design to
handle only external database.
How do you do you make them handle spreadsheet tables (ranges) in the same
Excel Workbook?
Regards
a
--
Alex
"Guillermo Rebaza" wrote:
> Alex wrote:
> > Hello,
> >
> > The code and page references that I give here are from "Excel 2003 VBA, Wrox".
> >
> > 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
> >
> > ==========================================================
> Hi,
> I built macros using the MS ODBC Excel driver for doing query on the
> same Excel Workbook the effects were: i) non secondary excel session,
> but ii) memory exhausted (large amount of consumed memory as each query
> is done). Now, I'm testing a version with QueryTables object it seems to
> be more efficient in memory. Pls let me to know if you can resolve your
> troubles.
>
> Guillermo.
> |
|
| Back to top |
|
 |
Guillermo Rebaza External

Since: May 27, 2005 Posts: 4
|
Posted: Wed Nov 09, 2005 12:57 am Post subject: Re: Using ADO to Query Excel [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Hi Alex,
my code use QueryTables with an ADO datasource.
In pseudocode:
'Creating String Connection
strConn1 = "ODBC;Provider= MSDASQL;DRIVER=Microsoft Access Driver
(*.mdb);"
strConnection = strConn1 & _
"DefaultDir=" & Application.ActiveWorkbook.Path & "; " & _
"DBQ=" & Application.ActiveWorkbook.Path & "\Data.mdb;
" & _
"ReadOnly=1" & "; " & _
"DriverId=25;MaxBufferSize=2048;PageTimeout=5"
'Getting QueryTable Object
getQueryTable(Query Table Name, QueryTableObject,
ADO String Connection, Excel Range)
'Refreshing QueryTable
QueryTableObject.Refresh
where:
getQueryTable, seeks for a QueryTable Name
and create it if It doesn't exist.
'**********************************************
'* VBA Code *
'**********************************************
Function getQueryTable(strQTName As String, ByRef oQT As QueryTable, _
strConnection As String, rngDst As Range) As Boolean
Dim msheet As Worksheet
'Dim strQry As String
'Dim QryParams() As Variant
'Dim QryParams_DataType() As Variant
Dim bExistQT As Boolean
Dim iQT As Long
getQueryTable = False
bExistQT = findQueryTable(rngDst.Worksheet, strQTName, iQT)
If bExistQT Then
Set oQT = rngDst.Worksheet.QueryTables(iQT)
Else
'Borrando QueryTables presentes en WorkSheet (2 avoid multiple copies
of the same QT)
For iiQ = 1 To rngDst.Worksheet.QueryTables.Count
rngDst.Worksheet.QueryTables(iiQ).Delete
Next iiQ
Set oQT = rngDst.Worksheet.QueryTables.Add(strConnection, rngDst)
End If
'Adjusting QryTables properties
oQT.Name = strQTName
oQT.FieldNames = False
oQT.FillAdjacentFormulas = False
oQT.HasAutoFormat = False
oQT.PreserveFormatting = True
oQT.PreserveColumnInfo = False
oQT.AdjustColumnWidth = False
oQT.SaveData = False
oQT.MaintainConnection = False
oQT.RefreshStyle = xlOverwriteCells
getQueryTable = True
End Function
Function findQueryTable(msheet As Worksheet, strQTName As String, idx As
Long) As Boolean
findQueryTable = False
If msheet.QueryTables.Count > 0 Then
For iQT = 1 To msheet.QueryTables.Count
If (StrComp(msheet.QueryTables(iQT).Name, strQTName) = 0) Then
findQueryTable = True
idx = iQT
Exit Function
End If
Next iQT
End If
End Function
Alex wrote:
> Hello Guillermo
> Thanks for your answer, apologies for the delay in replying.
> I am not familiar with QueryTable objects, and they appear to be design to
> handle only external database.
> How do you do you make them handle spreadsheet tables (ranges) in the same
> Excel Workbook?
> Regards
> a
> |
|
| Back to top |
|
 |
redan

Joined: Aug 27, 2007 Posts: 2
|
Posted: Mon Aug 27, 2007 5:52 am Post subject: [Login to view extended thread Info.] |
|
|
Hello,
I have exactely the same problem. Did someone find a solution ?
Thanks in advance. |
|
| Back to top |
|
 |
|
|
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
| |
|
|