473,406 Members | 2,352 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,406 software developers and data experts.

UPDATE with WHERE conditions reference to fields in other table

79 64KB
In my access database there is a transaction table named “T_Inv_Transaction” that has records of ingredient quantity generated when a Job_ID is created.

The fields in this table related to my question includes followings. There is no primary key assigned to this table.

Job_ID
ProdID
ProdV
BatchQTY
UOM

Each time when user hits a production button, a Job_ID is created by program. BatchQTY and UOM associated to specific Job_ID, ProdID, and ProdV are inserted into this table for tracking purpose.

The next step I need to develop is to update inventory in table “T_Inventory”. Fields in this table include followings with two primary keys: Prod_ID and Prod_V. This inventory table serves as storage place for ingredient quantity and get updated every time when a batch of recipe is produced.

Prod_ID
Prod_V
Qty
UOM

I am stuck with the UPDATE query statement. Is there any way the UPDATE with WHERE condition statement can reference to fields in other table? In this case, update quantity (Qty) in inventory table (with certain math) where Prod_ID and Prod_V equal to fields ProdID and ProdV in transaction table and with specific group of Job_ID?
Nov 8 '17 #1

✓ answered by NeoPa

Actually, it's interesting that SQL is a generic standard, but not all implementations of SQL are the same (or indeed fully standard).

In this case there actually is a problem with using Frinny's SQL in Access as Access uses the Jet SQL engine which has a slightly different syntax when dealing with linked tables within an UPDATE query.

As a general rule you should find the full syntax by following the instructions in Finding Jet SQL Help. In this particular case the linked tables must be defined within the UPDATE clause itself in order to get Access (Jet) to follow the instructions. The Help on this particular feature is poor (It isn't properly covered except for individual tables :-( ) but I was able to find the syntax from some old queries I'd done in the past.

So, Frinny's suggestion, for when using Jet SQL, would be :
Expand|Select|Wrap|Line Numbers
  1. UPDATE T_Inventory 
  2.        INNER JOIN
  3.        T_Inv_Transaction
  4.     ON T_Inv_Transaction.Prod_ID = T_Inventory.ProdID
  5. SET ....
  6. WHERE ...put your condition here
Another restriction within Jet SQL is that updates will only work if the underlying recordset is updatable. That is more restrictive in Jet/Access than in most other flavours of SQL. See Reasons for a Query to be Non-Updatable for more on that.

10 2450
Frinavale
9,735 Expert Mod 8TB
You should be able to accomplish this by joining on the two tables.

Here is a rough example:
Expand|Select|Wrap|Line Numbers
  1. UPDATE T_Inventory
  2. SET ....
  3. FROM T_Inventory JOIN T_Inv_Transaction ON T_Inv_Transaction.Prod_ID = T_Inventory.ProdID 
  4. WHERE ...put your condition here
  5.  
Nov 8 '17 #2
Joe Y
79 64KB
Thanks Frinavale. I will try this approach. I am not a VBA expert so it will take a while to tell results.
Nov 8 '17 #3
NeoPa
32,556 Expert Mod 16PB
That shouldn't be a problem Joe, nor is Frinavale. This is SQL code not VBA. She's great at SQL. Not so much at VBA ;-)
Nov 12 '17 #4
NeoPa
32,556 Expert Mod 16PB
Actually, it's interesting that SQL is a generic standard, but not all implementations of SQL are the same (or indeed fully standard).

In this case there actually is a problem with using Frinny's SQL in Access as Access uses the Jet SQL engine which has a slightly different syntax when dealing with linked tables within an UPDATE query.

As a general rule you should find the full syntax by following the instructions in Finding Jet SQL Help. In this particular case the linked tables must be defined within the UPDATE clause itself in order to get Access (Jet) to follow the instructions. The Help on this particular feature is poor (It isn't properly covered except for individual tables :-( ) but I was able to find the syntax from some old queries I'd done in the past.

So, Frinny's suggestion, for when using Jet SQL, would be :
Expand|Select|Wrap|Line Numbers
  1. UPDATE T_Inventory 
  2.        INNER JOIN
  3.        T_Inv_Transaction
  4.     ON T_Inv_Transaction.Prod_ID = T_Inventory.ProdID
  5. SET ....
  6. WHERE ...put your condition here
Another restriction within Jet SQL is that updates will only work if the underlying recordset is updatable. That is more restrictive in Jet/Access than in most other flavours of SQL. See Reasons for a Query to be Non-Updatable for more on that.
Nov 12 '17 #5
Joe Y
79 64KB
It works! However, I had to put the query in a string and execute the query with DoCmd.RunSQL in order to make it work.

Somehow, when I put the update statement directly in the VBA, Access keep popping up error message asking for “expect end statement”.
Below is the codes that worked for this case.

Thanks to both Frinavale and NeoPa for concept and VBA helps.

Expand|Select|Wrap|Line Numbers
  1. Dim InvSQL As String
  2. Dim strJID As String
  3.  
  4. strJID = Me.txtJob_ID
  5.  
  6. InvSQL = "UPDATE [T_Inventory] INNER JOIN [T_Inv_Transaction] " & _
  7. "ON ([T_Inventory].[Prod_V] = [T_Inv_Transaction].[ProdV]) AND ([T_Inventory].[Prod_ID] = [T_Inv_Transaction].[ProdID]) " & _
  8. "SET [T_Inventory].[Qty] = NZ([T_Inventory].[Qty]) - [T_Inv_Transaction].[BatchQTY], “ & _ 
  9. “[T_Inventory].[UOM] = [T_Inv_Transaction].[UOM] " & _
  10. "WHERE [T_Inv_Transaction].[Job_ID] =" & " '" & strJID & "' "
  11.  
  12. DoCmd.RunSQL InvSQL
Nov 12 '17 #6
Frinavale
9,735 Expert Mod 8TB
Glad it worked out for you!


And this is a reminder of why I tend to shy away from Access questions...the SQL I know and work with daily doesn't apply!

-Frinny
Nov 13 '17 #7
NeoPa
32,556 Expert Mod 16PB
You know you're always welcome to post here Frinny. Someone will generally pick up if there are syntax variations for Jet/ACE.

Joe Y:
It works! However, I had to put the query in a string and execute the query with DoCmd.RunSQL in order to make it work.
Exactly. This is SQL. The VBA interpreter won't handle SQL code as it's SQL. It handles VBA code only.

The normal way of executing SQL from within your VBA code is to prepare a string with the SQL code in it and pass it to either the DoCmd.RunSQL() or {DatabaseObject}.Execute() methods. The former prompts the user depending on your settings. The latter doesn't. A {DatabaseObject} can be retrieved for the current database by running the function CurrentDb().
Nov 13 '17 #8
Joe Y
79 64KB
NeoPa,

You are right. I am evaluating if the prompt from Access of "## record updated, Yes/No" is a good or bad thing to user.

Thanks,
Joe
Nov 13 '17 #9
Joe Y
79 64KB
Frinny,

You guided me to the right direction of using Join. The prompt response and helps is why I like to ask question in this forum.

Much appreciated!
Nov 13 '17 #10
NeoPa
32,556 Expert Mod 16PB
Joe Y:
You are right. I am evaluating if the prompt from Access of "## record updated, Yes/No" is a good or bad thing to user.
Yes. I'm generally right one way or another Joe (j/k).

When using the {DatabaseObject}.Execute() method you have access to that information via the {DatabaseObject}.RecordsAffected property. That then becomes available to you as a value within your VBA code. Display or not display - You have that choice. It won't naturally give the user an opportunity to proceed or turn back though. You could do that with a Transaction of course, but that would be a whole 'nother question.
Nov 13 '17 #11

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

Similar topics

0
by: Sean Utt | last post by:
Table "grps" Column | Type | Modifiers -------------+-----------------------------+-------------------------------- --------------------- grpsid ...
7
by: robert | last post by:
i need to update a column which is a member of the PK on this table. there are some thousands of rows to be updated, many more thousand already in the table. so, i get a constraint violation...
0
by: M Bourgon | last post by:
I am trying to update the contents of table A (I'll use Northwind..Products as an example) with the data from any changed fields in Table B (which is a copy of Northwind..Products, but with some...
2
by: bruno | last post by:
I'm trying to dynamicaly change Where conditions in a Datagrid without success. The aspx form has some input fields, used to build the search, plus a final SEARCH button. In the click event I build...
2
by: ruthboaz | last post by:
Hi, I am exporting a table with data to an excel file.. Some fields are empty in the exported file while the others and the key field, has values. The empty field(s) in excel, will be entered...
6
by: tone | last post by:
Hi, got: ! ! How to overwrite (update) all elements of ! with Max (or latest) of ! ? I tried if with a Update-Querry - without success. Thankx
1
by: SydBlack | last post by:
Hi I'm creating a simple inventory system with the option to print reports for individual accounts or grand total for all accounts. The individual reports works just fine, but for the grand...
4
by: ilikebirds | last post by:
I am trying to update a table that is Inner Joined or related to 2 other tables with similiar data. For Instance: Table 1 ----------- A - B -------- y - x z - null Table 2
8
by: Fluffygoldfish | last post by:
Hi I am trying to tidy up an access database that has traditionally just been used as a spreadsheet. I have split up the information into different tables and need some help transferring...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.