View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Mike Mike is offline
external usenet poster
 
Posts: 3,101
Default Query from Excel to Access - Passing Parameters?

Why not just do the query from excel like this.

Option Explicit
Private Sub getDataFromAccess()
'Needs reference the Axtive X Library 2.0 or higher
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim fld As ADODB.Field
Dim sSQL As String, strConn
Dim iCol As Long

strConn = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\Path to data base\DatabaseName.mdb;Persist Security
Info=False"

sSQL = "Select Field1, Field2 From TableName"

Set cnn = New ADODB.Connection
Set rs = New ADODB.Recordset

cnn.Open strConn
rs.Open sSQL, cnn, adOpenStatic, adLockOptimistic
iCol = 1
For Each fld In rs.Fields
Cells(1, iCol).Value = fld.Name
iCol = iCol + 1
Next fld
Cells(2, 1).CopyFromRecordset rs
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing
End Sub

"Marc T" wrote:

Hi all,

I have an Access Database which combines data from an external source
(Oracle based) with it's own data. Within the database is a query which
prompts for input such as 'choose week'. I need to run this query and
manipulate the data in Excel.... is there any way to pass the 'week'
parameter from Excel as can be done when doing a simple query via MS query? I
have tried a few ways but keep getting a Parameter Expected error.

I suppose the problem is trying to pass the parameter from Excel to MS
Query, and then on to Access.

Any advice most appreciated!
Marc