Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Use ADO to retrieve data from Access Parameter Query

Hi All
can someone please help with some sample ADO code - I need to run an Access
Parameter Query *from EXCEL* in my remote Access Database, which expects two
parameters and return the data to sheet1.

Thanks for any help.

Regards
Andrew


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default Use ADO to retrieve data from Access Parameter Query

Here is a function that returns a recordset based on a SQL statement.

Private Const m_cDBLocation As String = "D:\ForecastNew.mdb"

Public Function RunQuery(ByVal strSelect As String, ByVal strFrom As String, _
ByVal strWhere As String, ByVal strOrderBy, ByVal blnConnected As Boolean)
As ADODB.Recordset
Dim strConnection As String

strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" &
m_cDBLocation & ";"

Set RunQuery = New ADODB.Recordset
With RunQuery
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockBatchOptimistic
End With

RunQuery.Open strSelect & " " & strFrom & " " & strWhere & " " &
strOrderBy, strConnection, , , adCmdText
If blnConnected = False Then Set RunQuery.ActiveConnection = Nothing
End Function


The first for arguments for the function are the parts of the SQL Statement
and the last argument is whether you want to leave the coneection to the
database open. Leave it open if you wnat to update the record in the
database. You can get the SQL statment from the access database by changing
the vie of the query to SQL. You will need to do some playing to replace the
parameters with the variables.

HTH
"CodeMonkey" wrote:

Hi All
can someone please help with some sample ADO code - I need to run an Access
Parameter Query *from EXCEL* in my remote Access Database, which expects two
parameters and return the data to sheet1.

Thanks for any help.

Regards
Andrew



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Use ADO to retrieve data from Access Parameter Query

Jim
thanks for the response, but I believe that you misunderstood the question.
I want to run an existing Access *Parameter* Query and pass 2 params to the
query. I have managed to do this using the Parameter object in ADO now. Here
is my code for the benefit of all (most code in the ngs seems to be DAO):

Sub CollectDataByRegion()

Dim com As ADODB.Command
Dim col As Integer
Dim Param1 As ADODB.Parameter
Dim Param2 As ADODB.Parameter
Cells.Clear
Set com = New ADODB.Command
Dim rst As ADODB.recordset
com.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=PathToDatabase\MyDatabase.mdb;Persist Security Info=False;Jet
OLEDB:Database Password=SomePassword"
com.CommandText = "MyQuery"

Set Param1 = com.CreateParameter(, adDate, adParamInput)
Param1.Value = "10/02/04"
com.Parameters.Append Param1

Set Param2 = com.CreateParameter(, adDate, adParamInput)
Param2.Value = "10/10/04"
com.Parameters.Append Param2

Set rst = com.Execute


'Write the field names
For col = 0 To rst.Fields.Count - 1
Range("A1").Offset(0, col).Value = rst.Fields(col).Name
Next

' Write the recordset
Range("A1").Offset(1, 0).CopyFromRecordset rst
rst.Close
Set com = Nothing
End Sub

Regards
Andrew

"Jim Thomlinson" wrote in message
...
Here is a function that returns a recordset based on a SQL statement.

Private Const m_cDBLocation As String = "D:\ForecastNew.mdb"

Public Function RunQuery(ByVal strSelect As String, ByVal strFrom As

String, _
ByVal strWhere As String, ByVal strOrderBy, ByVal blnConnected As

Boolean)
As ADODB.Recordset
Dim strConnection As String

strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" &
m_cDBLocation & ";"

Set RunQuery = New ADODB.Recordset
With RunQuery
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockBatchOptimistic
End With

RunQuery.Open strSelect & " " & strFrom & " " & strWhere & " " &
strOrderBy, strConnection, , , adCmdText
If blnConnected = False Then Set RunQuery.ActiveConnection = Nothing
End Function


The first for arguments for the function are the parts of the SQL

Statement
and the last argument is whether you want to leave the coneection to the
database open. Leave it open if you wnat to update the record in the
database. You can get the SQL statment from the access database by

changing
the vie of the query to SQL. You will need to do some playing to replace

the
parameters with the variables.

HTH
"CodeMonkey" wrote:

Hi All
can someone please help with some sample ADO code - I need to run an

Access
Parameter Query *from EXCEL* in my remote Access Database, which expects

two
parameters and return the data to sheet1.

Thanks for any help.

Regards
Andrew





  #4   Report Post  
Posted to microsoft.public.excel.programming
TK TK is offline
external usenet poster
 
Posts: 177
Default Use ADO to retrieve data from Access Parameter Query

HI CodeMonkey

"CodeMonkey" wrote:

Jim
thanks for the response, but I believe that you misunderstood the question.


I want to run an existing Access *Parameter* Query and pass 2 params to the
query. I have managed to do this using the Parameter object in ADO now.


Well if it works and you know how to do it what's the question???

I can post you some code but I m not sure what it is your having
problems with?
Do you want to pass the parameter per an Excel cell or
an input box or hard coded as in your example or is that
not even the problem?

Good Luck
TK


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
pass parameter to access query Bill Manville Links and Linking in Excel 4 May 1st 23 03:45 AM
Get External Data from Access Parameter Query NewUser Excel Discussion (Misc queries) 0 December 1st 07 03:24 AM
import data from an Access parameter query PluckyMo Excel Discussion (Misc queries) 2 April 12th 06 04:42 AM
How to use a Access Query that as a parameter into Excel database query Karen Middleton Excel Discussion (Misc queries) 1 December 13th 04 07:54 PM
Pass Parameter to Access Query Al Excel Programming 3 April 29th 04 10:15 AM


All times are GMT +1. The time now is 02:49 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"