Results 1 to 2 of 2
  1. #1
    martingaleh is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2015
    Posts
    22

    Make change on main form after update on continuous subform

    I have a subform in continuous display and I want to make a change on the mainform based on a change in the subform. When I make a change in a control on the subform, I want an aggregate sum in the main form to update. Let's call this control control1. I can get the value of control1 with Me!control1 and it is the updated value. Control1 is bound to field1 in the underlying recordset. When afterUpdate event on the control fires, I look at the Me!recordset.field1 in my code and its still the old value. So if I want to sum the field1 values of every record in the recordset, it will be wrong, it will contain the old value of control1 on the record I just updated. My solution is to set the field1 value to the control1 value and then sum it. Here's the code:
    Code:
    Private Sub quantity_AfterUpdate()
        Me.Recordset.Edit
        Me.Recordset!field1 = Me!control1
        Me.Recordset.Update
        Dim rs As Recordset
        Set rs = Me.RecordsetClone
        Dim sum As Long
        sum = 0
        With rs
            If .RecordCount = 0 Then
                returnTotal = sum <--let's pretend I didn't just cut and paste this code from elsewhere to make this code sample more obvious
            Else
                .MoveFirst
                Do While Not .EOF
                    sum = sum + (!Price * Nz(!field1, 0))
                    .MoveNext
                    
        
                Loop
                    returnTotal = sum
            End If
            .Close
            
        End With
    End Sub
    The problem with this solution is it's dog slow because access has to think about it for some time. Almost 2 seconds. It should just work without this first step. Does anyone have code that does this?

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    In the footer of the subform, can't you have a control (maybe named "Amount") that sums "Price * Nz(field1, 0)"? Make it hidden so it can't be seen.
    Then have code that sets Me.ReturnTotal to the value of the control (maybe named "Amount") in the subform footer.

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

Similar Threads

  1. Replies: 2
    Last Post: 12-07-2014, 01:15 PM
  2. Update fields from subform to main form
    By gstylianou in forum Access
    Replies: 4
    Last Post: 06-10-2014, 12:59 AM
  3. Update Main Form From Subform
    By burrina in forum Forms
    Replies: 7
    Last Post: 02-06-2013, 03:55 PM
  4. Requery subform after update of main form
    By gemadan96 in forum Forms
    Replies: 3
    Last Post: 10-17-2012, 02:33 PM
  5. Replies: 7
    Last Post: 11-07-2011, 06:31 AM

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