Solved

Capturing a calculated control

Posted on 2014-01-31
7
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 85

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

729 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