简体   繁体   中英

Problems getting a subtotal from a subform in Access 2010

I've got a form (frmNewHours) with some controls on it which I use to alter the subform (tblHoursSubform), which is a datasheet view subform containing 2 visible columns for the user (Project and MTDHours). I want the subtotal of the MTDHours column displayed in an unbound field on my main form.

What I tried first:

  • I put an unbound control (Subtotal) in the main form
  • In the Form Footer of the subform I put an unbound control (HoursSubtotal) with a control source =Sum([MTDHours])
  • In the Subtotal control on the main form, I set it's control source to =[tblHoursSubform]![HoursSubtotal]

This worked perfectly at first, but as I continued to develop the form (maybe irrelevant details, but I use a couple controls on the main form to change the recordsource for the subform and the recordsource of the Project combobox control in the subform) the subtotal display stopped working. I deleted the subtotal controls and recreated them the exact same way and it worked again, which didn't really make sense to me but I continued on.

I finished developing the form and tested it on another computer, and the subtotal field stopped working again (the subform's control is erroring for some reason)

What I tried next:

  • I wrote a function that queries and returns the subtotal, and changed the control source to =GetHoursSubtotal(GetUserID(),[SelectedSession])

This works when the form is loaded, but as data in the subform changes the subtotal does not update, so I added the following code to the subform:

Private Sub Form_Current()
    Me.Parent.Controls("Subtotal").Requery
End Sub

While this works, it takes about a full second to refresh the subtotal control, during which the control is empty. So while it's functional, it's definitely not working as intended.

So now I'm at a loss for what to try next. Has anyone else experienced this and found a solution?

EDIT: Got it working. I went back to the original solution somewhat, but I changed the subtotal's control source to =Nz(Sum([MTDHours]),0) and now no more problems.

In the 1st attempt seems that when you changed the recordsource for subform, something got lost in the properties of the Sum control. Really I can't understand why it doesn't work on another computer. It makes no sense! When you requery the subform you update the sum control. You should also requery the main form to get the new value.

The 2nd solution is surely slower cause you are using a custom function instead of an embedded one. If it's a simple sum you can try to use the DSum function, filtering the recordset with the ID of the main form if required. You can assign the DSum function to the subtotal control of the main form. Again after requerying the sub you should have to requery the main.

Note that current should not be the right event 'cause you're forcing to update at each record (this doesn't matter if the form is not a continuous form). It would be better to update the subtotal when you fire the update subform event.

Let me know if it worked.

Bye

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM