471,892 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,892 software developers and data experts.

updating different tables from a form based on input

Feeling silly here - after creating a rather complex database that you guys helped with - this task seems simple... yet I am drawing a total blank.

I have a new project - building a database for a salon to keep up with customers and product sales.

Have a sales form which is truly only for inventory control, so
The form data is attached to a table named SalesTracker.
It has the following fields:

TransactionID <PrimaryKey>
SoldTo <uses combo box attached to Customers table, one column>
ItemName <uses combo box attached to Inventory table - 4 columns, ItemID, ItemName, ItemPrice & ItemsOnHand>
ItemsOnHand <pulls ItemsOnHand out of ItemName combo box column 3>
ItemPrice <pulls ItemPrice out of ItemName Combo box column 2>
QuantitySold <User input required>
SaleAmount <Calcuated using cmd button>

When the cmd button for calculate is pressed, I want to do the following:

sum the order using ItemPrice *QuantitySold (achieved this no problem)
update the ItemsOnHand field in the InventoryTable for the specific item by taking ItemsOnHand and subtracting QuantitySold value - got the math part... but drawing a blank when I go to update the Inventory Table for ItemID just sold.

Sorry, I am sure this is probably very simple... but its alluding me!

Dec 27 '15 #1

✓ answered by zmbd

If this is a stored query then you have to reference the form using the construct:
to pull the form's value into the query.

Take a look at my previous post, the code I've inserted there would be ran from within your VBA for the command button's on_click event.

8 945
Ok, so I tried to do an update query - but am having trouble getting data from the form into the query. Using forms!SalesTracker!NewInventory in the query but it keeps asking for that data, and yes the form is open when the query is run. I also tried to run an update query using the SalesTracker Table and the Inventory Table - but it is acting weird. If I have just opened the salestracker form, it works but if I go to another record and sell something else, it doesn't change anything. Then I noticed instead of changing just 1 record, it claims its changing 3 - but there are only 2 records in there?

here's the code:
Expand|Select|Wrap|Line Numbers
  1. UPDATE Inventory 
  2.    INNER JOIN SalesTracker 
  3.       ON Inventory.ItemID 
  4.          = SalesTracker.ItemID 
  5. SET Inventory.ItemsOnHand 
  6.     = SalesTracker.ItemsOnHand
  7. WHERE (((Inventory.ItemID)
  8.    =[SalesTracker].[ItemID]));
Dec 28 '15 #2
5,501 Expert Mod 4TB
>> Sorry, cross posted with you...
>> Was called away, thought I had pressed submit and had not.
>> Will have to take a closer look at your new post however, I'll leave this as is as it might point in the right direction :)

There are several ways to do this; however, we don't have a full picture of your database design to be able to guide you in the right direction.

You can either open a record-set on the table and change the field holding the quantity


You could run an update-query using either the value you've pre-calculate or you could let the UAQ do the math for you. You can do this using either a stored query or via VBA.

If the CBO_ItemName has the primary key from Inventory table *([ItemID]?) then I would use something like
Expand|Select|Wrap|Line Numbers
  1. 'This is AIR CODE...
  2. zSQL = "UPDATE [tbl_inventroy]" & _
  3.    " SET  [tbl_inventroy]![ItemsOnHand]" & _
  4.         " = [ ItemsOnHand]-" & me.QuantitySold & _
  5.    " WHERE ((([tbl_inventroy]![ItemID])=" & _
  6.     Me.CBO_ItemName & "));"
  7. '
  8. zdb.Execute Query:=zSQL, Options:=dbFailOnError
>> Warning here> There are no user prompts or warning using the DAO Execute method!
>edit> In the WHERE clause, if [tbl_inventroy]![ItemID] is a string/text data, then the value returned by Me.CBO_ItemName will have to match those (which should be handled by the rowsource) and the returned value enclosed in quotes as:
Expand|Select|Wrap|Line Numbers
  1. " WHERE ((([tbl_inventroy]![ItemID])='" & _
  2.     Me.CBO_ItemName & "'));"
Dec 28 '15 #3
5,501 Expert Mod 4TB
If this is a stored query then you have to reference the form using the construct:
to pull the form's value into the query.

Take a look at my previous post, the code I've inserted there would be ran from within your VBA for the command button's on_click event.
Dec 28 '15 #4
This is how I have it formatted and it's not working. Keep getting a popup like the field doesn't exist.

gonna try the code and see if i have better results.
Jan 2 '16 #5
5,501 Expert Mod 4TB
1) Was the form open?
2) Double check the spelling for the control name and form as this is the most common issue.
3) if (2) doesn't solve the issue, then please post the script you are actually using, the post you've made doesn't show any reference to a form control.
Jan 2 '16 #6
32,470 Expert Mod 16PB
Please pay close attention to what ZMBD is saying here. If you can master how best to communicate with others here - What information to include; What things to check for when things go wrong; How to proceed generally when things aren't working as expected; Etc. - you will have learned a lot more than simply how to fix this one problem. ZMBD's advice will lead you towards an approach that will stand you in very good stead going forwards. Precision and care related to what you do and how you communicate this to others are of supreme importance.
Jan 2 '16 #7
I got it! had a "." instead of "!"

Knew it was simple!!!! lol

Thanks for your help!
Jan 2 '16 #8
5,501 Expert Mod 4TB
as always my pleasure to help... I love the easy fixes!

I've also revised my post#2 as I noticed a flaw in the aircode... just a minor thing and a caveat about string values in the WHERE clause of a SQL, just little things to keep in mind as you move forward with your projects/

and Happy New Year!
Jan 2 '16 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by manning_news | last post: by
reply views Thread by stosh259 | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.