View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Stephen Bullen[_4_] Stephen Bullen[_4_] is offline
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