Using VB code to perform and show a calculation in a form.

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

Guest

I am wondering -- I am trying to come up with a way to have an Access form
have an embedded text box that performs a multiplication function based upon
a few fields from the table upon which the form is based. I.E. if I enter
into the table (via the form) Sold (X) amount of products at (Y) price, how
do I implant a box that will pop up with the calculation "X*Y" to show the
total value of the sale? This calculation does not necessarily have to be a
field in a query or table, but just for reference purposes.

I know there must be a way to achieve this. However, I am pretty
unfamiliar with VB and I have looked through the VB help section, and I'm not
even sure where to start.

Can anyone help me? Maybe outline some basic code for me that would help me
get a start? I've tried using the expression builder in a text box in the
form but I keep getting #NAME? as an error message when I enter the data for
the calculation.

Thanks so much!
 
Robyn,

Here's a simple example of what I think you need. No VBA
is required for this technique.

Create three text boxes with the below names:

txtQty - Contains number of items.

txtPrice - Contains price of items.

txtAmt - Calculates quantity * price.

In the text box named, "txtAmt", enter the below
expression in the "Control Source" property:

=Nz(Forms!YourForm!txtQty, 0) * Nz(Forms!YourForm!
txtPrice, 0)

Above, you will need to replace "YourForm" with the actual
name of your form.

The txtAmt control will automatically calculate the
product of the quantity and price controls or display a
zero if either of the other two fields is empty or
contains a zero. I just tested the above on my own
database and it works fine.
 
Back
Top