Results 1 to 8 of 8
  1. #1
    Steve82 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2013
    Location
    Cape Cod
    Posts
    4

    How to avoid using an update during each cycle of a loop?

    I've written some code that calculates the interest portion of 180 loan payments and updates the fields INT1 …INT180. It works fine but takes forever to run because its doing 180 updates per record. Is their a more efficient way to do this?
    Public Function IntPmt()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim f As String
    Dim t As Double
    Dim c As Double
    Dim r As Double
    Dim b As Double
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("tbl_IntPmt", dbOpenDynaset)
    Do While Not rs.EOF
    t = rs![TERM]
    r = rs![Rate] / 12


    b = rs![RPMT BAL]
    c = 1
    For c = 1 To t
    f = "INT" & c
    rs.Edit
    rs(f) = Abs(IPmt(r, c, t, b, 0, 0))
    rs.Update
    Next c
    rs.MoveNext
    Loop

    rs.Close

    Set rs = Nothing
    Set db = Nothing
    End Function

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,623
    Did you try putting the rs.Update outside the loop?

    Why are you saving calculated data?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Steve82 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2013
    Location
    Cape Cod
    Posts
    4
    I tired that but it wouldn't work. I'm saving because I'm generating an amortization schedule; principal, interest and dates paid.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,623
    Still don't understand why you can't just do a query with the calcs and use that as the amortization 'table'.

    Actually, try the Update just after the Next c line. Should only have to update the record once, not after each field populated.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Steve82 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2013
    Location
    Cape Cod
    Posts
    4
    I'm trying to reduce the code. I'm using a variable to change the field names so I don't have to list all 180 fields. Update after Next c didn't work.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,623
    I understand the technique and use it.

    I have a procedure that opens a recordset, adds a new record, in a loop populates each field of the new record, then outside the loop runs the Update. It works. Unfortunately, never had need to do this with existing record. Probably also need to move the rs.Edit outside the For loop, keep the two commands at the same level:

    Do While Not rs.EOF
    t = rs![TERM]
    r = rs![Rate] / 12
    b = rs![RPMT BAL]
    c = 1
    rs.Edit
    For c = 1 To t
    f = "INT" & c
    rs(f) = Abs(IPmt(r, c, t, b, 0, 0))
    Next c
    rs.Update
    rs.MoveNext
    Loop

    Having 180 similar rows indicates non-normalized data structure.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Steve82 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2013
    Location
    Cape Cod
    Posts
    4
    I'll give it a shot. I guess I could read from on table or record set and add to another. Is that what you're talking about?

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,623
    My procedure is taking data from one record and creating a new record in same table.

    In your case, I suggested you calculate the interest values in a query and use the query as source for amortization values but you would have to build 180 expressions because of non-normalized table structure.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Update table from vba on loop
    By twmr2000 in forum Access
    Replies: 4
    Last Post: 10-24-2013, 12:04 PM
  2. Replies: 1
    Last Post: 05-17-2013, 08:35 AM
  3. How to update subform records using loop
    By tahirsatti in forum Forms
    Replies: 15
    Last Post: 05-13-2013, 01:22 AM
  4. Cycle Text Boxes with a For next loop
    By Gary in forum Programming
    Replies: 3
    Last Post: 07-20-2010, 09:32 AM
  5. update table in loop based on IF statement
    By LALouw in forum Access
    Replies: 0
    Last Post: 07-26-2009, 08:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums