Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Capturing a calculated control

Posted on 2014-01-31
7
Medium Priority
?
336 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 800 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 400 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 800 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

604 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