I have a form that displays 10 variables. 6 are already defined, and the other 4 are user defined. There is also an equation field in this form that allows the user to input an equation based on these variables.
So, for example, I have fields named kAir, Tmax, JC, JA, CS, AmbT, VA, VB, VC and VD.
The user may want to calculate
(kAir * Tmax*JC)/(1/JA*AmbT).
I would like to have this result carried out by access, and input into the same table.
So far I have a sub that calls the table with the equation and variables. I then have it move through the records, and calculate the given equation for each record. To start, I have just coded in a generic equation to use on each record.
Currently, Access calculates the value of the first record, and inputs that value for EVERY record- not very helpful.
Below is the code i am using.
--------------------------------------------------------------------------
Sub CalcPSA()
Dim dbs As Database
Dim PSATable As Recordset
Dim JA As Double, JC As Double, CA As Double, Cs As Double, AmbT As Double, HSnkT As Double, Va As Double, Vb As Double, Vc As Double, Vd As Double, Ve As Double, Vf As Double
Dim PC As Double
Set dbs = CurrentDB
Set PSATable = dbs.OpenRecordset("PSATable", dbOpenDynaset)
PSATable.MoveFirst
JA = PSATable!JA
JC = PSATable!JC
CA = PSATable!CA
Cs = PSATable!Cs
AmbT = PSATable!AmbT
HSnkT = PSATable!HSnkT
Va = PSATable!Va
Vb = PSATable!Vb
Vc = PSATable!Vc
Vd = PSATable!Vd
Ve = PSATable!Ve
Vf = PSATable!Vf
Do Until PSATable.EOF
PSATable.Edit
PC = Nz(CalcPSAFunction(JA, JC, CA, Cs, AmbT, HSnkT, Va, Vb, Vc, Vd, Ve, Vf), Null)
PSATable![TjPredicted] = PC
PSATable.Update
PC = 0
PSATable.MoveNext
Loop
PSATable.Close
End Sub
The function is as follows.
--------------------------------------------------------------------------
Function CalcPSAFunction(JA As Double, JC As Double, CA As Double, Cs As Double, AmbT As Double, HSnkT As Double, Va As Double, Vb As Double, Vc As Double, Vd As Double, Ve As Double, Vf As Double) As Double
CalcPSAFunction = (JA / JC + Cs / CA) * 10
End Function
In the future I would like the user to write their own equation for each record and have Access evaluate this.
What am I doing wrong, and why is the first record's equation evaluation being copied to every record?
So, for example, I have fields named kAir, Tmax, JC, JA, CS, AmbT, VA, VB, VC and VD.
The user may want to calculate
(kAir * Tmax*JC)/(1/JA*AmbT).
I would like to have this result carried out by access, and input into the same table.
So far I have a sub that calls the table with the equation and variables. I then have it move through the records, and calculate the given equation for each record. To start, I have just coded in a generic equation to use on each record.
Currently, Access calculates the value of the first record, and inputs that value for EVERY record- not very helpful.
Below is the code i am using.
--------------------------------------------------------------------------
Sub CalcPSA()
Dim dbs As Database
Dim PSATable As Recordset
Dim JA As Double, JC As Double, CA As Double, Cs As Double, AmbT As Double, HSnkT As Double, Va As Double, Vb As Double, Vc As Double, Vd As Double, Ve As Double, Vf As Double
Dim PC As Double
Set dbs = CurrentDB
Set PSATable = dbs.OpenRecordset("PSATable", dbOpenDynaset)
PSATable.MoveFirst
JA = PSATable!JA
JC = PSATable!JC
CA = PSATable!CA
Cs = PSATable!Cs
AmbT = PSATable!AmbT
HSnkT = PSATable!HSnkT
Va = PSATable!Va
Vb = PSATable!Vb
Vc = PSATable!Vc
Vd = PSATable!Vd
Ve = PSATable!Ve
Vf = PSATable!Vf
Do Until PSATable.EOF
PSATable.Edit
PC = Nz(CalcPSAFunction(JA, JC, CA, Cs, AmbT, HSnkT, Va, Vb, Vc, Vd, Ve, Vf), Null)
PSATable![TjPredicted] = PC
PSATable.Update
PC = 0
PSATable.MoveNext
Loop
PSATable.Close
End Sub
The function is as follows.
--------------------------------------------------------------------------
Function CalcPSAFunction(JA As Double, JC As Double, CA As Double, Cs As Double, AmbT As Double, HSnkT As Double, Va As Double, Vb As Double, Vc As Double, Vd As Double, Ve As Double, Vf As Double) As Double
CalcPSAFunction = (JA / JC + Cs / CA) * 10
End Function
In the future I would like the user to write their own equation for each record and have Access evaluate this.
What am I doing wrong, and why is the first record's equation evaluation being copied to every record?