Passing 2 parameters to stored query?



I have an Access 2003 database that I need to write some VBA code for
to populate a table. The table is based on a query I have built in
Access queries. Right now I have 2 parameters that are passed to the
query from a form (DateFrom and DateTo). When I open the form and
populate the variables (DateFrom and DateTo) then open the query it
works fine.

My problem is that I need to do this from VBA coding and pass the 2
parameters to the query that way and populate my recordset. How do I
go about doing this? When I step through it, I find that the recordset
is not populating.

Here is some sample code that I have been trying to use.

Dim comm As ADODB.Command
Dim pmStartDate As ADODB.Parameter
Dim pmEndDate As ADODB.Parameter
Dim rst As ADODB.Recordset

Set comm = New ADODB.Command
Set comm.ActiveConnection = CurrentProject.Connection
comm.CommandText = "qrySupportRptTest"
comm.CommandType = adCmdStoredProc

Set pmStartDate = comm.CreateParameter("StartDate", adDBDate,
adParamInput, , Me.txtdatefrom)
comm.Parameters.Append pmStartDate

Set pmEndDate = comm.CreateParameter("EndDate", adDBDate,
adParamInput, , Me.txtDateTo)
comm.Parameters.Append pmEndDate

comm.Execute

Set comm = Nothing

Set rst = New ADODB.Recordset
With rst
Set .ActiveConnection = CurrentProject.Connection
.Source = "select * from QrySupportRptTest where DateFrom = #"
& pmStartDate & "#"
.LockType = adLockOptimistic
.CursorType = adOpenKeyset
.CursorLocation = adUseClient
.Open
End With


'On Error Resume Next

' Loop for record inside loop for date
' This keeps the dates grouped

If rst.RecordCount <> 0 Then
Do Until CalcDate = Me.txtDateTo
rst.MoveFirst
Do Until rst.EOF
If CalcDate >=
rst.Fields("QrySupportRptTest.StartDate").Value And CalcDate <=
rst.Fields("QrySupportRptTest.EndDate").Value Or CalcDate >=
rst.Fields("QrySupportRptTest.StartDate").Value And
rsPaxData.Fields("QrySupportRptTest.EndDate").Value Is Null Then
DoCmd.Execute "Insert into TblPaxReport (SptState,
SptType, CalcDate, PAX, UIC) values (" &
rst.Fields("QrySupportRptTest.SptState") & "," &
rst.Fields("QrySupportRptTest.SptType") & "," & CalcDate & "," &
rst.Fields("QrySupportRptTest.PAX") & "," &
rst.Fields("QrySupportRptTest.UIC") & ");"
End If
rst.MoveNext
Loop
CalcDate = DateAdd("d", 1, CalcDate)
Loop
End If

' Run report
'Docmd.OpenReport

' DoCmd.CloseQuery stPAXquery

' Clean up
rst.Close
Set rst = Nothing
Set comm = Nothing
Set pmStartDate = Nothing
Set pmEndDate = Nothing

.



Relevant Pages

  • Re: Combo Box Limit of 20 Fields
    ... record from a quey an use the columns to populate the controls on your form. ... Is the query based on the recordset of the form or is it from a different ... Set rst = Nothing ...
    (microsoft.public.access.forms)
  • RE: Data entry property & lookup
    ... Dave Hargis, Microsoft Access MVP ... most recently entere record. ... the most recently entered or you can add a date/time stamp field and populate ... I had put an unbound combo box in but didn't set the query up ...
    (microsoft.public.access.forms)
  • Re: Hard to explain - multiple update fields query...
    ... "John Spencer" wrote: ... query to get the data back into the AwardsTable ... AwardsTable in your Access database and do all the work there. ... You will need 40 queries to populate the table from your current table. ...
    (microsoft.public.access.queries)
  • Passing a value from one form to another
    ... 10 labels and a control array of say 10 buttons. ... This part of it works fine, please do not see this as a DB query. ... hide my 1st form and populate the control array of labels on this 2nd ... from the ID that is stored in the relevant button's tag. ...
    (microsoft.public.vb.enterprise)
  • Re: create and/or insert into a table, specifying columns once
    ... >have to create a query that comes up with all of the columns in two places: ... >first to create a new table and then to populate an existing table. ... for all iterations. ... I see no reason to prefer the third or fourth option over this. ...
    (microsoft.public.sqlserver.programming)