473,383 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Updating an Inventory Table using a form linked to another table

I am attempting to update a table that keeps an inventory using a form that creates records for for a different table. I'm pretty weak on the programming side, but I have been looking around at other posts and thought I came up with what would work. My goal is when pressing a button, the number entered into the "Amount Distributed" box on my form (that is linked to a distribution table) will be subtracted from the "On Hand" column on my Inventory table for a specific lot number. I have a combo box on my form that selects the lot number from a query that references my Inventory table.
Here is what I have (that does not work...)
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.  
  3. Dim strSQL As String
  4.  
  5. strSQL = "UPDATE Tbl.Inventory SET Inventory.On_Hand = " & Inventory.On_Hand - Me.Amount_Distributed & " WHERE me.Lot_Number = Inventory.Lot_Number "
  6.  
  7. End Sub
Am I in over my head?
Aug 26 '19 #1
2 1418
Seth Schrock
2,965 Expert 2GB
First, lets work on the strSQL variable. I'm guessing based on what I see that you have a table named Inventory that has at least a On_Hand field and a Lot_Number. If this is all correct (exact names are important), then lets continue with the UPDATE query code. Immediately following the UPDATE keyword comes the table name. You have Tbl.Inventory. Again assuming what I said above is true, then this should just be Inventory. Then comes the SET keyword. You are setting Inventory.On_Hand to equal an amount. Because you have ended the quotation, the VBA code is going to try to calculate the Inventory.On_Hand - Me.Amount_Distributed before putting the difference into the SQL string that you are building. Unfortunately, VBA won't know what Inventory.On_Hand means, only the SQL code will when it runs. So you need the Inventory.On_Hand - all inside the quotes and then the Me.Amount_Distributed outside the quotes. In the WHERE clause, you have the order of the information reversed and don't have the quotes correct. All of that said, here is the what (I think) the strSQL variable should look like:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "UPDATE Inventory SET Inventory.On_Hand = Inventory.On_Hand - " & Me.Amount_Distributed & _
  2.                " WHERE Inventory.Lot_Number = " & Me.Lot_Number
Now, all this code has done is assign some text to a string variable. Access doesn't even know that this is a query that needs to be ran. It is just some text just like these posts are just text. We have to tell VBA to run it as a query. Add the following:

Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Set db = CurrentDb
  3.  
  4. db.Execute strSQL, dbFailOnError
Make sure that line 4 is below your strSQL lines. So, something like this all together:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.  
  3. Dim strSQL As String
  4. Dim db As DAO.Database
  5.  
  6. Set db = CurrentDb
  7.  
  8. strSQL = "UPDATE Inventory SET Inventory.On_Hand = Inventory.On_Hand - " & Me.Amount_Distributed & _
  9.          " WHERE Inventory.Lot_Number = " & Me.Lot_Number
  10.  
  11. db.Execute strSQL, dbFailOnError
Now as far as inventory databases go, they are very tricky and require a good amount of knowledge. I'm going to guess that you may be over your head right now, but by no means give up. You should become very fluent with SQL queries. Also, Database Normalization is a very important concept that you should become familiar with and utilize. Unfortunately I don't have a link for proper table design for an inventory database, but basically, you shouldn't be storing an On Hand amount and changing it every time you distribute one. Instead, you should have a transactions table and then use a query to make the calculation for you for a real-time On Hand amount.

Hope this helps. You should also read Before Posting Code. It gives some site rules as well as good troubleshooting techniques that you can use when trying to figure out what the problem is.

Welcome to Bytes!
Aug 26 '19 #2
NeoPa
32,556 Expert Mod 16PB
PhilyWilly03:
Am I in over my head?
If I'm honest it does look very much like it.

Without a view of the whole system it's hard to advise too reliably but applying updates using SQL as you're looking to do is rarely appropriate. Not never, but rare. Let's skip that for now though. The scope of that would require more attention than you should expect from a forum site. You should be looking at education or employing an expert. Neither trivial.

For this question you're looking at creating SQL on the fly using VBA and then executing it from VBA. Check out How to Debug SQL String. It looks like you're very likely to produce SQL that is not what you require. The linked article will hopefully help you to understand the separation between the VBA side of things and the resultant SQL. Your code indicates that may be confusing you a little.

When you have executable SQL that can work you then have to execute it. This is currently missing from your code. You'd need to add this as my example below :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.     Dim strSQL As String
  3.     Dim dbVar As DAO.Database
  4.  
  5.     On Error GoTo ErrorHandler
  6.     strSQL = "UPDATE [tblInventory]" _
  7.            & " SET [On_Hand]=[On_Hand]-" & Me.Amount_Distributed _
  8.            & " WHERE [Lot_Number]=" & Me.Lot_Number
  9.     Set dbVar = CurrentDb()
  10.     Call dbVar.Execute(strSQL, dbFailOnError)
  11.     Exit Sub
  12.  
  13. ErrorHandler:
  14.     'Whatever
  15. End Sub
NB. The code to create strSQL is largely guesswork on my part but probably illustrates where you need to make changes.
Aug 26 '19 #3

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

Similar topics

7
by: Philip Mette | last post by:
I have been searching many postings and I cant seem to find anyone that has this answer so I decided to post. I am using SQL (Transact-SQL).If I have 2 tables with columns...
14
by: sdowney717 | last post by:
Using the the NumId from TitleData, I would like to delete the corresponding row in Bookdata using pure SQL. I want it to delete all rows in bookdata where the Titledata.NumID is a match to...
2
by: Colm O'Hagan | last post by:
Hi there, I having a problem with a database I'm setting up, I would be delighted if someone out there could help. The database I'm setting up is a task register datebase, it will be used to...
2
by: fkealty | last post by:
I'm attempting to make a small user database more efficient. I have an employee table with id numbers, last name first name title and address. Using a combo box I select the last name and have...
6
by: cyoung311 | last post by:
I'm trying to do an automatic update of one table from another through a form. Basically, when a selection is made for an item, in this case a particular workshop, I want to get the associated...
1
by: Dreamerw7 | last post by:
Hi, I know this is probably a dumb question, but here goes: I have 3 tables: REGION REG_ID REGION
1
by: Bo Long | last post by:
I believe the following a valid SQL statement, but MS Access returns with an error "Operation must be an updateable query". Any suggestions would be greatly appreciated! UPDATE FERCPTILoad AS...
2
by: obs | last post by:
Hi all. I have two tables: Pages consists of an id field: PageId, and some other fields NewsTemplate consists of another id field: PageId , and some other fields I want to update PageId in...
3
MattFitzgerald
by: MattFitzgerald | last post by:
My Forms & Tables:- Main form is Frm_LE_List (contains Customer Details) Stored in Tbl_LE_List Which contains subform Frm_VOL_References (Contains Orders known as VOL's) Stored in...
5
by: new2sql | last post by:
Firstly, thanks in advance for any assistance, it is much appriciated. I have two tables, structure as follows: TBL_FANS Column | Example --------------------------------------...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.