Results 1 to 4 of 4
  1. #1
    bakkouz is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2011
    Posts
    25

    Update field value in a table based on the sum (or abstraction) value of other table

    Hi there,
    I'm still a beginner so please take it easy on me

    Ok, So, I have two tables, One is called SpareParts, and the other is called Spare_Parts_Operations.

    Table "SpareParts" basically stores Spare parts names and quantities, Such as:

    Item Quantity EA


    CD-Drive 1 Unit
    Cable 300 m
    Keyboard 5 Unit

    And so on.

    In table "Spare_Parts_Operations" the user (Using a Data Entry Form) stores information about spare parts he installed on machines, Such as:

    Item Serial Number Quantity Installed By Installed To
    Keyboard AB123D 1 John Mick
    HDD C2334G 1 Sarah Jim
    Mouse 67JT5 1 Sarah Peter

    and so on.
    Usually the Quantity field in "Spare_Parts_Operations" is always 1, since he only installes one item at a time with a unique serial number.

    Now, what I want to do is: after the user clicks the "Add" button on the "Spare_Parts_Operations" form, I want the according quantity of the Item in "SpareParts" table to get decreased by 1.

    ie: if i had 10 Keybaords in "SpareParts" and the user/tech installes a new keyboard on a machine, and uses the form "Spare_Parts_Operations" to records this operations, the Quantity of keyboards in "SpareParts" should now become 9.

    I can of course make him update the Quantity field in "SpareParts" table manually, But I want it done automatically to avoid users from messing with the "SpareParts" Table, I've been trying to figure out how to do, even searched the web for answers, it but I honestly came up with nothing.

    help is very much appreciated,
    Thank you.

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by bakkouz View Post
    Now, what I want to do is: after the user clicks the "Add" button on the "Spare_Parts_Operations" form, I want the according quantity of the Item in "SpareParts" table to get decreased by 1.
    run an sql operation on it. It's as simple as that. here is an example of running such a statement using a form control called "qty" that has a value of 1:
    Code:
    currentdb.execute "UPDATE table SET " & _
    "table.qty = table.qty - " & me.qty & " WHERE " & _
    "[id] = " & me.productID
    HTH!

  3. #3
    bakkouz is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2011
    Posts
    25
    ajetrumpet, Thanks for your reply.
    I modified the code like this:

    Private Sub Command51_Click()
    CurrentDb.Execute "UPDATE table SET " & _
    "SpareParts.Quantity = SpareParts.Quantity - " & Me.Quantity & " WHERE " & _
    "[Item] = " & Me.item_ID
    End Sub


    but I keep getting a compile error on Me.Quantity.
    What am i doing wrong?

  4. #4
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    maybe try to use a variable instead:

    Code:
    Private Sub Command51_Click()
    
    dim var as long
    
    var = dlookup("quantity", "spareparts", "[item] = " & me.item_id) - me.quantity
    
    CurrentDb.Execute "UPDATE table SET " & _
    "SpareParts.Quantity = " & var & " WHERE " & _
    "[Item] = " & Me.item_ID
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 11-24-2014, 02:11 PM
  2. Update table based on List Box selection
    By tpcervelo in forum Forms
    Replies: 0
    Last Post: 11-04-2010, 01:32 PM
  3. Update 1 field from a table to another table
    By Mere_Male_1960 in forum Queries
    Replies: 1
    Last Post: 09-28-2010, 09:40 PM
  4. Replies: 4
    Last Post: 09-03-2009, 02:01 PM
  5. update table in loop based on IF statement
    By LALouw in forum Access
    Replies: 0
    Last Post: 07-26-2009, 08:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums