How to store calculated fields ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all

NB : For me, this question is not about conception and NOT storing calculated fields

In my form, I ask the user to fulfill a few fields, then I calculate other fields from that. I am using this because I then use the Chart/Graphic view from my Table to display some informations

For example, I want to be able to ask for a number, say '17' in a 'enter_number' field, then concatenate it with 'w' in my calculated fields with '="w"&[enter_number]' . I would like to store that field. This here would create a column that would represent the different weeks of a year. That column would be used as an axe on my chart

Putting the formula in the 'ControlSource' property field doesn't update the DB (no link, I guess). Putting it in different 'Event' property fields don't solve my problem. Do I have to write any VBA 'Sub' or use a 'Request' to store that value

Please don't answer me that I shouldn't store calculated fields since my databse will be a small one with no consideration of wasted space :

Thanks in advanc

Nico
 
Nico,

Yes, you will need VBA code to store a calculated field.
Place a control on your form bound to the to-be-calculated
field. In the After Update event of the source field, use
something like:



-----Original Message-----
Hi all,

NB : For me, this question is not about conception and NOT storing calculated fields.

In my form, I ask the user to fulfill a few fields, then
I calculate other fields from that. I am using this
because I then use the Chart/Graphic view from my Table to
display some informations.
For example, I want to be able to ask for a number,
say '17' in a 'enter_number' field, then concatenate it
with 'w' in my calculated fields with '="w"&
[enter_number]' . I would like to store that field. This
here would create a column that would represent the
different weeks of a year. That column would be used as an
axe on my chart.
Putting the formula in the 'ControlSource' property field
doesn't update the DB (no link, I guess). Putting it in
different 'Event' property fields don't solve my problem.
Do I have to write any VBA 'Sub' or use a 'Request' to
store that value ?
Please don't answer me that I shouldn't store calculated
fields since my databse will be a small one with no
consideration of wasted space :)
 
Nico,

Yes, you will need VBA code to store a calculated field.
Place a control on your form bound to the to-be-calculated
field. In the After Update event of the source field, use
something like:



-----Original Message-----
Hi all,

NB : For me, this question is not about conception and NOT storing calculated fields.

In my form, I ask the user to fulfill a few fields, then
I calculate other fields from that. I am using this
because I then use the Chart/Graphic view from my Table to
display some informations.
For example, I want to be able to ask for a number,
say '17' in a 'enter_number' field, then concatenate it
with 'w' in my calculated fields with '="w"&
[enter_number]' . I would like to store that field. This
here would create a column that would represent the
different weeks of a year. That column would be used as an
axe on my chart.
Putting the formula in the 'ControlSource' property field
doesn't update the DB (no link, I guess). Putting it in
different 'Event' property fields don't solve my problem.
Do I have to write any VBA 'Sub' or use a 'Request' to
store that value ?
Please don't answer me that I shouldn't store calculated
fields since my databse will be a small one with no
consideration of wasted space :)
 
Sorry, Nico. I pressed that Tab key once too many. :)

The code should be something like:

[YourCalculatedControl] = "W" + [YourSourceControl]

You should be aware, however, that you can achieve what
you want with no programming (and its inevitable
debugging) in a query. Drag the desired fields to the
grid, and create a new calculated field by typing the name
enclosed by brackets, a colon, and the calculation,
enclosing literals in quotes and fieldnames in brackets,
for example:

[Week]: "W" + [WeekNumber]

You can then base your Chart/Graphic view on the query,
which to Access looks just like a table.

You should also be aware that disk space is the minor
objection of developers to storing calculated fields.
More importantly, even if these don't apply to your
current situation:

1) It's faster to calculate the value on the fly than
look it up from a table.
2) Storing it relies on your programming acumen to ensure
that the data cannot be changed outside your form where
you set the calculated fields through procedures. If the
table is edited directly, these procedures won't be run.
On the other hand, the query will produce the accurate
calculation every time.

HTH
Kevin Sprinkel

-----Original Message-----
Hi all,

NB : For me, this question is not about conception and NOT storing calculated fields.

In my form, I ask the user to fulfill a few fields, then
I calculate other fields from that. I am using this
because I then use the Chart/Graphic view from my Table to
display some informations.
For example, I want to be able to ask for a number,
say '17' in a 'enter_number' field, then concatenate it
with 'w' in my calculated fields with '="w"&
[enter_number]' . I would like to store that field. This
here would create a column that would represent the
different weeks of a year. That column would be used as an
axe on my chart.
Putting the formula in the 'ControlSource' property field
doesn't update the DB (no link, I guess). Putting it in
different 'Event' property fields don't solve my problem.
Do I have to write any VBA 'Sub' or use a 'Request' to
store that value ?
Please don't answer me that I shouldn't store calculated
fields since my databse will be a small one with no
consideration of wasted space :)
 
Hi Kevin,

thanks for answering but the display of the end of your message is missing... :)
 
See above. I accidentally tabbed to the Send button and
hit it twice. I included the rest of the text above.
 
Thanks Kevin

well I guess the example I gave was way too simple :

This time, I want to be able to calculate many fields from a few. For example, I want to be able to fulfill a whole MS Project DB from a few fields. I want to calculate them instead of having MS Project doing it. Well, I know MS Project isn't really complyant with 'Data Importation' :

Many fields have to be fulfilled depending of the values of other. That's why I would use 'Formula' such as 'IIF' (not sure. In French, that would be 'VraiFaux', i.e. 'TrueFalse'. This formula makes a logical test, say compares 2 dates, then have 2 available results depending of the test). Do I have to load a 'Sub' in the 'AfterUpdate Event' of my source control ? What would the syntax / process look like ? I am a little familiar with VBA and Excel, but I am not sure about how to insert a calculated 'value' in a 'Control', using (or not) formulas

By the way, I am thinking of protecting my datas from the users except from the interface. I mean I just want them to access the form, not allowing them to 'edit' the DB. 'DoCmd.Minimize' would only minimize the DB, not 'Hide' it. Is there a way

And eventually, is this possible to use 'Basic' Access as a multiuser interface ? I mean I would drop my application on a shared directory, not using any server application (such as MS Project Server nor one dedicated to Access). I would like to use a 'buffer' DB for preventing error from Updating my DB, and a boolean while writting for preventing from reading, assuming that the 'write' operation would take less than a seconde. Is that possible

Thanks for your help

Nico
 
Beaucoup de questions! :) See below for specific
responses.
-----Original Message-----
Thanks Kevin,

well I guess the example I gave was way too simple :)

This time, I want to be able to calculate many fields
from a few. For example, I want to be able to fulfill a
whole MS Project DB from a few fields. I want to calculate
them instead of having MS Project doing it. Well, I know
MS Project isn't really complyant with 'Data
Importation' :)
Many fields have to be fulfilled depending of the values
of other. That's why I would use 'Formula' such as 'IIF'
(not sure. In French, that would be 'VraiFaux',
i.e. 'TrueFalse'. This formula makes a logical test, say
compares 2 dates, then have 2 available results depending
of the test). Do I have to load a 'Sub' in
the 'AfterUpdate Event' of my source control ? What would
the syntax / process look like ? I am a little familiar
with VBA and Excel, but I am not sure about how to insert
a calculated 'value' in a 'Control', using (or not)
formulas.
========================================================
Firstly, let's tackle the idea of a form "control".
Reports also have controls, which have similar properties,
but for clarity let's restrict the discussion to form
controls. Basically, they are a placeholder for data and
include the following types: textbox, combo box, list
box, option group, checkbox, etc. Most can be either
Bound or Unbound (a label is by definition Unbound). If
they are Bound, then what is entered into the control is
automatically stored in the field to which it is bound.
So include bound controls for all of your calculated
fields on your form. The easiest way to do this is to
drag and drop them from the View, Field List box.

@IIF is a VBA function that evaluates a logical statement,
and returns one of two values, such as:

Gender = @IIF([GenderType]="M", "Male", "Female")

Which assigns the result to a variable named Gender.

They can be nested, although I don't know how many
levels. Beyond a simple case, I prefer one of the other
logical VBA constructs, If.Then or Select Case..

If [GenderType] = "M" Then
Gender = "Male"
Else
Gender = "Female"
EndIf


Select Case [GenderType]
Case "M"
Gender = "Male"
Case "F"
Gender = "Female"
Case Else
Gender = "On ne sait pas!"
End Select

Since they depend on more than one field's value, use the
Form's AfterUpdate event to set your calculated fields.
If you wish to do error-checking on what's been entered,
use the BeforeUpdate event, which is called before writing
the changes to the table.

=========================================================
By the way, I am thinking of protecting my datas from the
users except from the interface. I mean I just want them
to access the form, not allowing them to 'edit' the
DB. 'DoCmd.Minimize' would only minimize the DB,
not 'Hide' it. Is there a way ?=========================================================
This is a complex issue in which I'm not expert. I
suggest you post a message specific to it for the MVP's.
=========================================================
And eventually, is this possible to use 'Basic' Access as
a multiuser interface ? I mean I would drop my application
on a shared directory, not using any server application
(such as MS Project Server nor one dedicated to Access). I
would like to use a 'buffer' DB for preventing error from
Updating my DB, and a boolean while writting for
preventing from reading, assuming that the 'write'
operation would take less than a seconde. Is that
possible ?=========================================================
Access is by nature a multiuser application. For multiple
users to use it, they each must have a valid copy of
Access installed, or you must provide a run-time version
of your application. The latter can only be done if you
own the Developer's version, and will require additional
error-checking programming and toolbar building on your
part. A run-time error not trapped by the programmer will
cause an ungraceful exit (avec deux pieds gauches) from
the application, and all functionality you wish the user
to have must be built from scratch on custom toolbars.

Re: a "buffer DB"-I've always done all error-checking at
the control and/or form level, "forcing" the user to enter
valid data. Again, this is a complex issue; I suggest you
post a question specific to it.

Good luck with your project. A vÔtre service.

Kevin Sprinkel
=========================================================
 
Me again :
One last question...I promise

I don't really get which property controls the value of the bound field I want to be calculated. After the update of the 'Source Control', I should run a 'Sub' that would change the value of the calculated field. If its name is 'calculatedField' bound to a 'Field1' in the table and set in the 'ControlSource' property, would something like ' [calculatedField]=IIF(logical test;value if true;value if wrong) ' or ' [calculatedField].Value=IIF(logical test;value if true;value if wrong) ' work

I guess this is my last question
thanks for keeping helping newbie or not !
 
Don't worry about asking for help; this forum has helped
me and countless others get their projects off the
ground. Returning the favor is my pleasure.

If you were simply *displaying* a calculation in a form
control, you would set the Control Source property to an
expression, such as the following for an [ExtdPrice]
control:

= [Quantity]*[UnitPrice]

This expression will be evaluated, but not stored
anywhere.

Since you wish to store your calculation, you will instead
bind the control to the corresponding table field, and set
the value of control (and hence the underlying field)
through VBA. Since the calculations depend on the value
of more than one other control, this is most conveniently
done in the Form BeforeUpdate or AfterUpdate Event
Procedure as described earlier.

So, in your case, the Control Source will not be a
calculation as above, but instead the name of the field in
which you wish to store the value.

HTH
Kevin Sprinkel

-----Original Message-----
Me again :)
One last question...I promise !

I don't really get which property controls the value of
the bound field I want to be calculated. After the update
of the 'Source Control', I should run a 'Sub' that would
change the value of the calculated field. If its name
is 'calculatedField' bound to a 'Field1' in the table and
set in the 'ControlSource' property, would something
like ' [calculatedField]=IIF(logical test;value if
true;value if wrong) ' or ' [calculatedField].Value=IIF
(logical test;value if true;value if wrong) ' work ?
 
Your Form AfterUpdate event procedure will look something
like this. I've assumed that your textbox controls are
named txt<fieldname> as is a common practice.

Sub Form_AfterUpdate()
Dim Discount As Single

' Selects different cases based on value of a single
' control. Syntax is Select Case <expression>.
' See VBA Help. Press <Ctrl>-G from form design mode
' and choose Help from VBA window.

Select Case Me![txtQty]
Case <20
Discount = 0
Case <50
Discount = .15
Case <100
Discount = .25
Case Else
Discount = .30
End Select

' Assigns the result of a calculation to another form
' control. Since this control is bound to the field
' "CalcFld1", the value will be stored in the table.

Me![txtCalcFld1]=Me![txtQty]*Me![txtPrice]*(1-Discount)

' Assign other controls here...

End Sub

HTH
Kevin Sprinkel
-----Original Message-----
Me again :)
One last question...I promise !

I don't really get which property controls the value of
the bound field I want to be calculated. After the update
of the 'Source Control', I should run a 'Sub' that would
change the value of the calculated field. If its name
is 'calculatedField' bound to a 'Field1' in the table and
set in the 'ControlSource' property, would something
like ' [calculatedField]=IIF(logical test;value if
true;value if wrong) ' or ' [calculatedField].Value=IIF
(logical test;value if true;value if wrong) ' work ?
 
Back
Top