473,473 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What's the best way to implement an update/add if not found procedure?

5 New Member
I'm working with a normalised dbase and use a set of temporary files to store information about customers and their orders before committing to the permament versions of the files.
Existing orders can be amended, new orders can be added.
I'm building the routine(s) to copy the temporary files to the permanent omes and wondered what's the best way to compose the order handling routine which needs to update the order if present and add it if not.
I can do this quite easily in VBA, but I've seen many posts which state that SQL is quicker than recordset handling and I would expect, involves less code.
SQL, I'm not so proficient with:-
I'd like to be able to run a query which rattles through the temp orders (header) file and attempts to update related entries on the orders file, if the order number is not found, execute an insert to add it. Ideally, the temp order lines and order lines files could be managed at the same time, rather than separately.
I can post the file designs if necessary, but the solution should be generic as I'm certain I'll be using the technique elsewhere.

Thanks in advance...
Aug 7 '10 #1
9 1571
ADezii
8,834 Recognized Expert Expert
Essentially, two different Queries (UPDATE and APPEND) can perform this task for you using SQL only, with no VBA involved whatsoever.
  1. The first Query (UPDATE) will Join tblTempOrders and tblOders via the Primary Key of tblTempOrders (OrderID) and the Foreign Key of tblOrders (OrderID). This would involve a 1 to MANY Relationship between the Tables, and would return all Orders for which a Temp Order exists. To Update a Field(s) would then be a simple matter. To illustrate this point, and based partially on Tables in the Northwind Database, the following SQL Statement will push ahead all Order Dates in the tblOrders Table for which a corresponding Order exists in tblTempOrders +10 days:
    Expand|Select|Wrap|Line Numbers
    1. UPDATE tbTempOrders INNER JOIN tblOrders ON tbTempOrders.OrderID = tblOrders.OrderID 
    2. SET tblOrders.OrderDate = tblOrders.OrderDate+10
    3. WHERE (((tblOrders.OrderDate) Is Not Null));
  2. The second Query (APPEND), designed a little differently, will find all Orders in tblTempOrders which do not exist in tblOrders based again on the OrderID Field. It will then Append these Records into tblOrders. This Query makes use of what is called a LEFT JOIN.
    Expand|Select|Wrap|Line Numbers
    1. INSERT INTO tblOrders ( CustomerID, OrderDate, RequiredDate, ShippedDate, ShipVia, Freight, OrderID )
    2. SELECT tbTempOrders.CustomerID, tbTempOrders.OrderDate, tbTempOrders.RequiredDate,
    3.  tbTempOrders.ShippedDate, tbTempOrders.ShipVia, tbTempOrders.Freight, tbTempOrders.OrderID
    4. FROM tbTempOrders LEFT JOIN tblOrders ON tbTempOrders.OrderID = tblOrders.OrderID
    5. WHERE (((tblOrders.OrderID) Is Null));
P.S. - I will be the first to tell you that SQL is not my strong point. I'm sure that other Experts/Moderators/Members will chime in and provide you with a better insight into the matter.
Aug 7 '10 #2
Colin Warburton
5 New Member
Ok, thanks for the response ADezii. This takes me in a completely different direction - I'd got locked into testing for record not found before updating/ inserting or thinking about record counts. This looks far easier.
Just so I understand you correctly:
I need to pick a mandatory attribute on the target file and test for not null for the update and null for the insert - the left join ensures that all records on the source file are included, the right side will be null where there isn't a corresponding record.
The only doubt I have is that you referred to a 1 to many relationship between temp orders and orders. It's not quite like that. I'll explain a bit more and let me know if that alters the solution:-
There's a 1 to Many between temp orders and temp order lines
There's a 1 to Many between orders and order lines
There's a 1 to 1 between temp orders and orders
There's a 1 to 1 between temp order lines and order lines
The idea is that order manipulation is done in the temp files and when the user completes, the data is copied (or added if there's new orders) to their 'live' counterparts. The temp files are then cleared down ready for next use.
Aug 7 '10 #3
ADezii
8,834 Recognized Expert Expert
When you say Temp Orders and Orders 'Files', what exactly are you referring to?
Aug 7 '10 #4
Colin Warburton
5 New Member
'Tables' - a consequence of my background :-)
Aug 7 '10 #5
ADezii
8,834 Recognized Expert Expert
A Picture is Worth a Thousand Words, especially in this case. Download the Attachment, based on the Sample Northwind Database, and note the following:
  1. Relationship between tblTempOrders and tblOrders.
  2. qryOrdersAppend - Design View, SQL View, presence of LEFT JOIN, Execute it (3 records should be Appended to tblOrders).
  3. qryOrdersUpdate - Design View, SQL View, Execute it - the Order Date of all Related Records in tblOrders will be pushed 'ahead' 10 Days.
  4. Hope this all helps.
Attached Files
File Type: zip UPDATE_ADD.zip (75.8 KB, 64 views)
Aug 7 '10 #6
Colin Warburton
5 New Member
...and a thousand words are indeed saved. I'm now certain I can go forward with this solution.

Thank you ufor your time.
Aug 7 '10 #7
ADezii
8,834 Recognized Expert Expert
You are quite welcome.
Aug 7 '10 #8
Colin Warburton
5 New Member
Well, many chores, child-handling, child-moaning, pestering and thankfully, putting to bed... later, I finally get back to the important things in life - this dbase app.
I've piloted the two queries in the query designer and (you'll already know this of course), they work. I had to take into account a related table for the append query first.
Now all I have to do is to copy the SQLs into their vba procs and spend the next four years staring at assorted apostrophes, double quotes, ampersands, underscores and commas until they works there too!

Job's a good 'un - consider yourself well and truly thanked!
Aug 7 '10 #9
ADezii
8,834 Recognized Expert Expert
Good luck and keep in touch...
Aug 7 '10 #10

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

Similar topics

17
by: | last post by:
I have an app that retrieves data from an Access database. At the moment I have the SQL string as a Const in my app. I understand this is not best practice. I don't want the user to have access to...
1
by: Rocael Hernandez | last post by:
Hello all, I was wondering what's best for PG, since we have to decide what will be the new server for our DB, that will serve one or more websites. Our actual configuration is a dual xeon...
3
by: martinharvey | last post by:
This is probably a very simple question but i would appreciate some help with the correct syntax for and update stored procedure I have created a user form that allows the user to update the...
1
by: David Shorthouse | last post by:
Hey folks, I am attempting to pass null as the input value from a series of textboxes if the user does not input a value prior to submit. To try and do this, I am using a vbscript function on...
1
by: TerryStone | last post by:
I am writing a Windows application, with an SQL Server (Express) database. The application is single user. The Windows application is the only application that accesses the database. I have...
1
by: objectmodelol | last post by:
I just switched from MS SQL 2000/2005 to MySql. What's wrong with this stored procedure: DELIMITER $$ DROP PROCEDURE IF EXISTS `listing`.`SaveUser` $$ CREATE DEFINER=`root`@`localhost`...
5
by: chandanlinster | last post by:
When I was reading the man-page of fflush, I came across this statement --- "The function fflush forces a write of all user-space buffered data for the given output or update stream via the...
2
by: kbutterly | last post by:
All, I have a menu which contains Category as the master and Product as the child. When I click on a Category in the menu, I want one formView control, fvpc, to show, and then when I click on...
8
by: pamela fluente | last post by:
I need an opinion. I have to add NEW pages to an existing site. The site has - also - some ASPX pages. These pages, I have seen, have the following doctype: <!DOCTYPE html PUBLIC "-//W3C//DTD...
5
by: homayountabrizi | last post by:
What should I add to event procedure to activate FIND command Button in Access 2000. I created a Find (command Button) on a created Form but I can't activate it.
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.