Link to home
Start Free TrialLog in
Avatar of juricta
jurictaFlag for United States of America

asked on

Looking for code to UPDATE tables from ONE form - Access 2010

I have a continuous form that that displays multiple records and each record is its own line.   This form uses Table1 as its datasource. Each line/record has a field called QTY (from Table2)  and NEWQTY.  There is one calculation done in an AFTER UPDATE that takes QTY from Table 2 and subtracts SHOTS USED  from it-this new number is called NEWQTY.  I want to replace QTY in Table2 with the NEWQTY for each record (see attached JPG).  The form can have up to 20 records showing.

In the attached picture:
Name, Code,QTY - come from Table2
QTY - user inputs data
NEWQTY - calculated when CALCULATE button pushed.

Three things need to happen when the user CLICKS the CALCULATE button:
1:Calculate NEWQTY for each record - (I have this)
2:After calculate, replace QTY from Table 2 with NEWQTY for EACH/ALL records. - (need this_
3:ADD ALL current records to a different table - (I have this done)

When the user opens the form again to input data the new values for QTY should show i.e instead of 4 Buck the second user should see 3, the second user should see 15 00 instead of 25.

I need VBA code or code/query to make this happen and be transparent to the user.

Thanks for the help
Form-Data.jpg
Avatar of jkaios
jkaios
Flag of Marshall Islands image

In your CalculateButton_Click() event try this:

dim rs as recordset
set rs = CurrentDb.OpenRecordset("Select QTY from Table2")
rs.Edit
rs("QTY") = myForm.QTY
rs.Update
ASKER CERTIFIED SOLUTION
Avatar of jkaios
jkaios
Flag of Marshall Islands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PatHartman
First the lecture - storing calculated values is dangerous to your health and frequently leads to data errors because the underlying data was changed but the value was not updated.

OK - now that you know you are not supposed to do this, I will tell you how to do it.
Create a query that joins the two tables (you may need a left join) and use that query as the RecordSource for the form.  Pull the NewQty field from table 2.  If you have this column in table 1, you should probably delete it.

In the BeforeUpdate event of the Form, calculate the new value:

Me.NewQty = Me.Qty - Me.ShotsUsed.


If you want the user to see the change as soon as he tabs out of Me.ShotsUsed, then you need to put code into TWO places because you need to make sure the calculation happens if EITHER field changes.

Me.NewQty = Nz(Me.Qty,0) - Nz(Me.ShotsUsed,0)

The Nz() function MUST be used because when you perform this calculation in the AfterUpdate events of the individual controls, either control may be blank and you don't want to generate an error.

Currently you are trying to run this as a batch process and that is the wrong approach to take especially since you are depending on the user to press the Calculate button to make this happen.  What if he doesn't press the calculate button?  Then with your method, whatever rows had a value change now have invalid totals which is EXACTLY why I started with a lecture.  Storing calculated values requires extreme care to ensure that the values are ALWAYS, ALWAYS, ALWAYS updated to reflect the state of their constituent parts.
Avatar of juricta

ASKER

I have to accept this as the answer as it was the first comment that worked.  Thank you for the help and excellent support