简体   繁体   中英

MSAccess: Call a summation from a subform (in datasheet view) to be displayed in a textbox on the master form?

Sorry if this question is simple but I have googled and haven't found a satisfactory answer.

I'm creating an engineering cost estimator. I have a form that takes inputs as ISO/Drawing , and for each drawing number are many subforms where you can input ComponentDesc and should spit back out a TotalHours to complete number. I've included a picture (sorry for bad quality)

From the image, the table on the bottom is a subform in datasheet mode (which is usually hidden and located in the footer) which will calculate appropriate Total MH (manhours) for the ComponentDesc inputted into the subform on the right. I would like the small (and incomplete) textboxes to the left (below the title "MH Totals for ISO/Drawings") to display the aggregate total from the subform on the bottom.

I've been trying to use DSUM() to define Control Source for the textbox but it keeps coming back with #ERROR as seen in the textbox to the left. Right now what I have typed out is:

=DSum("[Total MH]","frm-PipingHandleMH")

in the expression builder. [Total MH] being my field and frm-PipingHandleMH being the subform on the bottom. I've tried to put brackets around everything but it didn't work (even though I'm not exactly sure what brackets usually do). Any advice?

DSum (and all domain aggregate functions) acts on a table or query. If you want to use that approach you need to refer to the source of that form and use a filter parameter to limit to appropriate records the IE if the form's datasource is qry-PipingHandles and if the form you are trying to sum on currently is showing handles for widget 4 then it would be something like:

=dsum("[Total MH]","qry-PipingHandles","[widgetID] = 4")

Note that if that 4 was the currrent state of form then you need to pass it in, so something like:

=dsum("[Total MH]","qry-PipingHandles","[widgetID] = " & [frm-PipingHandlesMH]![WidgetID].Value)

Where you reference the field in the form and append it onto the string that is applied as a filter to the source for Dsum.

Another approach is to put a subtotal in the footer of the form (iirc you don't actually need to show the footer) and then reference that footer control from the parent form.

Brackets are needed to demarcate names that include spaces or other odd characters, they also can be used (eg in query design view) to force Access to treat something as a name rather than a string literal.

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