Error Opening a Query With Criteria Through Code (1 Viewer)

LaBam

Registered User.
Local time
Today, 11:42
Joined
Mar 21, 2002
Messages
48
May be this has been solved in this forum before but I can't find it. Here it goes.

I have a code I wrote to open a query and use its values to update a table. The query gets its Date range from the StartDate and EndDate controls on the form. The query runs fine if i open it by double-cliking on it while the form is open. But I get errors when I tried opening it from the code eventhough the form is open. I get the following message: TOO FEW PARAMETERS. EXPECTED 2. The query retuns no records. It looks like the code runs the query without picking the values from the date controls on the form. In the query criteria I have the folowing function:

Between [forms]![frmRevReports]![txtBegin] And [forms]![frmRevReports]![txtEnd]

However, when I removed this BETWEEN condition from the criteria in the query, the code runs just fine. That is, when it makes no reference to the date controls on the form.

What do I have to add to get the code to reference the dates on the form and supply them to the criteria of the query, "QRY_REVENUE_MINUTES_OUT_1"

Here is my code. Please help:
Code:
    Dim rstOutGoingMin As Recordset
    Dim rstTotalOutMin As Recordset
    Dim rstBalanceConsulting As Recordset
    Dim mMinutes As Double
    Dim mOutgoingCallsCount As Double
    Dim currNumber As String

    Set db = CurrentDb()
    db.QueryTimeout = 0
    
    Set rstOutGoingMin = db.OpenRecordset("QRY_REVENUE_MINUTES_OUT_1", dbOpenForwardOnly)
    With rstOutGoingMin
        Do While Not .EOF
            currNumber = !ANumber
            mOutgoingCallsCount = !OutgoingCallsCount
                Set rstTotalOutMin = db.OpenRecordset("REVENUE_REPORTS_ACCOUNTS", dbOpenTable)
                With rstTotalOutMin
                        .Index = "PrimaryKey"
                        .Seek "=", currNumber
                        If Not .NoMatch Then
                            .Edit
                            !Out_Going_Min = mMinutes
                            !Out_Going_Calls_Count = mOutgoingCallsCount
                            .Update
                        End If
                End With
                .MoveNext
          Loop
                rstTotalOutMin.Close
       End With
    rstOutGoingMin.Close
 

RuralGuy

AWF VIP
Local time
Today, 04:42
Joined
Jul 2, 2005
Messages
13,826
I have several suggestions. Disambiguate your declarations! ie:
Dim rstOutGoingMin As DAO.Recordset
Dim rstTotalOutMin As DAO.Recordset
Dim rstBalanceConsulting As DAO.Recordset


Since you are working with two different RecordSets at the same time, don't use the With function! It makes it more difficult to read. Use complete names each time you refer to a RecordSet property.

Open the 2nd RecordSet at the beginning and leave it open. Just use a MoveFirst inside the loop and quit opening the RecordSet inside the loop.

Dont use Seek! Use a FindFirst after a MoveFirst.

Set everything *you* set to = Nothing when you are done. It is good programming practice and you can't always trust Access to release memory when it goes out of scope.

Post back if you need additional assistance.
 

LaBam

Registered User.
Local time
Today, 11:42
Joined
Mar 21, 2002
Messages
48
Thank you very much RuralGuy. I'll try the siggestions you gave later in the day and get back to you ASAP. I'm kinda squeezed right now.;)
 

Users who are viewing this thread

Top Bottom