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