Solved

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

Posted on 2014-04-07
4
1,987 Views
Last Modified: 2014-04-08
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
0
Comment
Question by:juricta
  • 2
4 Comments
 
LVL 12

Expert Comment

by:jkaios
ID: 39984914
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
0
 
LVL 12

Accepted Solution

by:
jkaios earned 350 total points
ID: 39984923
Private Sub CalculateButton_Click()

   Dim rs As DAO.Recordset
   Set rs = CurrentDb.OpenRecordset("Select QTY from Table2 where [Name] = 'Buck'")
   rs.Edit
   rs("QTY") = Me.QTY
   rs.Update

End Sub
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39986647
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.
0
 

Author Closing Comment

by:juricta
ID: 39987437
I have to accept this as the answer as it was the first comment that worked.  Thank you for the help and excellent support
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now