Question : Using ADO on Access Database 'parameter query'

I have a union query in my Access Database that I want to use ADO to set as a recordset.  The problem is that the query has a parameter 'Year' setup in access, so that when you double click the query in access you are prompted for the Year.  Since this parameter is actually passed to the union query in access from the two query that make up the union, I can't execute the SQL statement and just replace it with a variable very easily.  I fingured there must be a way to pass the parameter.  I'm pretty sure the code below corrosponds to the example I've seen in my research, but it does not want to work, and I recieve and automation error.  Can someone let me know where I'm going wrong?  Is it because I'm running the union query where the parameters really don't exist?  I'm running the code in and Excel workbook using VBA.

Access 2003
Excel 2007
Win XP
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
oConnString = "Driver={Microsoft Access Driver (*.mdb)};Dbq=N:\emPath.mdb"
 
Set oConn = New ADODB.Connection
Set rs = New ADODB.Recordset
Set oCmd = New ADODB.Command
oConn.Open oConnString
 
Year = InputBox("Year")
 
Set oCmd.ActiveConnection = oConn
oCmd.CommandText = "qryQueryName"
oCmd.CommandType = adCmdStoredProc
oCmd.Parameters.Append oCmd.CreateParameter("Year", adInteger, adParamInput)
oCmd.Parameters("Year") = Year
 
Set rs = oCmd.Execute

Answer : Using ADO on Access Database 'parameter query'

Two things:

I couldn't see the name of the Parameter in the Join you posted, but you can eliminate any error there by asking ADO to populate the Parameters collection and then assigning your value:
  oCmd.Parameters.Refresh
  oCmd.Parameters(1).Value = InputBox("Year")
This will also allow you to check the names/types of the parameters the query is actually expecting by examining the parameters collection after this statement has executed.
It's one extra round-trip, but once things are sorted you could always revert to creating the parameter yourself.

The other thing is that you're using the (default) generic ODBC driver, MSDASQL (though I can't think that it will have caused the problem). You could always try the more direct Jet OLEDB driver:
  Provider=Microsoft.Jet.OLEDB.4.0; Data Source=N:\emPath.mdb

 


Random Solutions  
 
programming4us programming4us