Solved

Capturing a calculated control

Posted on 2014-01-31
7
324 Views
Last Modified: 2014-02-01
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.
0
Comment
Question by:DatabaseDek
7 Comments
 
LVL 12

Accepted Solution

by:
Paul_Harris_Fusion earned 200 total points
ID: 39823570
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
 
LVL 84

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 100 total points
ID: 39823574
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
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 200 total points
ID: 39823597
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:DatabaseDek
ID: 39823648
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
 
LVL 12

Expert Comment

by:Paul_Harris_Fusion
ID: 39823684
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
 
LVL 12

Expert Comment

by:Paul_Harris_Fusion
ID: 39823686
Sorry - in your form you need
Form.OnChange 
g_MyField = [Z]     ' store your calculation in the global variable

Open in new window

0
 

Author Closing Comment

by:DatabaseDek
ID: 39826420
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now