469,126 Members | 1,232 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,126 developers. It's quick & easy.

updating different tables from a form based on input

49
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!

Sophie
Dec 27 '15 #1

✓ answered by zmbd

If this is a stored query then you have to reference the form using the construct:
[Forms]![OpenFormNameHere]![ControlNameHere]
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 905
sooli
49
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
zmbd
5,400 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 :)


sooli
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

or

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 & "'));"
  3.  
Dec 28 '15 #3
zmbd
5,400 Expert Mod 4TB
If this is a stored query then you have to reference the form using the construct:
[Forms]![OpenFormNameHere]![ControlNameHere]
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
sooli
49
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
zmbd
5,400 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
NeoPa
32,162 Expert Mod 16PB
@Sophie.
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
sooli
49
I got it! had a "." instead of "!"


Knew it was simple!!!! lol

Thanks for your help!
Jan 2 '16 #8
zmbd
5,400 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/

B.O.L.
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
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.