Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 12th, 2004, 09:32 AM
Registered User
 
Join Date: Jan 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Moving through a Recordset

On a monthly basis I want to create an "Actual vs Budget" report.

I have created a table called tblBudgetActual that contains budgetary revenue amounts. The primary key is composed of 3 fields [Territory], [Year], and [Month]. This file also contains a field for Actual revenue which will be updated by the routine below, just before the Budget vs Actual Report is created.

A query is created (qryActual) that gathers the Territory, OrderDate, and Revenue amount for each order.

This routine starts by entering the Year into a Form. The 1st thing is that all Actual Revenue values from tblBudgetActual are reset to 0 for each record (row) in the specified year.

Now for the problem. I am attempting to step through qryActual from the beginning (it is ordered By Territory and By OrderDate - ascending) and add the specific revenue to the appropriate record in tblActualBudget. For example, there is a record in qryActual for Territory = "B001", OrderDate = 2/23/04, Revenue = $12,670; this needs to get added to tblBudgetActual where Territory = "B001", Year = "2004", and Month = "02".

How do I migrate through qryActual? I have been trying to use the following routine unsuccessfully (I have left out the number to String conversions for simplicity)

Dim db As DAO.Database, rcdBA As DAO.Recordset, rcdA As DAO.Recordset
Set db = CurrentDb()
Set rcdBA = db.OpenRecordset("tblBudgetActual")
Set rcdA = db.OpenRecordset("qryActual")

' --> Reset rcdBA Actual Revenue values to 0 goes here (no problem)

'Move current Order revenue values into rcdBA; the variable strMo is extracted from rcdA!OrderDate into "01", "02", etc.

Do Until rcdA.EOF
rcdBA.FindFirst(rcdBA!Territory=rcdA!Territory And rcdBA!Month=strMo)
Do While rcdBA!Territory=rcdA!Territory And rcdBA!Month=strMo
    rcdBA.Edit
    rcdBA!Revenue = rcdBA!Revenue + rcdA!Revenue
    rcdBA.Update
    rcdA.MoveNext
Loop
Loop

From what I can tell the main problem seems to be with the "Find" statement. How do I jump in to the correct record in rcdBA? Thanks for any help.
 
Old April 12th, 2004, 03:11 PM
Registered User
 
Join Date: Mar 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Instead of a Do While you will probably want to use a FindFirst with a FindNext. Something like this

Do Until rcdA.EOF
   rcdBA.FindFirst("Territory='" & rcdA!Territory & "' AND Month='" & strMo & "'"
   (This is assuming Territory is a string value)

   Do Until rcdBA.NoMatch=True
      rcdBA.Edit
      rcdBA!Revenue = rcdBA!Revenue + rcdA!Revenue
      rcdBA.Update
      rcdBA.FindNext("Territory='" & rcdA!Territory & "' AND Month='" & strMo & "'"
   Loop
   rcdBA.MoveNext
Loop
 
Old April 12th, 2004, 08:47 PM
Registered User
 
Join Date: Jan 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Your response has allowed me to get to a solution that is much simpler than I originally was thinking. My original thinking was to have 2 loops; the outer for rcdBA (tblBudgetActual) and the inner for rcdA (qryActual). I believe the single loop below will work, but I got the idea from your response. Thanks for taking a few minutes to respond.

Do Until rcdA.EOF
  strMo = Month(rcdA!OrderDate)
  If Len(strMo) = 1 Then
     strMo = "0" & strMo
  End If

  rcdBA.FindNext ("Territory='" & rcdA!Territory & "' AND Month='" & strMo & "'")

  rcdBA.Edit
  rcdBA!Revenue = rcdA!Revenue + rcdBA!Revenue
  rcdBA.Update
  rcdA.MoveNext
Loop







Similar Threads
Thread Thread Starter Forum Replies Last Post
Clone DAO Recordset into ADO Recordset kamrans74 VB How-To 0 March 6th, 2007 11:57 AM
Query from Recordset into another Recordset kamrans74 Pro VB Databases 5 March 5th, 2007 04:17 PM
ADODB.Recordset (0x800A0CB3)Current Recordset does tks_muthu Classic ASP Databases 0 June 16th, 2005 07:22 AM
Convert ADO recordset to DAO recordset andrew_taft Access 1 May 5th, 2004 02:31 PM
Moving around a recordset Blaise Classic ASP Databases 6 June 25th, 2003 06:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.