473,513 Members | 4,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using EVAL() in a Query with a nested Field Reference

Bri
Greetings,

I am using Eval() in a query with only limited success. If the text
within the function contains a reference to a Field I get #ERROR#. I'll
try and explain what I'm trying to do and maybe one of you can either
tell me how to get the Eval() to work or suggest and alternate method.

I have a table called Units:
Fields: Unit, Formula
Sample Data:
"ea", "* 1"
"km", "* [Interval] / 1000"

I have a Query that returns (amongst other fields):
Fields: Interval, Unit, Quantity, Rate, Charge
Charge: Eval([Quantity] & " * " & [Rate] & [Formula])

The "ea" unit works like a charm.
The "km" unit returns #ERROR#

Now I can see that since the [Interval] is in the value of the Formula
Field that I need to do something to get it included in the Eval, but I
can't figure out what.

Any ideas?

Thanks.

--
Bri

Nov 13 '05 #1
8 8601
Br
Bri <no*@here.com> wrote:
Greetings,

I am using Eval() in a query with only limited success. If the text
within the function contains a reference to a Field I get #ERROR#.
I'll try and explain what I'm trying to do and maybe one of you can
either tell me how to get the Eval() to work or suggest and alternate
method.
I have a table called Units:
Fields: Unit, Formula
Sample Data:
"ea", "* 1"
"km", "* [Interval] / 1000"

I have a Query that returns (amongst other fields):
Fields: Interval, Unit, Quantity, Rate, Charge
Charge: Eval([Quantity] & " * " & [Rate] & [Formula])

The "ea" unit works like a charm.
The "km" unit returns #ERROR#

Now I can see that since the [Interval] is in the value of the Formula
Field that I need to do something to get it included in the Eval, but
I can't figure out what.

Any ideas?

Thanks.


I think you'd need to replace the string "[Interval]" with the actual
value?

Is there only two formulae and are the ever likely to change? I'd be
tempted to hardcode them otherwise.

eg. Charge : Iif([Unit] = "ea", [Quantity], [Quantity]/[Interval]*1000)

--
regards,

Bradley

A Christian Response
http://www.pastornet.net.au/response
Nov 13 '05 #2
Bri


Br@dley wrote:
Bri <no*@here.com> wrote:
Greetings,

I am using Eval() in a query with only limited success. If the text
within the function contains a reference to a Field I get #ERROR#.
I'll try and explain what I'm trying to do and maybe one of you can
either tell me how to get the Eval() to work or suggest and alternate
method.
I have a table called Units:
Fields: Unit, Formula
Sample Data:
"ea", "* 1"
"km", "* [Interval] / 1000"

I have a Query that returns (amongst other fields):
Fields: Interval, Unit, Quantity, Rate, Charge
Charge: Eval([Quantity] & " * " & [Rate] & [Formula])

The "ea" unit works like a charm.
The "km" unit returns #ERROR#

Now I can see that since the [Interval] is in the value of the Formula
Field that I need to do something to get it included in the Eval, but
I can't figure out what.

Any ideas?

Thanks.

I think you'd need to replace the string "[Interval]" with the actual
value?

Is there only two formulae and are the ever likely to change? I'd be
tempted to hardcode them otherwise.

eg. Charge : Iif([Unit] = "ea", [Quantity], [Quantity]/[Interval]*1000)


The value for Interval is variable and changes from record to record,
otherwise I would have used the actual value. And these two fomulae are
just one example of a woring one and a non-working one. There are
several others too. Also I wanted the User to be able to add more Units
in the future without having to have me modify the app.

It seems like such a simple thing to do, but the solution eludes me.

--
Bri

Nov 13 '05 #3
Why don't you create a function and use a select case
in the function to calculate your answer.
In your query:
Charge: CalcAnswer([Unit], [Interval], [Quanty], [Rate])

Function CalcAnswer(UM as string, Intvl as integer, Qty as integer _
, Rate as single) as single
CalcAnswer=0
Select Case UM
Case "ea"
CalcAnswer = Qty * Rate * Intvl * 1
Case "km"
CalcAnswer = Qty * Rate * Intvl /1000
Case whatever...and so on
Case Else
" return an answer if there is no match on unit of measure.
CalcAnswer= -1 'or what ever
End Select
EndFunction

This is air code. You will need to set the datatypes correctly.
Also you need to add logic to test for zero or bad data so you
don't cause an invalid answer or perform an invalid calculation.

Ron
Bri wrote:


Br@dley wrote:
Bri <no*@here.com> wrote:
Greetings,

I am using Eval() in a query with only limited success. If the text
within the function contains a reference to a Field I get #ERROR#.
I'll try and explain what I'm trying to do and maybe one of you can
either tell me how to get the Eval() to work or suggest and alternate
method.
I have a table called Units:
Fields: Unit, Formula
Sample Data:
"ea", "* 1"
"km", "* [Interval] / 1000"

I have a Query that returns (amongst other fields):
Fields: Interval, Unit, Quantity, Rate, Charge
Charge: Eval([Quantity] & " * " & [Rate] & [Formula])

The "ea" unit works like a charm.
The "km" unit returns #ERROR#

Now I can see that since the [Interval] is in the value of the Formula
Field that I need to do something to get it included in the Eval, but
I can't figure out what.

Any ideas?

Thanks.


I think you'd need to replace the string "[Interval]" with the actual
value?

Is there only two formulae and are the ever likely to change? I'd be
tempted to hardcode them otherwise.

eg. Charge : Iif([Unit] = "ea", [Quantity], [Quantity]/[Interval]*1000)

The value for Interval is variable and changes from record to record,
otherwise I would have used the actual value. And these two fomulae are
just one example of a woring one and a non-working one. There are
several others too. Also I wanted the User to be able to add more Units
in the future without having to have me modify the app.

It seems like such a simple thing to do, but the solution eludes me.

--
Bri


Nov 13 '05 #4
Br
Ronald Roberts <rw*@robcom.com> wrote:
Why don't you create a function......


Yep, that's probably what I'd have done actually...

<>
--
regards,

Bradley

A Christian Response
http://www.pastornet.net.au/response
Nov 13 '05 #5
Bri

Ronald Roberts wrote:
Why don't you create a function and use a select case
in the function to calculate your answer.
In your query:
Charge: CalcAnswer([Unit], [Interval], [Quanty], [Rate])

Function CalcAnswer(UM as string, Intvl as integer, Qty as integer _
, Rate as single) as single
CalcAnswer=0
Select Case UM
Case "ea"
CalcAnswer = Qty * Rate * Intvl * 1
Case "km"
CalcAnswer = Qty * Rate * Intvl /1000
Case whatever...and so on
Case Else
" return an answer if there is no match on unit of measure.
CalcAnswer= -1 'or what ever
End Select
EndFunction

This is air code. You will need to set the datatypes correctly.
Also you need to add logic to test for zero or bad data so you
don't cause an invalid answer or perform an invalid calculation.


Actually, a function is what I started on after not getting any
solutions to the original idea. Its not quite as simple as your example
though because the Formula is in a Field, so I will need to parse it to
do the actual calculation. The idea is that the calculations will be
data driven so the users can add new ones in the future without my
intervention. If it had been a fixed set of known formulae I would have
just done a Switch() in the query. Oh and there are more fields that
could be involved in the formulae than I've shown here. I tried to
reduce the problem down to just a basic example so that the problem
would be easier for me to explain.

The Eval() solution seemed like a nice simple solution, its too bad it
didn't quite do the trick.

--
Bri

Nov 13 '05 #6
Bri

Ronald Roberts wrote:
Why don't you create a function and use a select case
in the function to calculate your answer.
In your query:
Charge: CalcAnswer([Unit], [Interval], [Quanty], [Rate])

Function CalcAnswer(UM as string, Intvl as integer, Qty as integer _
, Rate as single) as single
CalcAnswer=0
Select Case UM
Case "ea"
CalcAnswer = Qty * Rate * Intvl * 1
Case "km"
CalcAnswer = Qty * Rate * Intvl /1000
Case whatever...and so on
Case Else
" return an answer if there is no match on unit of measure.
CalcAnswer= -1 'or what ever
End Select
EndFunction

This is air code. You will need to set the datatypes correctly.
Also you need to add logic to test for zero or bad data so you
don't cause an invalid answer or perform an invalid calculation.


Actually, a function is what I started on after not getting any
solutions to the original idea. Its not quite as simple as your example
though because the Formula is in a Field, so I will need to parse it to
do the actual calculation. The idea is that the calculations will be
data driven so the users can add new ones in the future without my
intervention. If it had been a fixed set of known formulae I would have
just done a Switch() in the query. Oh and there are more fields that
could be involved in the formulae than I've shown here. I tried to
reduce the problem down to just a basic example so that the problem
would be easier for me to explain.

The Eval() solution seemed like a nice simple solution, its too bad it
didn't quite do the trick.

--
Bri

Nov 13 '05 #7
I understand what you are saying. I have an application were I do
something similar. Over a recordset I needed to calculate answers
based upon selected criteria. Something like, How many males are
within select age groups and what is their race that are classified as
enrolled. I created a table with the field name I wanted to count or
sum, the table name, the Where part of the SQL statement, an operation
field telling me if it were a count or sum operation and an English
description telling the user what this answer represents.

I process this table putting the answer back into the record and also
calculate a percent of the total student count I do this by testing
the operation code and then parsing the Where clause into the Dsum or
Dcount function
Table: Student
Field: StateCode
OP Code: 1=Count, 2 = Sum
English Desc: Native Hawaiian or Other Pacific Islander Male between
16 - 18 years of age Enrolled
SQL Where: (SU_Age Between 16 AND 18) AND UCase(TRIM(SU_gender))= _
'MALE' AND SU_race=6 AND SU_enrolled=True

Then in the logic I do this to get the count.
If IsNull(DCount(cCountField, tFile, Criteria1)) Then
nNum = 0
Else
nNum = DCount(cCountField, tFile, Criteria1)
End If

Then this to get the percent which was calculated before I processed
each record in the table.
If nStudentTotal = 0 Then
nPercent = 0
Else
nPercent = Format(((nNum / nStudentTotal) * 100), "000.0")
End If
I actually have 2 fields of 255 characters for the Where Clause
which I parse together if the second Field is not blank, empty or
null to complete the Where Clause. I currently have some 800
plus records in this table.

The user can then lookup the answer in a subform in datasheet view
or run a report showing all of the records.

I did this in Visual FoxPro 3 which has the ability to do what
you want to do with the Eval command. Later I converted it to Access.

Ron
Bri wrote:

Ronald Roberts wrote:
Why don't you create a function and use a select case
in the function to calculate your answer.
In your query:
Charge: CalcAnswer([Unit], [Interval], [Quanty], [Rate])

Function CalcAnswer(UM as string, Intvl as integer, Qty as integer _
, Rate as single) as single
CalcAnswer=0
Select Case UM
Case "ea"
CalcAnswer = Qty * Rate * Intvl * 1
Case "km"
CalcAnswer = Qty * Rate * Intvl /1000
Case whatever...and so on
Case Else
" return an answer if there is no match on unit of measure.
CalcAnswer= -1 'or what ever
End Select
EndFunction

This is air code. You will need to set the datatypes correctly.
Also you need to add logic to test for zero or bad data so you
don't cause an invalid answer or perform an invalid calculation.

Actually, a function is what I started on after not getting any
solutions to the original idea. Its not quite as simple as your example
though because the Formula is in a Field, so I will need to parse it to
do the actual calculation. The idea is that the calculations will be
data driven so the users can add new ones in the future without my
intervention. If it had been a fixed set of known formulae I would have
just done a Switch() in the query. Oh and there are more fields that
could be involved in the formulae than I've shown here. I tried to
reduce the problem down to just a basic example so that the problem
would be easier for me to explain.

The Eval() solution seemed like a nice simple solution, its too bad it
didn't quite do the trick.

--
Bri


Nov 13 '05 #8
I understand what you are saying. I have an application were I do
something similar. Over a recordset I needed to calculate answers
based upon selected criteria. Something like, How many males are
within select age groups and what is their race that are classified as
enrolled. I created a table with the field name I wanted to count or
sum, the table name, the Where part of the SQL statement, an operation
field telling me if it were a count or sum operation and an English
description telling the user what this answer represents.

I process this table putting the answer back into the record and also
calculate a percent of the total student count I do this by testing
the operation code and then parsing the Where clause into the Dsum or
Dcount function
Table: Student
Field: StateCode
OP Code: 1=Count, 2 = Sum
English Desc: Native Hawaiian or Other Pacific Islander Male between
16 - 18 years of age Enrolled
SQL Where: (SU_Age Between 16 AND 18) AND UCase(TRIM(SU_gender))= _
'MALE' AND SU_race=6 AND SU_enrolled=True

Then in the logic I do this to get the count.
If IsNull(DCount(cCountField, tFile, Criteria1)) Then
nNum = 0
Else
nNum = DCount(cCountField, tFile, Criteria1)
End If

Then this to get the percent which was calculated before I processed
each record in the table.
If nStudentTotal = 0 Then
nPercent = 0
Else
nPercent = Format(((nNum / nStudentTotal) * 100), "000.0")
End If
I actually have 2 fields of 255 characters for the Where Clause
which I parse together if the second Field is not blank, empty or
null to complete the Where Clause. I currently have some 800
plus records in this table.

The user can then lookup the answer in a subform in datasheet view
or run a report showing all of the records.

I did this in Visual FoxPro 3 which has the ability to do what
you want to do with the Eval command. Later I converted it to Access.

Ron
Bri wrote:

Ronald Roberts wrote:
Why don't you create a function and use a select case
in the function to calculate your answer.
In your query:
Charge: CalcAnswer([Unit], [Interval], [Quanty], [Rate])

Function CalcAnswer(UM as string, Intvl as integer, Qty as integer _
, Rate as single) as single
CalcAnswer=0
Select Case UM
Case "ea"
CalcAnswer = Qty * Rate * Intvl * 1
Case "km"
CalcAnswer = Qty * Rate * Intvl /1000
Case whatever...and so on
Case Else
" return an answer if there is no match on unit of measure.
CalcAnswer= -1 'or what ever
End Select
EndFunction

This is air code. You will need to set the datatypes correctly.
Also you need to add logic to test for zero or bad data so you
don't cause an invalid answer or perform an invalid calculation.

Actually, a function is what I started on after not getting any
solutions to the original idea. Its not quite as simple as your example
though because the Formula is in a Field, so I will need to parse it to
do the actual calculation. The idea is that the calculations will be
data driven so the users can add new ones in the future without my
intervention. If it had been a fixed set of known formulae I would have
just done a Switch() in the query. Oh and there are more fields that
could be involved in the formulae than I've shown here. I tried to
reduce the problem down to just a basic example so that the problem
would be easier for me to explain.

The Eval() solution seemed like a nice simple solution, its too bad it
didn't quite do the trick.

--
Bri


Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3494
by: John young | last post by:
I have been looking for an answer to a problem and have found this group and hope you can assist . I have been re doing a data base I have made for a car club I am with and have been trying to make a query that selects from a table as desribed below .. I have a table (Volunteer) that has a member field (memnumber) and a number of fields ...
3
53717
by: GL | last post by:
Hi, Is there a way to add a field to an existing table using a query of some sort (without needing to manually add a field to the table). I know how to do it with a make table query, but I have a specific need to only add a new field to a table if possible. Here's a simplified example of what I'm trying to do: I get a file with the...
2
1833
by: LoopyNZ | last post by:
Hi, (Access 97) I'm creating a query (QRY_SUMMARY) to join a query (QRY) to itself (QRY_1). I'm returning QRY.* and selected fields from QRY_1. With each field (field_name) I return from QRY_1 I'm appending "_1" to to the end to avoid name conflicts with the fields from QRY, i.e. "field_name" becomes "field_name_1".
1
1966
by: Joel Byrd | last post by:
I've been using an eval() statement, which has been working fine until I put it inside of a function. Is this a known problem, and is there a known solution/work-around to this?
8
2165
by: abhishek | last post by:
>>a,b=3,4 7 Now I want to evaluate y by substituting for the evaluated value of x. eval(y) will try to add "a+b" to 3 and return an error. I could do this, 10 but this becomes unwieldy if I have and so on, because the replacements have to be done in exactly the
13
9262
by: Genki | last post by:
Can you figure out how to do this in Access2002/VBA? I want to get the value of an object property. The trick is that the name of the property to retrieve is stored in a table. Here's how I've set it up: I have a class module "objPerson" which has a property "FirstName". This works: objPerson.FirstName = "John Smith" strFirstName =...
7
5045
by: Darko | last post by:
Hello, I have this particular problem with eval() when using Microsoft Internet Explorer, when trying to define an event handler. This is the code: function BigObject() { this.items = new Array(); this.values = new Array();
3
1673
by: Rick Mavrovik | last post by:
Hi, I am using repeater bound to a dataset. Within the repeater I have got ItemTemplate in which I am displaying data by using Eval("DataFieldName"). Does anyone know how can I perform any calculation in ItemTemplate on the bound data..For instance. I need to display an image based on some rule like if Eval("ThisField") is greater than...
5
1957
by: Smiley | last post by:
I'm fooling around with using Eval and trying to manipulate a few things. I ran into a couple of weird results. First of all, in one place I used the following code: $filestring = str_replace("<?", "\n<?\n", $filestring); $filestring = str_replace("?>", "\n?>\n", $filestring); Not a huge thing, just making things easier to read for me. ...
0
7270
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7397
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7563
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7125
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5703
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4757
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3252
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
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 we have to send another system

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.