Using a calculated Variable from a function in another function

G

Guest

If I am calculating a variable in one function and I want to use the results
of that variable in another function; how can I accomplish this?

Calculated variable function code

Public Function CID15_Calc()
Dim D150B As Double
D150B = Forms!frmCTFill!txbxQAN15.Value * DLookup(" [Cable_DIA]", "tbl600V",
"[Design] = FORM![txbxCID15]")
End Function

I need to use the resultant of the equation to D150B in another function.
The only difference is that I have 24 of these resultants that need to be
added together within the function,but if I knew how to get one to work the
others would be easy.
 
M

Marshall Barton

Heiko said:
If I am calculating a variable in one function and I want to use the results
of that variable in another function; how can I accomplish this?

Calculated variable function code

Public Function CID15_Calc()
Dim D150B As Double
D150B = Forms!frmCTFill!txbxQAN15.Value * DLookup(" [Cable_DIA]", "tbl600V",
"[Design] = FORM![txbxCID15]")
End Function

I need to use the resultant of the equation to D150B in another function.
The only difference is that I have 24 of these resultants that need to be
added together within the function,but if I knew how to get one to work the
others would be easy.


The result of a function is returned to the function call
through the function's name. This is the primary
distinction between Function and Sub procedures.

For example, if you have a function such as your's, it
should be done this way:

Public Function CID15_Calc()
CID15_Calc = Forms!frmCTFill!txbxQAN15 _
* DLookup("[Cable_DIA]", "tbl600V", _
"[Design] = FORM![txbxCID15]")
End Function

Then in whatever procedure needs the result, the function
would be used this way:

D150B = CID15_Calc()
 
W

Wolfgang Kais

Hello Heiko.
If I am calculating a variable in one function and I want to use the
results of that variable in another function; how can I accomplish this?

Calculated variable function code

Public Function CID15_Calc()
Dim D150B As Double
D150B = Forms!frmCTFill!txbxQAN15.Value * DLookup(" [Cable_DIA]", _
"tbl600V", "[Design] = FORM![txbxCID15]")
End Function

I need to use the resultant of the equation to D150B in another
function. The only difference is that I have 24 of these resultants
that need to be added together within the function, but if I knew
how to get one to work the others would be easy.

You function does not return the value. Change it lime this:
Public Function CID15_Calc() as Double
Dim D150B As Double
D150B = Forms!frmCTFill!txbxQAN15.Value * DLookup(" [Cable_DIA]", _
"tbl600V", "[Design] = FORM![txbxCID15]")
CID15_Calc = D150B
End Function

Now you can use the result of the function for other calculations.
 
G

Guest

Marshall and Wolfgang,

Thanks for the response and it looks like a viable option....but what if I
have more than one forumla within the same function that I want to remember
the value to.

In my case I calculate the D150B which looks up the diameter of the cable
the second variable looks up the aea of the cable...Sameformula other than it
looks up the area. It should be stored in A150B. I guess I coud separate them
into two completely different functions.

Any further suggestions.

Thanks for your help!!!!
--
Heiko K Stugg


Wolfgang Kais said:
Hello Heiko.
If I am calculating a variable in one function and I want to use the
results of that variable in another function; how can I accomplish this?

Calculated variable function code

Public Function CID15_Calc()
Dim D150B As Double
D150B = Forms!frmCTFill!txbxQAN15.Value * DLookup(" [Cable_DIA]", _
"tbl600V", "[Design] = FORM![txbxCID15]")
End Function

I need to use the resultant of the equation to D150B in another
function. The only difference is that I have 24 of these resultants
that need to be added together within the function, but if I knew
how to get one to work the others would be easy.

You function does not return the value. Change it lime this:
Public Function CID15_Calc() as Double
Dim D150B As Double
D150B = Forms!frmCTFill!txbxQAN15.Value * DLookup(" [Cable_DIA]", _
"tbl600V", "[Design] = FORM![txbxCID15]")
CID15_Calc = D150B
End Function

Now you can use the result of the function for other calculations.
 
J

J. Goddard

Hi -

Since the first function uses the value of a control as an argument, you
can generalize it by passing the control reference in the parameter
list, like this:



Public Function Diam_Calc(myControl as Control) as Double
Dim D150B As Double
D150B = MyControl.Value * DLookup(" [Cable_DIA]", _
"tbl600V", "[Design] = " & myControl.value)
Diam_Calc = D150B
End Function

You would call the function like this:

DiamValue = DiamCalc(me![txbxCID15])

with as similar call for area:

AreaValue = AreaCalc(me![txbxCID15])

To sum the 24, just put the 24 calls in one expression:

DiamSum = DiamCalc(me![txbxCID01]) + DiamCalc(me![txbxCID01]) + ...

for each of the 24 controls This assumes they are all on the same form;
if not the references for the controls will change, but the idea is the
same.

John
 
J

J. Goddard

Oops -

Obviously in the sum expression, you reference 24 *different* conttrols:

DiamSum = DiamCalc(me![txbxCID01]) + DiamCalc(me![txbxCID02]) + ...

2:15AM takes its toll!

John


J. Goddard said:
Hi -

Since the first function uses the value of a control as an argument, you
can generalize it by passing the control reference in the parameter
list, like this:



Public Function Diam_Calc(myControl as Control) as Double
Dim D150B As Double
D150B = MyControl.Value * DLookup(" [Cable_DIA]", _
"tbl600V", "[Design] = " & myControl.value)
Diam_Calc = D150B
End Function

You would call the function like this:

DiamValue = DiamCalc(me![txbxCID15])

with as similar call for area:

AreaValue = AreaCalc(me![txbxCID15])

To sum the 24, just put the 24 calls in one expression:

DiamSum = DiamCalc(me![txbxCID01]) + DiamCalc(me![txbxCID01]) + ...

for each of the 24 controls This assumes they are all on the same form;
if not the references for the controls will change, but the idea is the
same.

John



Marshall and Wolfgang,

Thanks for the response and it looks like a viable option....but what
if I have more than one forumla within the same function that I want
to remember the value to.

In my case I calculate the D150B which looks up the diameter of the
cable the second variable looks up the aea of the cable...Sameformula
other than it looks up the area. It should be stored in A150B. I guess
I coud separate them into two completely different functions.

Any further suggestions.

Thanks for your help!!!!
 
G

Guest

Thanks John,

I'll give it a whirl.
--
Heiko K Stugg


J. Goddard said:
Oops -

Obviously in the sum expression, you reference 24 *different* conttrols:

DiamSum = DiamCalc(me![txbxCID01]) + DiamCalc(me![txbxCID02]) + ...

2:15AM takes its toll!

John


J. Goddard said:
Hi -

Since the first function uses the value of a control as an argument, you
can generalize it by passing the control reference in the parameter
list, like this:



Public Function Diam_Calc(myControl as Control) as Double
Dim D150B As Double
D150B = MyControl.Value * DLookup(" [Cable_DIA]", _
"tbl600V", "[Design] = " & myControl.value)
Diam_Calc = D150B
End Function

You would call the function like this:

DiamValue = DiamCalc(me![txbxCID15])

with as similar call for area:

AreaValue = AreaCalc(me![txbxCID15])

To sum the 24, just put the 24 calls in one expression:

DiamSum = DiamCalc(me![txbxCID01]) + DiamCalc(me![txbxCID01]) + ...

for each of the 24 controls This assumes they are all on the same form;
if not the references for the controls will change, but the idea is the
same.

John



Marshall and Wolfgang,

Thanks for the response and it looks like a viable option....but what
if I have more than one forumla within the same function that I want
to remember the value to.

In my case I calculate the D150B which looks up the diameter of the
cable the second variable looks up the aea of the cable...Sameformula
other than it looks up the area. It should be stored in A150B. I guess
I coud separate them into two completely different functions.

Any further suggestions.

Thanks for your help!!!!
 
G

Guest

I wanted to post the entire code to see if there maybe a better way of doing
this and hopefully I can do a better job of explaining this...so here goes.

This is the entire function code.

Public Function CID01_Calc()
On Error Resume Next
Dim CT01 As Integer
'DIM Cabcond01 = 0 CT = Variable
Dim D010A, D010B, D010C, D010D, D010E, D010X As Double
Dim A010A, A010B, A010C, A010D, A010E, A010X As Double
'DIM Cabcond01 = 1 CT = Variable
Dim D011A, D011B, D011C, D011D, D011E, D011X As Double
Dim A011A, A011B, A011C, A011D, A011E, A011X As Double
Dim CABcond01 As Integer
Forms!frmCTFill!Wire01.Value = DLookup(" [UNGR_SIZE]", "tbl600V",
"[Design] = FORM![txbxCID01]")
CT01 = DLookup(" [CTFC]", "tbl600V", "[Design] = FORM![txbxCID01]")
CABcond01 = DLookup(" [CabTypCond]", "tbl600V", "[Design] =
FORM![txbxCID01]")
'Determine Whether Cable Type is Single Conductors or MultiConductors (0
= Multi, 1 = Single)
If CABcond01 = 0 Then '(MultiConductor)
'Determine Cable Type for correct Calculations (0 = 600V Multi, 1 = 600V
Single, 2 = Control Multi, 3 = 5KV Single, 4= 15KV Single)
If CT01 = 0 Then '(600V Multi)
D010A = 0
A010A = Forms!frmCTFill!txbxQan01.Value * DLookup(" [Cable_A]",
"tbl600V", "[Design] = FORM![txbxCID01]")
ElseIf CT01 = 1 Then '(600V Single) 'Sum of the Diameters per NEC
392.9 (A)(1)
D010B = Forms!frmCTFill!txbxQan01.Value * DLookup("
[Cable_DIA]", "tbl600V", "[Design] = FORM![txbxCID01]")
A010B = 0
ElseIf CT01 = 2 Then '(Control Multi)
D010C = 0
A010C = Forms!frmCTFill!txbxQan01.Value * DLookup(" [Cable_A]",
"tbl600V", "[Design] = FORM![txbxCID01]")
ElseIf CT01 = 3 Then '(5KV Single) - Based on Maintained spacing
between cable diameters (NEC 392.13(A)(2))
D010D = Forms!frmCTFill!txbxQan01.Value * DLookup("
[Cable_DIA]", "tbl600V", "[Design] = FORM![txbxCID01]")
A010D = 0
ElseIf CT01 = 4 Then '(15KV Single) - Based on Maintained spacing
between cable diameters (NEC 392.13(A)(2))
D010E = Forms!frmCTFill!txbxQan01.Value * DLookup("
[Cable_DIA]", "tbl600V", "[Design] = FORM![txbxCID01]")
A010E = 0
Else
D010X = 1000
A010X = 1000
End If
Else 'CABCond01 = 1 (Single Conductor)
'Determine Cable Type for correct Calculations (0 = 600V Multi, 1 = 600V
Single, 2 = Control Multi, 3 = 5KV Single, 4= 15KV Single)
If CT01 = 0 Then '(600V Multi) - Not applicable
D011A = 1000
A011A = 1000
ElseIf CT01 = 1 Then '(600V Single - NEC 391.10(A)(4)) - Triplexed
Cables Only
If Forms!frmCTFill!Wire01.Value = "1/0" Or _
Forms!frmCTFill!Wire01.Value = "2/0" Or _
Forms!frmCTFill!Wire01.Value = "3/0" Or _
Forms!frmCTFill!Wire01.Value = "4/0" Then
D011B = Forms!frmCTFill!txbxQan01.Value * DLookup("
[Cable_DIA]", "tbl600V", "[Design] = FORM![txbxCID01]")
A011B = 0
Else '(600V Single - NEC 391.10(A)(3)) - Triplexed Cables Only
D011B = 0
A011B = Forms!frmCTFill!txbxQan01.Value * DLookup("
[Cable_A]", "tbl600V", "[Design] = FORM![txbxCID01]")
End If
ElseIf CT01 = 2 Then '(Control Multi) - Not applicable
D011C = 1000
A011C = 1000
ElseIf CT01 = 3 Then '(5KV Single) - NEC 392.13(B)(3)) - Triplexed
Cables Only
D011D = Forms!frmCTFill!txbxQan01.Value * DLookup("
[Cable_DIA]", "tbl600V", "[Design] = FORM![txbxCID01]")
A011D = 0
ElseIf CT01 = 4 Then '(15KV Single) - NEC 392.13(B)(3)) - Triplexed
Cables Only
D011E = Forms!frmCTFill!txbxQan01.Value * DLookup("
[Cable_DIA]", "tbl600V", "[Design] = FORM![txbxCID01]")
A011E = 0
Else
D011X = 1000
A011X = 1000
End If
End If
Forms!frmCTFill!TD01.Value = (D010A + D010B + D010C + D010D + D010E
+ D010X + D011A + D011B + D011C + D011D + D011E + D011X)
Forms!frmCTFill!TCA01.Value = (A010A + A010B + A010C + A010D + A010E
+ A010X + A011A + A011B + A011C + A011D + A011E + A011X)
Forms!frmCTFill!WPF01.Value = Forms!frmCTFill!txbxQan01.Value *
DLookup(" [Cable_WT]", "tbl600V", "[Design] = FORM![txbxCID01]")
Forms!frmCTFill!TotWt.Value = Forms!frmCTFill!WPF01.Value +
Forms!frmCTFill!WPF02.Value + Forms!frmCTFill!WPF03.Value + Join_
Forms!frmCTFill!WPF04.Value Forms!frmCTFill!WPF05.Value +
Forms!frmCTFill!WPF06.Value + Forms!frmCTFill!WPF07.Value + Join_
Forms!frmCTFill!WPF08.Value Forms!frmCTFill!WPF09.Value +
Forms!frmCTFill!WPF10.Value + Forms!frmCTFill!WPF11.Value + Join_
Forms!frmCTFill!WPF12.Value Forms!frmCTFill!WPF13.Value +
Forms!frmCTFill!WPF14.Value + Forms!frmCTFill!WPF15.Value + Join_
Forms!frmCTFill!WPF16.Value Forms!frmCTFill!WPF17.Value +
Forms!frmCTFill!WPF18.Value + Forms!frmCTFill!WPF19.Value + Join_
Forms!frmCTFill!WPF20.Value Forms!frmCTFill!WPF21.Value +
Forms!frmCTFill!WPF22.Value + Forms!frmCTFill!WPF23.Value + Join_
Forms!frmCTFill!WPF24.Value
End Function

The reason for all the different variable within the function is becasue
based upon the conditions of the cable type [CABcond01]. [Cabcond01] looks at
the cable table and returns either a 0 or 1 bepending up what the user
selected in the form. It then performs different calculations based on the
National Electrical Code requirements. In some cases it needs to get the
diameter in other cases the area and sometimes both based upon the Voltage
class of the cable. The [CT01] variable gets the Cable Tray Fill Condition
[CTFC], again based upon the Cable selected by user from the cable table.
This [CT01] variable basically determines whether or not the cable can be
placed into a particular cable tray. For instance: the National Electrical
Code will not let you put a 600 Volt cable with a 15KV cable....so the [CT01]
will limit the selection. The code executes everything fine; but I need to
return the values of all the variables D010A, A010A, etc to another function
for other calculations.

Would it just be easier to store this information in an invisible field on
the form and then retrieve the information in the other function????

Any suggestions....
--
Heiko K Stugg


Heiko K Stugg said:
If I am calculating a variable in one function and I want to use the results
of that variable in another function; how can I accomplish this?

Calculated variable function code

Public Function CID15_Calc()
Dim D150B As Double
D150B = Forms!frmCTFill!txbxQAN15.Value * DLookup(" [Cable_DIA]", "tbl600V",
"[Design] = FORM![txbxCID15]")
End Function

I need to use the resultant of the equation to D150B in another function.
The only difference is that I have 24 of these resultants that need to be
added together within the function,but if I knew how to get one to work the
others would be easy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top