Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 43
Default calling a stored access query from excel vba

I have a query already written inside of Access 2002 which I am trying to get
a recordset into Excel. Does anyone know if its possible to do this? I could
pass the SQL syntax into the connection object, but it's a pretty lengthy
query.This is what I have so far, but I get an error that says operation is
not supported for this type of object....
DbPath = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\test.mdb;"
Set ConnectionVariable = New ADODB.Connection
ConnectionVariable.ConnectionString = DbPath
ConnectionVariable.Open
Set cmd = New ADODB.Command
QueryString = "QueryName"
Set cmd.ActiveConnection = ConnectionVariable
With cmd
.Properties("Jet OLEDB:Stored Query") = True
.CommandText = QueryString
End With
Set oRecordset = New ADODB.Recordset
Set oRecordset = cmd.Execute
Set Output = Worksheets("Worksheet").Range("A2")
If oRecordset.EOF < True Then
oRecordset.MoveFirst
End If
Output.CopyFromRecordset oRecordset

Thanks,

Travis
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 205
Default calling a stored access query from excel vba

Hi Travis,

DbPath = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\test.mdb;"
Set ConnectionVariable = New ADODB.Connection
ConnectionVariable.ConnectionString = DbPath
ConnectionVariable.Open
Set cmd = New ADODB.Command
QueryString = "QueryName"
Set cmd.ActiveConnection = ConnectionVariable
With cmd
.Properties("Jet OLEDB:Stored Query") = True
.CommandText = QueryString
End With
Set oRecordset = New ADODB.Recordset
Set oRecordset = cmd.Execute
Set Output = Worksheets("Worksheet").Range("A2")
If oRecordset.EOF < True Then
oRecordset.MoveFirst
End If
Output.CopyFromRecordset oRecordset


I'd do it just using the recordset, rather than the command:

oRecordset.Open "QueryName", ConnectionVariable

The only time I'd go for the command is when needing parameters.

Regards

Stephen Bullen
Microsoft MVP - Excel

Professional Excel Development
The most advanced Excel VBA book available
www.oaltd.co.uk/ProExcelDev


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
Calling a common XLA Library file, stored on a network drive :) eXcellence Excel Discussion (Misc queries) 5 June 28th 05 02:10 PM
Calling a common XLA Library file, stored on a network drive :) eXcellence Setting up and Configuration of Excel 5 June 28th 05 02:10 PM
Problem Code: Retrieving Stored Access 03 Query Bettergains Excel Discussion (Misc queries) 2 December 7th 04 04:11 PM
calling an MS Access query from within VBA for Excel Shane Ambry Excel Programming 0 August 18th 04 08:38 AM
calling a stored procedure on MS SQL Server within MS Excel 2000 Witold Domienik Excel Programming 0 March 2nd 04 10:47 AM


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

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

About Us

"It's about Microsoft Excel"