Thanx for the reply. I was afraid to give too much code
for fear of overload. But since you asked: ;>
Here's some more detail on what I'm trying to do. I'll
start with the query that works.
In Access, it is set up with five fields:
Group_Name Group
Success Sum
Failure Sum
date_resolve Where Month([date_resolve])=ReportMonth
date_resolve Where Year([date_resolve])=ReportYear
The first field is the one that all the others are
grouped on. The next two are sums. The last two are where
the parameters are entered. The field is listed twice
because Access shows it that way. I initially entered it
like this:
date_resolve Where Month([date_resolve])=ReportMonth
and Year([date_resolve])=ReportYear
But when I save it and close it, then reopen it in design
view, Access has split the field in to two. So I went
with it.
So if I run that query in Access, it'll prompt for the
ReportMonth and ReportYear. I enter those (8 and 2004 for
example) and the resulting dataset contains all data for
August 2004.
Now the queries whole raison d'etre is to supply data for
an Excel report. So in Excel I have VBA code that uses
ADO to connect to the database, supply the ReportMonth
and ReportYear, run the query, and return the results in
a recordset. My code is this:
cn.Open "Driver={Microsoft Access Driver
(*.mdb)};Dbq=C:\MyPath\MyDatabase.mdb;"
'cm is my Command object
'cn is my Connection object
'pm1 and pm2 are my parameters
'rs is my Recordset
'qryDailyBusinessHours is the query I just described
'ReportMonth is an integer variable containing 8
'ReportYear is an integer variable containing 2004
With cm
.ActiveConnection = cn
.CommandText= "qryDailyBusinessHours"
.CommandType = adCmdStoredProc
Set pm1 = cm.CreateParameter("date_resolve", adInteger,
adParamInput)
.Parameters.Append pm1
pm1.Value = ReportMonth
Set pm2 = cm.CreateParameter(InputField, adInteger,
adParamInput)
.Parameters.Append pm2
pm2.Value = ReportYear
End With
rs.Open cm
'Code to use theses results
rs.close
cn.close
set rs = nothing
set cn = nothing
set cm = nothing
set pm1 = nohting
set pm2 = nothing
This works.
Okay, still reading?
I have another access query that is virtually identical.
It has a group by field, two sum fields and the same
date_value fields described at top. It connects to the
same table for data. If I run it in Access, enter the
ReportMonth and ReportYear, I get a dataset with values.
So in my VBA, I have a second procedure that uses the
same exact ADO code mentioned above with the only change
being the query name.
It should work, but it doesn't. It returns NULL for each
field.
I've tried not destroying objects, running the good
procedure first, then the bad one, then in reverse order.
I still get NULL for the 'bad' one and correct results
for the 'good' one.
The good one ALWAYS works and the bad one ALWAYS DOESN'T.
So I'm just scratching my head trying to figure out what
one's got that the other doesn't.
Any ideas would be welcomed. I'll also post a solution if
I come up with it.
Stumped
(A/K/A tod)
-----Original Message-----
Hi Tod,
Off hand, it looks like you are using
"Date_Created" twice which does not
make sense to me (unless a typo?).
Would you mind posting back the SQL for
"qryDailyBusinessHours" here so we
can see what the name of your parameters
are? Otherwise, we would probably just be
wasting your time guessing.
If your query is expecting 2 integer parameters,
one for a "ReportMonth"
and another for a "ReportYear"
(in that order),
and ReportMonth/ReportYear are integer variables
assigned values in your code,
then we don't really have to know the parameter
names if we pass them using an array.
Dim cn As New ADODB.Connection
Dim cm As New ADODB.Command
Dim rs As New ADODB.Recordset
Dim ReportMonth As Integer
Dim ReportYear As Integer
ReportMonth = 1
ReportYear = 2004
' Open the connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\MyPath\MyDatabase.mdb;"
'create the command
Set cm.ActiveConnection = cn
cm.CommandText = "qryDailyBusinessHours"
'execute the command, passing values for parameters in array
Set rs = cm.Execute(, Array(ReportMonth, ReportYear), adCmdStoredProc)
'<do something with rs>
rs.Close
Set rs = Nothing
-------------------
My *guess* is that you might actually
only have one parameter "Date_Created,"
but you want to use your 2 variables.
Any further suggestions would require looking
at the SQL of your query.
Thanks,
Gary Walter
.