Capturing a calculated control

I have an unbound calculated control [Z] on a form. It gets it's value from calculated control A +calculated control B +calculated control C +calculated control D . A, B, C & D get their values from subforms within the form. All works fine when the form is visible and on the current record but now I want to use the value of control [Z] in a report.

What is the most reliable way to get that value into a field (lets say [Total]) for later use bearing in mind that all calculated controls change with almost every change of any field in the form or subform.

Normally I would create a calculated field in the forms underlying query. But it is far too involved and not really possible in this instance.
Derek BrownMDAsked:
Who is Participating?
 
Paul_Harris_FusionCommented:
The only (rather messy) way I can think of is to use VBA to store the results of your calculation in a global variable and declare a function to return that variable.   You can then reference the function from your report.

However,  I would never do it this way myself as I think the approach is messy and can be difficult to debug.  
It also means your report can only ever be launched from the form.


I would have thought your 'Normal' approach would be better
i.e.  Perform the calculation in an underlying query
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Why not create a query?

Assuming that the data on your forms and subforms come from tables, just recreate the operations needed to fill those calculated controls on your forms, and use that for your report.

You can store the output of calculated controls, of course, but given your statement "bearing in mind that all calculated controls change with almost every change of any field in the form or subform. " I'd be leery of doing that.
0
 
mbizupCommented:
There are several possible approaches... the best approach depends on the nature of your report (which you haven't described here).

-- Does your report display the same overall data as your form?
If YES, then an easy start is to copy/paste all of your form controls (Ctrl-A, Ctrl-C, Ctrl-V) from your form to your report, including the calculated controls, and also copy the recordsource from the form to the report.  (Earlier versions of Access had a 'Save as report' feature, but I'm not seeing it offhand in 2010).

-- Are the recordsources different and you need to add these calculated fields to your report?
As Scott described, you can work this out through queries or other operations, such as DLookup (etc) to fill the calculated controls.

Alternatively, you can leave the form open in the background when you open the report, and refer to the form's calculated control from the report.  ie: using the form reference either in the report's query or directly from the control source property of controls on your report:

= Forms!YourFormName!YourCalculatedControl
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Derek BrownMDAuthor Commented:
Thank you.

The report will have between one and a hundred records. The main form is actualy a large sub form. It didn't seem relevant when I posted but I can see it is.

I like to create reports based on the form's query. Then I am confident that what I get in a report is guaranteed to show the same results.

From your posts it seems I will have to create a separate query and copy the conditions from the calculated controls to the new query. I was hoping something like this in the form's on change property

Form.OnChange
New field = [Z]
0
 
Paul_Harris_FusionCommented:
As I mentioned previously you could do:

In your form:
Form.OnChange 
New field = [Z] 

Open in new window


In a module:
 
Public g_MyField as string

Public Function MyField () as string
    MyField = g_MyField
End Function

Open in new window


In a report:
     Set the datasource for a control to the MyField() function


But I would still do it with a query myself!
0
 
Paul_Harris_FusionCommented:
Sorry - in your form you need
Form.OnChange 
g_MyField = [Z]     ' store your calculation in the global variable

Open in new window

0
 
Derek BrownMDAuthor Commented:
Thank you all for confirming my fears.

Three hours later and I have a multi table multi calculated field query that gets it done.

I hate hard work!

Thanks again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.