PC Review


Reply
Thread Tools Rate Thread

Calculation/expression stored in a field

 
 
stefania nj
Guest
Posts: n/a
 
      29th Oct 2010
Hi,
I do not know if this is doable, but I hope so.
I store the calculation expression that needs to be performed between
texboxes in a form in a table field.
for example
frm.T6 +frm.T8

I would like to grab that expression
and do the following
vCalc=frm.T6 +frm.T8

But I get the string and not the calculation. How can I grab that
expression and actually have the expression work like it was a line of
code?

Thanks for all your help.

Stefania
 
Reply With Quote
 
 
 
 
John W. Vinson
Guest
Posts: n/a
 
      29th Oct 2010
On Fri, 29 Oct 2010 08:54:30 -0700 (PDT), stefania nj <(E-Mail Removed)>
wrote:

>Hi,
>I do not know if this is doable, but I hope so.
>I store the calculation expression that needs to be performed between
>texboxes in a form in a table field.
>for example
>frm.T6 +frm.T8
>
>I would like to grab that expression
>and do the following
>vCalc=frm.T6 +frm.T8
>
>But I get the string and not the calculation. How can I grab that
>expression and actually have the expression work like it was a line of
>code?
>
>Thanks for all your help.
>
>Stefania


Try using the Eval() function: if txtMyExpression contains

"frm.T6 + frm.T8"

then

vCalc = Eval(txtMyExpression)

should do the calculation, if the pieces for it are available.
--

John W. Vinson [MVP]
Microsoft's replacements for these newsgroups:
http://social.msdn.microsoft.com/For...-US/accessdev/
http://social.answers.microsoft.com/.../en-US/addbuz/
and see also http://www.utteraccess.com
 
Reply With Quote
 
stefania nj
Guest
Posts: n/a
 
      29th Oct 2010
On Oct 29, 12:29*pm, John W. Vinson
<jvinson@STOP_SPAM.WysardOfInfo.com> wrote:
> On Fri, 29 Oct 2010 08:54:30 -0700 (PDT), stefania nj <stefani...@gmail.com>
> wrote:
>
>
>
> >Hi,
> >I do not know if this is doable, but I hope so.
> >I store the calculation expression that needs to be performed between
> >texboxes in a form in a table field.
> >for example
> >frm.T6 +frm.T8

>
> >I would like to grab that expression
> >and do the following
> >vCalc=frm.T6 +frm.T8

>
> >But I get the string and not the calculation. How can I *grab that
> >expression and actually have the expression work like it was a line of
> >code?

>
> >Thanks for all your help.

>
> >Stefania

>
> Try using the Eval() function: if txtMyExpression *contains
>
> "frm.T6 + frm.T8"
>
> then
>
> vCalc = Eval(txtMyExpression)
>
> should do the calculation, if the pieces for it are available.
> --
>
> * * * * * * *John W. Vinson [MVP]
> *Microsoft's replacements for these newsgroups:
> *http://social.msdn.microsoft.com/For...-US/accessdev/
> *http://social.answers.microsoft.com/.../en-US/addbuz/
> *and see alsohttp://www.utteraccess.com


John,
Thank you for your reply.
It still does not work. I get a run time error 2482 can't find 'frm'
But if I write the line of code it works perfectly.
Thanks again.
Stefania
 
Reply With Quote
 
stefania nj
Guest
Posts: n/a
 
      29th Oct 2010
On Oct 29, 12:50*pm, stefania nj <stefani...@gmail.com> wrote:
> On Oct 29, 12:29*pm, John W. Vinson
>
>
>
> <jvinson@STOP_SPAM.WysardOfInfo.com> wrote:
> > On Fri, 29 Oct 2010 08:54:30 -0700 (PDT), stefania nj <stefani...@gmail..com>
> > wrote:

>
> > >Hi,
> > >I do not know if this is doable, but I hope so.
> > >I store the calculation expression that needs to be performed between
> > >texboxes in a form in a table field.
> > >for example
> > >frm.T6 +frm.T8

>
> > >I would like to grab that expression
> > >and do the following
> > >vCalc=frm.T6 +frm.T8

>
> > >But I get the string and not the calculation. How can I *grab that
> > >expression and actually have the expression work like it was a line of
> > >code?

>
> > >Thanks for all your help.

>
> > >Stefania

>
> > Try using the Eval() function: if txtMyExpression *contains

>
> > "frm.T6 + frm.T8"

>
> > then

>
> > vCalc = Eval(txtMyExpression)

>
> > should do the calculation, if the pieces for it are available.
> > --

>
> > * * * * * * *John W. Vinson [MVP]
> > *Microsoft's replacements for these newsgroups:
> > *http://social.msdn.microsoft.com/For...-US/accessdev/
> > *http://social.answers.microsoft.com/.../en-US/addbuz/
> > *and see alsohttp://www.utteraccess.com

>
> John,
> Thank you for your reply.
> It still does not work. I get a run time error 2482 can't find 'frm'
> But if I write the line of code it works perfectly.
> Thanks again.
> Stefania

John,
It works if the expression is the following:
Val(forms!frmQAAdmin!T6) +val(forms!frmQAAdmin!T7)

If that is what I need to do that is what I'll do.

Thank you for your assistance.

Stefania
 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      29th Oct 2010
On Fri, 29 Oct 2010 10:01:24 -0700 (PDT), stefania nj <(E-Mail Removed)>
wrote:


>John,
>It works if the expression is the following:
>Val(forms!frmQAAdmin!T6) +val(forms!frmQAAdmin!T7)
>
>If that is what I need to do that is what I'll do.


What's the context? Where are you trying to use this?

A Query will not have any reference to a "frm" object, even if it's been set
in code.

Just the NEED to do this suggests that your table design might not be properly
normalized: what are T6 and T7 and so on? Might you be "storing data in
fieldnames"?
--

John W. Vinson [MVP]
Microsoft's replacements for these newsgroups:
http://social.msdn.microsoft.com/For...-US/accessdev/
http://social.answers.microsoft.com/.../en-US/addbuz/
and see also http://www.utteraccess.com
 
Reply With Quote
 
stefania nj
Guest
Posts: n/a
 
      2nd Nov 2010
On Oct 29, 1:08*pm, John W. Vinson
<jvinson@STOP_SPAM.WysardOfInfo.com> wrote:
> On Fri, 29 Oct 2010 10:01:24 -0700 (PDT),stefanianj <stefani...@gmail.com>
> wrote:
>
> >John,
> >It works if the expression is the following:
> >Val(forms!frmQAAdmin!T6) +val(forms!frmQAAdmin!T7)

>
> >If that is what I need to do that is what I'll do.

>
> What's the context? Where are you trying to use this?
>
> A Query will not have any reference to a "frm" object, even if it's been set
> in code.
>
> Just the NEED to do this suggests that your table design might not be properly
> normalized: what are T6 and T7 and so on? Might you be "storing data in
> fieldnames"?
> --
>
> * * * * * * *John W. Vinson [MVP]
> *Microsoft's replacements for these newsgroups:
> *http://social.msdn.microsoft.com/For...-US/accessdev/
> *http://social.answers.microsoft.com/.../en-US/addbuz/
> *and see alsohttp://www.utteraccess.com


Hi John,
T1, T2 etc. are the names of the textboxes on the form and they are
not names of field on a table. The form is unbound.
I call a function in the enter event of a textbox:
This is the function:
Public Function performCalculation() As Boolean
Dim strCtlname As String
Dim i As Integer
Dim sql As String
Dim vCalc
Dim sngCalc As Long
Dim rst As DAO.Recordset
Dim frm As Form
On Error GoTo Err_performCalculation

Set frm = forms!frmQAAdmin
'get control name and order
strCtlname = frm.ActiveControl.Name
i = Right(strCtlname, Len(strCtlname) - 1)
'check if control has a calculation
sql = "SELECT FIELD_CALCULATION FROM TMP_ADMIN WHERE
FORM_ADMIN_ITEM_ORDER_ID = " & i & ";"
Set rst = CurrentDb.OpenRecordset(sql)
With rst
.MoveFirst
If Not IsNull(!FIELD_CALCULATION) Then


vCalc = Eval(!FIELD_CALCULATION)
frm.ActiveControl.value = vCalc
End If
End With
performCalculation = True
Exit_performCalculation:
performCalculation = False
Exit Function

Err_performCalculation:
Select Case Err.Number
Case 11
MsgBox "Division by zero is not allowed. Please Enter all
correct required amounts for calculation!"
strCtlname = "T" & i - 1
If frm.Controls(strCtlname).Visible = True Then
frm.Controls(strCtlname).SetFocus
Else
strCtlname = "C" & i - 1
frm.Controls(strCtlname).SetFocus
End If
Resume Exit_performCalculation

Case 13
MsgBox "Please Enter all Field required for calculation!"
strCtlname = "T" & i - 1
If frm.Controls(strCtlname).Visible = True Then
frm.Controls(strCtlname).SetFocus
Else
strCtlname = "C" & i - 1
frm.Controls(strCtlname).SetFocus
End If
Resume Exit_performCalculation
Case Else
DoCmd.Echo True
Call LogError(Err.Number, Err.Description,
"performCalculation of Module modQA")
Resume Exit_performCalculation
'MsgBox "Error " & Err.Number & " (" & Err.Description & ")
in procedure performCalculation of Module modQA"
End Select
End Function
 
Reply With Quote
 
stefania nj
Guest
Posts: n/a
 
      2nd Nov 2010
On Oct 29, 1:08*pm, John W. Vinson
<jvinson@STOP_SPAM.WysardOfInfo.com> wrote:
> On Fri, 29 Oct 2010 10:01:24 -0700 (PDT),stefanianj <stefani...@gmail.com>
> wrote:
>
> >John,
> >It works if the expression is the following:
> >Val(forms!frmQAAdmin!T6) +val(forms!frmQAAdmin!T7)

>
> >If that is what I need to do that is what I'll do.

>
> What's the context? Where are you trying to use this?
>
> A Query will not have any reference to a "frm" object, even if it's been set
> in code.
>
> Just the NEED to do this suggests that your table design might not be properly
> normalized: what are T6 and T7 and so on? Might you be "storing data in
> fieldnames"?
> --
>
> * * * * * * *John W. Vinson [MVP]
> *Microsoft's replacements for these newsgroups:
> *http://social.msdn.microsoft.com/For...-US/accessdev/
> *http://social.answers.microsoft.com/.../en-US/addbuz/
> *and see alsohttp://www.utteraccess.com


Hi John,
I am using an unbound form "frmQAAdmin" which has 15 textboxes that
are visible only if available for a particular selection.
With in that selection there could be calculations that I need to
perform using the value in the textboxes

This is the function called on the enter Event of each textbox that is
called:

Public Function performCalculation() As Boolean
Dim strCtlname As String
Dim i As Integer
Dim sql As String
Dim vCalc
Dim sngCalc As Long
Dim rst As DAO.Recordset
Dim frm As Form
On Error GoTo Err_performCalculation

Set frm = forms!frmQAAdmin
'get control name and order
strCtlname = frm.ActiveControl.Name
i = Right(strCtlname, Len(strCtlname) - 1)
'check if control has a calculation
sql = "SELECT FIELD_CALCULATION FROM TMP_ADMIN WHERE
FORM_ADMIN_ITEM_ORDER_ID = " & i & ";"
Set rst = CurrentDb.OpenRecordset(sql)
With rst
if .recordcount>0 then
.MoveFirst
If Not IsNull(!FIELD_CALCULATION) Then


vCalc = Eval(!FIELD_CALCULATION)
frm.ActiveControl.value = vCalc
End If
End if
End With
performCalculation = True
Exit_performCalculation:
performCalculation = False
Exit Function

Err_performCalculation:
Select Case Err.Number
Case 11
MsgBox "Devision by zero is not allowed. Please Enter all
correct required amounts for calculation!"
strCtlname = "T" & i - 1
If frm.Controls(strCtlname).Visible = True Then
frm.Controls(strCtlname).SetFocus
Else
strCtlname = "C" & i - 1
frm.Controls(strCtlname).SetFocus
End If
Resume Exit_performCalculation

Case 13
MsgBox "Please Enter all Field required for calculation!"
strCtlname = "T" & i - 1
If frm.Controls(strCtlname).Visible = True Then
frm.Controls(strCtlname).SetFocus
Else
strCtlname = "C" & i - 1
frm.Controls(strCtlname).SetFocus
End If
Resume Exit_performCalculation
Case Else
DoCmd.Echo True
Call LogError(Err.Number, Err.Description,
"performCalculation of Module modQA")
Resume Exit_performCalculation
'MsgBox "Error " & Err.Number & " (" & Err.Description & ")
in procedure performCalculation of Module modQA"
End Select
End Function
Any way your suggestion of the Eval function saved me.

Thanks again,
Stefania
 
Reply With Quote
 
a a r o n . k e m p f @ g m a i l . c o m
Guest
Posts: n/a
 
      12th Nov 2010
John;

Storing calculated values was a new feature introduced in Access 2010,
right?

ANd it's been a key part of SQL Server for the past 11 years.

No wonder you don't know anything about it.. MVPs are systematically
biased against REAL DATABASES

-Aaron



On Oct 29, 9:08*am, John W. Vinson
<jvinson@STOP_SPAM.WysardOfInfo.com> wrote:
> On Fri, 29 Oct 2010 10:01:24 -0700 (PDT), stefania nj <stefani...@gmail.com>
> wrote:
>
> >John,
> >It works if the expression is the following:
> >Val(forms!frmQAAdmin!T6) +val(forms!frmQAAdmin!T7)

>
> >If that is what I need to do that is what I'll do.

>
> What's the context? Where are you trying to use this?
>
> A Query will not have any reference to a "frm" object, even if it's been set
> in code.
>
> Just the NEED to do this suggests that your table design might not be properly
> normalized: what are T6 and T7 and so on? Might you be "storing data in
> fieldnames"?
> --
>
> * * * * * * *John W. Vinson [MVP]
> *Microsoft's replacements for these newsgroups:
> *http://social.msdn.microsoft.com/For...-US/accessdev/
> *http://social.answers.microsoft.com/.../en-US/addbuz/
> *and see alsohttp://www.utteraccess.com


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculation Expression Help SITCFanTN Microsoft Access Queries 1 11th May 2008 03:31 PM
Calculation Expression Help =?Utf-8?B?a25vd3Nob3dyb3NlZ3Jvd3M=?= Microsoft Access Reports 3 8th Nov 2007 06:31 PM
Calculation Expression =?Utf-8?B?TmVsc29uIFRoZSBNaXNzaW5nIExlYWQ=?= Microsoft Access Queries 6 11th May 2007 05:56 AM
Calculation Expression for Average =?Utf-8?B?TmVsc29uIFRoZSBNaXNzaW5nIExlYWQ=?= Microsoft Access Queries 1 10th May 2007 01:59 PM
calculation expression =?Utf-8?B?Y2FjYWNhY29ubmll?= Microsoft Access Getting Started 2 20th Apr 2007 11:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:59 AM.