473,503 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable Calculated values

41 New Member
Hi:

I have a need to have several different formulas for a calculated value on a form and report. Is there a way to store the formula in a field then refer to that field in the control source of the control property? Thanks,

Patti
Mar 9 '08 #1
20 3693
ADezii
8,834 Recognized Expert Expert
Hi:

I have a need to have several different formulas for a calculated value on a form and report. Is there a way to store the formula in a field then refer to that field in the control source of the control property? Thanks,

Patti
If I interpret you correctly, you must explicitly refer to the Formula/Function in the Control Source of the Control itself, and not to a Field containing the Formula. What, if I may ask, are you trying to accomplish?
Mar 10 '08 #2
patriciashoe
41 New Member
If I interpret you correctly, you must explicitly refer to the Formula/Function in the Control Source of the Control itself, and not to a Field containing the Formula. What, if I may ask, are you trying to accomplish?

I have a form with a calculated values that divides two fields. THis formula works for all but a few of the items in the form which requires a different calculation. In some cases the result that is currently obtained needs to further divided by another number.
Mar 10 '08 #3
ADezii
8,834 Recognized Expert Expert
I have a form with a calculated values that divides two fields. THis formula works for all but a few of the items in the form which requires a different calculation. In some cases the result that is currently obtained needs to further divided by another number.
Why not?
  1. Create a Calculated Field in the underlying Record Source for the Form. This Field would call a Public Function and pass to it the value of the Fields for each Record. Within the Function, the Fields would be analyzed, and the appropriate result would be passed back to the Calculated Field.
  2. Apply similar logic in the Form's Current() Event.
  3. If you need further clarification, just let me know.
Mar 10 '08 #4
patriciashoe
41 New Member
I can use a little more direction. Of the 150 records there are only six or seven that require a different formula. These records are flagged. Now, if you can elaborate just a little more on your solution, I can work through it. I can code but I have to work at it. thanks,

Patti
Mar 10 '08 #5
ADezii
8,834 Recognized Expert Expert
I can use a little more direction. Of the 150 records there are only six or seven that require a different formula. These records are flagged. Now, if you can elaborate just a little more on your solution, I can work through it. I can code but I have to work at it. thanks,

Patti
Kndly provide me a little more information, so that we can get your on the right track:
  1. Table Name.
  2. Field Names that are involved in the Formulas and their Data Types.
  3. Formula that is applied to 'Flagged' Records.
  4. Formula that is applied to 'Non-Flagged' Records.
  5. Flagged Field Name and Data Type.
  6. Value in 'Flagged' Field that sets the Flag (True, "FormulaB", etc.).
  7. Can either Field that is involved in the Formulas be NUll? (no value). If so, how do you handle this case?
  8. Any other info you see as relevant.
  9. I'll check back this afternoon, and we'll see what we can do.
Mar 10 '08 #6
patriciashoe
41 New Member
Thanks again,

The table is called teacherresoruces. The fields are

buidlingID
TeacherFTE (number)
Enroll_students (number)
tr_year (number)
subjectID(number) Note that in the subjects table there are unique subjectid, this is where I have a field called divisor to indicate a different formula. I was hoping to have the actual formula. Fro now it is a numeric field that I use to do the division as noted below.

The calculated value on the report and form is enroll_students/teacherFTE This works for 90 percent of the records. With the exception of two subjectid, the formula is changed as follows:

(student_enroll/teacherFTE)/5 or some other number. Right now I am doing the following. If there is no change in the formula the divisor is 1. Therefore the formula is (student_enroll/teacherfte)/[divisor]. I am plugging in the oher divisors and this seems to work. however, I anticipate greater complexity in the future so I don't know if this work around is viable in the long term. Two formulas are more involved and have to reference an enrollment field that is located on a sub form off the main form and is also part go query I am using. That is it in a nutshell. If you can get me moving in the right direction I can probably fill in the rest. Many thanks,

Patti
Mar 10 '08 #7
NeoPa
32,557 Recognized Expert Moderator MVP
One way that may work is to define a Public Function in a standard code module that can handle a list of different formulas.
You would pass (from your SQL) parameters reflecting the formula required and the variables to be used.
For instance :
Expand|Select|Wrap|Line Numbers
  1. Public Function Compute(ByVal Formula As String, _
  2.                         ByVal Students As Double, _
  3.                         ByVal TeacherFTE As Double, _
  4.                         ByVal Divisor As Double) As Double
  5.   Select Case UCase(Formula)
  6.   Case "MAIN"
  7.     Compute = Students / TeacherFTE / Divisor
  8.   Case "ALTERNATE1"
  9.     Compute = 3.14159 * Students / TeacherFTE
  10.   Case "ALTERNATE2"
  11.     Compute = TeacherFTE / Students / Divisor
  12.   End Select
  13. End Function
Mar 10 '08 #8
ADezii
8,834 Recognized Expert Expert
Thanks again,

The table is called teacherresoruces. The fields are

buidlingID
TeacherFTE (number)
Enroll_students (number)
tr_year (number)
subjectID(number) Note that in the subjects table there are unique subjectid, this is where I have a field called divisor to indicate a different formula. I was hoping to have the actual formula. Fro now it is a numeric field that I use to do the division as noted below.

The calculated value on the report and form is enroll_students/teacherFTE This works for 90 percent of the records. With the exception of two subjectid, the formula is changed as follows:

(student_enroll/teacherFTE)/5 or some other number. Right now I am doing the following. If there is no change in the formula the divisor is 1. Therefore the formula is (student_enroll/teacherfte)/[divisor]. I am plugging in the oher divisors and this seems to work. however, I anticipate greater complexity in the future so I don't know if this work around is viable in the long term. Two formulas are more involved and have to reference an enrollment field that is located on a sub form off the main form and is also part go query I am using. That is it in a nutshell. If you can get me moving in the right direction I can probably fill in the rest. Many thanks,

Patti
My old pal NeoPa has already pointed you in the right direction and has given the necessary logic.
Mar 11 '08 #9
patriciashoe
41 New Member
Thank you both!! I am on my way.

Patti
Mar 11 '08 #10
NeoPa
32,557 Recognized Expert Moderator MVP
My old pal NeoPa has already pointed you in the right direction and has given the necessary logic.
I suspect I just managed to squeeze in there quickly before you provided something very similar ADezii ;)

@Patti Let us know how you get on with it.
Mar 11 '08 #11
patriciashoe
41 New Member
Gents:

I have coded my formulas and have test them. Can you suggest way that I can code the following:

In my grade table I have a field that would indicate which formula is to be used. On my form and my reports I would like to use this field to programatically set up the function. For example, for gradeid 7 the formula would be "MAIN". When the report runs I need to have the calculated value reflect that I am using the "MAIN" formula. I have about 144 gradeid records.Currently this is the code I have in the report for this calculated value: THis is using my old method.

Expand|Select|Wrap|Line Numbers
  1. =IIf([student_enroll]=0,0,([student_enroll]/[teacherfte])/!staffingDivisor)
Thanks for all the help. This project is just about completed.

Patti
Mar 14 '08 #12
NeoPa
32,557 Recognized Expert Moderator MVP
You've said you want to use a particular field to determine which algorithm to use, but you haven't left any clues as to the logic behind it, or even what values are likely to be found in that field (I'm assuming the field is called [GradeID]).

It could be that you could store the algorithm name in [GradeID]. If so it's quite easy. If not, then we need some indication of what you want it to do. If it's likely to get at all complicated in the future, I'd advise setting up the mapping in a table rather than in code.

Let us know.
Mar 14 '08 #13
patriciashoe
41 New Member
Sorry if I am not being very clear. I guess I am looking for the easiest way to use these formulas in a text box based on a record in the table. If I have a numeric field called formulaid for each gradeid, then how do I indicate what formula should be in the text box calculated value?

table data example

gradeid numberic
gradename text
buildingid
formulaid

For example:this is what I am thinking could work

if the formulaid = 1 then call the function using the first formula in the case statement.

if there is a better way to day this please let me know. Thank again,

Patti
Mar 14 '08 #14
NeoPa
32,557 Recognized Expert Moderator MVP
This is very confusing. Not much of the information you give relates to the other information, so all we have is independent snippets.

To user the Compute() function, we need to have values for each of the four parameters (Formula; Students; TeacherFTE; Divisor).

Let us see if we can't guess some of what's going on and you can tell us if we are on the right lines.

You have a TeacherResources table something like :
Expand|Select|Wrap|Line Numbers
  1. Table = [TeacherResources]
  2. FieldName; Type; Index
  3. BuidlingID; Numeric
  4. TeacherFTE; Numeric
  5. Enroll_Students; Numeric
  6. Tr_Year; Numeric
  7. SubjectID; Numeric; FK
You have a Subjects table something like :
Expand|Select|Wrap|Line Numbers
  1. Table = [Subjects]
  2. FieldName; Type; Index
  3. SubjectID; Numeric; PK
  4. Divisor; Numeric
So far, we have the Students, TeacherFTE and Divisor parameters (as the SubjectID in TeacherResources matches the SubjectID in Subjects).
Now, we have a Grades table, but no discernible way to access it :
Expand|Select|Wrap|Line Numbers
  1. Table = [Grades]
  2. FieldName; Type; Index
  3. GradeID; Numeric; PK
  4. GradeName; String
  5. BuildingID; Numeric
  6. FormulaID; Numeric
When dealing with a TeacherResource item, how can we find the right Grade that goes with it?
Mar 14 '08 #15
NeoPa
32,557 Recognized Expert Moderator MVP
When we have that information, we can look at how we should access the Formula code that the Select statement works from. I would think that it can be stored in the Grades table. Either in place of, or as well as, FormulaID. I can't see any reason for having FormulaID unless it acts as a Foreign Key (FK) into another table for handling formula codes. This may be a good way to do it if there are a large number of them.

Another alternative is to change the code in Compute() to work from the FormulaID numbers instead of the codes. The only reason I suggested strings in the first place is that it's easier to work with and remember if you can assign a string with a meaning, rather than a simple number with no connection to the formula itself.
Mar 14 '08 #16
patriciashoe
41 New Member
Sorry for the confusion. Here are the data structures I am using:
Expand|Select|Wrap|Line Numbers
  1. Table = [TeacherResources]
  2. FieldName; Type; Index
  3. BuidlingID; Numeric
  4. TeacherFTE; Numeric
  5. Enroll_Students; Numeric
  6. Tr_Year; Numeric
  7. SubjectID; Numeric; FK
Expand|Select|Wrap|Line Numbers
  1. Table = [Grades]
  2. FieldName; Type; Index
  3. SubjectID; Numeric; PK
  4. GradeName; String
  5. BuildingID; Numeric
  6. FormulaID; Numeric
  7. Divisor; nummeric
The tables are linked as noted. Your suggestion to use a numeric for the case statement could do the trick. How about something like this

=IIf([student_enroll]=0,0,myfunction([formulaID],[enroll_students],teacherfte]

Thanks again, Patti
Mar 15 '08 #17
NeoPa
32,557 Recognized Expert Moderator MVP
The line would look something like :
Expand|Select|Wrap|Line Numbers
  1. =Compute([FormulaID], [Enroll_Students], [TeacherFTE], [Divisor])
You can call it MyFunction if you prefer of course.
There is no need for an IIf() call. Ensure in the code that division by zero is always avoided.
Mar 16 '08 #18
NeoPa
32,557 Recognized Expert Moderator MVP
I should advise you by the way Patti, that naming can be very important. If you have a PK called [SubjectID] in a [Grades] table then this is likely to confuse people. Not just in a forum like this, but anyone having to deal with the database in a maintenance capacity - including yourself later on.

In a question, it makes it very important for you to make this clear before you start otherwise confusion is almost certain to ensue.

In short, proper naming can be considered unimportant, but never by those with experience.
Mar 16 '08 #19
patriciashoe
41 New Member
Thank you!!! Everything is working just fine. I did take note of your advice about proper naming.

Thanks again, Patti
Mar 17 '08 #20
NeoPa
32,557 Recognized Expert Moderator MVP
I'm really pleased to hear that it's working for you Patti :)

That problem was not a simple one, and is a good illustration of where a bit of lateral thinking can provide real flexibility.
Mar 18 '08 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

3
843
by: Stefanos | last post by:
Hi everybody, I need some help on calculated values in my database design. I'm creating an accounting / business management application with the usual modules like A/R, A/P, Inventory, etc. ...
1
2615
by: Otto Porter | last post by:
/*Given*/ CREATE TABLE ( IDENTITY (1, 1) NOT NULL , NULL , (2) NULL , NULL , CONSTRAINT PRIMARY KEY CLUSTERED (
9
2163
by: Jack | last post by:
Hi, I got a asp form page where the POST ACTION = confirmation.asp page is used to save the values collected from text boxes from the form in a database. For example txtTotalOutlay field is being...
10
2311
by: David Casey | last post by:
I'm working on a program for my C++ class and have it all written and working except for one part. I need to compare two numeric variables to determine decimal accuracy between them. For example:...
1
2933
by: SJH | last post by:
I have been given an older database and asked to make upgrades and what not. One interesting thing I have come across with the database is that it was at one time set up so one of the tables would...
0
2065
by: Henry | last post by:
Hi Is there any good ideas how to update form vb code always after some values are changed / added by vb (not user). I have some code behind subforms vb and there are calculations behind...
1
2168
by: douh | last post by:
I know that this is not the way, however I need to save certian calculated values so that they do not change over time, ie. new tax rates etc. This is for an invoice header and invoice subform. I...
19
2112
by: Skybuck Flying | last post by:
Hi, I think I might have just invented the variable bit cpu :) It works simply like this: Each "data bit" has a "meta data bit". The meta data bit describes if the bit is the ending bit...
11
2322
by: James Hallam | last post by:
I have read through all the past topics and couldn't find what I was after so... I am looking to store some calculated values (don't flame just yet, just read on!). I have an piece of code...
29
4204
by: Martin | last post by:
For reasons I won't go into, I need to transfer from 1 to 3 bytes to a variable that I know is 4 bytes long. Bytes not written to in the 4-byte target variable must be zero. Is the following use of...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.