473,396 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Evaluating Complex Recordset Field Statements

I'm playing with a generic routine to export data, the concept is to have a
list of data driven templates defining what fields to output.

Evaluating a simple variable field name in the function is easily achieved
by something like

dim rst as dao.recordset
....
strField = "Amount"
debug.print rst.fields(strField) ' or just rst(strField)

What I'm interested in is seeing if there is a way to evaluate a complex
statement such as

strField = "Amount * Tax"
or even embedding a formula like strField = "format([MyDate],"dd-mmm")"
rst(strField) doesn't work because there is no field explicitly called
[Amount * Tax]

I've tried a few variants like
strField = "=[Amount * Tax]"
strField = "rst("Amount") * rst("Tax")" but it doesn't know of the
recordset object.

Obviously I can just go and set up the base query for the recordset with the
calculated fields and reference them, but I'm curious if anybody has any
ideas on a code solution.

--
Regards,
Kevin
Nov 13 '05 #1
3 4092
Kevin,

the expressions can be used as part of the rst.sql string varible as
"<expression> as <name>" and referred to by name in the
rst.fields("<name>") expressions. You can construct the rst.sql string
from application-parameters and refer to the fields by their name. Even
expressions using your own public VBA-fnctions using parameters
referring to constants or fields from the database can be inegrated in
the rst.sql string. When redefining existing or already opened
recorsets a rst.requery will be needed to get the results.

Marc

Nov 13 '05 #2
"Kevin Rollo" <kr*******@hotmail.com> wrote in
news:95******************@news-server.bigpond.net.au:
I'm playing with a generic routine to export data, the concept is
to have a list of data driven templates defining what fields to
output.

Evaluating a simple variable field name in the function is easily
achieved by something like

dim rst as dao.recordset
....
strField = "Amount"
debug.print rst.fields(strField) ' or just rst(strField)

What I'm interested in is seeing if there is a way to evaluate a
complex statement such as

strField = "Amount * Tax"
or even embedding a formula like strField =
"format([MyDate],"dd-mmm")" rst(strField) doesn't work because
there is no field explicitly called
[Amount * Tax]

I've tried a few variants like
strField = "=[Amount * Tax]"
strField = "rst("Amount") * rst("Tax")" but it doesn't know
of the
recordset object.

Obviously I can just go and set up the base query for the
recordset with the calculated fields and reference them, but I'm
curious if anybody has any ideas on a code solution.


I see two solutions to this:

1. define your field as:

strField = "format([MyDate],"dd-mmm") As MyDate"

(you'll have to handle the nested quotes, of course; if you're
assigning strField from a value stored in a table, it won't matter,
of course)

2. keep track of how many fields your are adding in your dynamically
constructed recordset, and refer to them by index. If the field
defined above is the 3rd one in the SELECT clause of your recordset,
you could refer to it as:

rst(2)

because it's a zero-based index (rst(0) is the first field, rst(1)
is the second, etc.).

But, again, you'd then have to know how to connect the field to the
index number, and that might be completely impossible in the context
you're talking trying to use it.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #3
Constructing the altered query for the recordset seems to be the best way to
go. It's easy enough to build the data driven from the reference "fields"
table and ensuring the use of unique calc field names that you know have no
chance of already existing in the table keeps it all workable. I did have a
quick go at building a parser to interpret the complex statement, but was
awed at how much coding was involved, and arrived at the assumption that the
query was going to be far more efficient anyway.

--
Regards,
Kevin

"David W. Fenton" <dX********@bway.net.invalid> wrote in message
news:Xn**********************************@216.196. 97.142...
"Kevin Rollo" <kr*******@hotmail.com> wrote in
news:95******************@news-server.bigpond.net.au:
I'm playing with a generic routine to export data, the concept is
to have a list of data driven templates defining what fields to
output.

Evaluating a simple variable field name in the function is easily
achieved by something like

dim rst as dao.recordset
....
strField = "Amount"
debug.print rst.fields(strField) ' or just rst(strField)

What I'm interested in is seeing if there is a way to evaluate a
complex statement such as

strField = "Amount * Tax"
or even embedding a formula like strField =
"format([MyDate],"dd-mmm")" rst(strField) doesn't work because
there is no field explicitly called
[Amount * Tax]

I've tried a few variants like
strField = "=[Amount * Tax]"
strField = "rst("Amount") * rst("Tax")" but it doesn't know
of the
recordset object.

Obviously I can just go and set up the base query for the
recordset with the calculated fields and reference them, but I'm
curious if anybody has any ideas on a code solution.


I see two solutions to this:

1. define your field as:

strField = "format([MyDate],"dd-mmm") As MyDate"

(you'll have to handle the nested quotes, of course; if you're
assigning strField from a value stored in a table, it won't matter,
of course)

2. keep track of how many fields your are adding in your dynamically
constructed recordset, and refer to them by index. If the field
defined above is the 3rd one in the SELECT clause of your recordset,
you could refer to it as:

rst(2)

because it's a zero-based index (rst(0) is the first field, rst(1)
is the second, etc.).

But, again, you'd then have to know how to connect the field to the
index number, and that might be completely impossible in the context
you're talking trying to use it.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #4

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

Similar topics

10
by: Greg Hurlman | last post by:
I've got what I'm sure is a very simple problem. In an ASP page, I am trying to write out 4 fields from a recordset in succession: Response.Write rs("LastName") Response.Write rs("Suffix")...
8
by: dmiller23462 | last post by:
My brain is nuked....Can anybody tell me right off the bat what is wrong with this code? Along with any glaring errors, please let me know the syntax to display a message (Response.Write would be...
15
by: U N Me | last post by:
I have a continuous form that lists records from a query. One of the columns is an amount field, In the footer of the form I place a text box that displays the total (sum) of the amount. The...
0
by: Brad | last post by:
Hi all, I've never come across this one before and am hoping someone may be able to shed some light on a very strange situation. I'm using a DNS less connection and a ADO recordset to retrieve...
2
by: RBohannon | last post by:
I had some help on this one earlier, but I'm still having a bit of trouble. I'm sure it's something simple that I just don't know. I'm using Access2000. I have one table with employee salary...
4
by: orange | last post by:
I've got a database in .txt file similar to this: BookName;Author;Year;ReviewedBy;Rating;Pages Nemesis;Isaac Asimov;1989;13,31,24;good;110 Sense & Sensibility;Jane Austen;1970;45,32;great;120...
14
by: Karl Irvin | last post by:
While adding data to a record set, I use something like rst! = some variable rst! = some variable ...... rst! = some variable The rst! , rst! etc. are manually typed in and it gets...
5
by: ineedahelp | last post by:
Does anyone know why my DLOOKUP evaluates to NULL for every instance of symbol. In the table, some of the values of MARKETPRICE are blank, some have a number, type double and some of the values are...
3
by: Stef Mientki | last post by:
hello, I'm trying to create a high level debugger, based on rpd2. So when the debugger enters a breakpoint, I want to display the values of all variables in the, let's say, 5 lines above the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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
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...
0
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...

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.