473,387 Members | 1,464 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,387 software developers and data experts.

How to overwrite an old record when using INSERT INTO statement?

79 64KB
My Access DB handles product formulation and specification. In the formula form, many text boxes, such as Cost or ingredient statement, are calculated fields based on each ingredient data in the formula.

Once a formula is complete, it becomes an ingredient. The new ingredient ID and Version in the ingredient table (table name TProduct) is supposed to inherit the formula ID and Version. I built a command button to copy ID, Version, cost, and other information from Formula form to Ingredient form using INSERT INTO statement.

However, I am stuck with one issue. If the record already exists, INSERT INTO statement does not update new information in the existing record. User will not notice that the record in Ingredient Table is not updated.

I don’t know how to modify the code to (1) warn user that same ID and Version exists in Ingredient table; (2) delete the existing ingredient record; (3) create a new record with updated information from formula table.

Below is my current INSERT INTO code. Is there a simple way to handle this? Thanks.

Expand|Select|Wrap|Line Numbers
  1.  Dim dbs As Database
  2. Set dbs = CurrentDb
  3.  
  4. Dim strRID As String
  5. Dim strRver As String
  6. Dim strCost As Currency
  7. Dim strStatement As String
  8.  
  9. strRID = Me.[Txt_ID]
  10. strRver = Me.[txt_Version]
  11. strCost = Me.[txt_Total_Cost]
  12. strStatement = Me.[txt_RecipeStatement]
  13.  
  14. dbs.Execute " INSERT INTO TProduct " _
  15. & "(productID, ProductVersion, UnitCost, spec, vendorID, VendorLocation) VALUES " _
  16. & "('" & strRID & "' , '" & strRver & "', '" & strCost & "', '" & strStatement & "', '10000', 'NYC');"
  17.  
  18. dbs.Close 
Apr 15 '12 #1

✓ answered by mshmyob

Unfortunatley Access does not have something like "INSERT OR IGNORE" like SQLite but what you could do is

1) do a DLOOKUP based on the PK
2) If the DLOOKUP returns NULL (ie: not found) the do step 3 else do step 4
3) INSERT statement
4) UPDATE statement

cheers,

10 8194
mshmyob
904 Expert 512MB
Unfortunatley Access does not have something like "INSERT OR IGNORE" like SQLite but what you could do is

1) do a DLOOKUP based on the PK
2) If the DLOOKUP returns NULL (ie: not found) the do step 3 else do step 4
3) INSERT statement
4) UPDATE statement

cheers,
Apr 15 '12 #2
nico5038
3,080 Expert 2GB
When you replace the existing record with the INSERT record, you could first issue a DELETE * FROM tblX x WHERE EXISTS (select ID from tblY y where x.id = y.id)
This will remove the dupes and the INSERT will add them with the most recent information.
Apr 15 '12 #3
Joe Y
79 64KB
Although Nico5038's method looks easier, I have already got it done using mshmyob's method. Both methods are valuable lesson for my vba journal.

Thanks for both of you.

Joe
Apr 16 '12 #4
NeoPa
32,556 Expert Mod 16PB
Actually, if you use an UPDATE query, it will add any records that don't already exist anyway.
Apr 17 '12 #5
Mariostg
332 100+
@NeoPa
I remember I read something about this that you mentionned somewhere else and indeed that works (I think it was a link with example you gave). But in the SQL statement, you have to indicate a JOIN clause.
Apr 18 '12 #6
NeoPa
32,556 Expert Mod 16PB
A curious comment Mario. I agree 100%, of course. It would indeed be required, but so would various other components of the required SQL. I suppose, because that's almost self-evident, I'm just curious that you'd bring it up.
Apr 18 '12 #7
Mariostg
332 100+
@NeoPa
I just wanted to emphasize the UPDATE point, because before I read that other post of you (I am too lazy to find it again, or maybe you have to many posts lol) it never occured to me an UPDATE statement could be used to also INSERT provided the proper syntax of course.
Apr 18 '12 #8
NeoPa
32,556 Expert Mod 16PB
Interesting point Mario. Like yourself, I had no idea about that for many years. I'd been working in Access (and SQL generally) and bemoaning the absence of such a facility. It was only while researching something for someone on here that I happened to see a reference to it and so I checked to find that it did, indeed, handle the add/amend situation perfectly.

PS. I couldn't find where I found this information. I have no reference in my database and neither MSDN nor Jet SQL Help (Finding Jet SQL Help) seems to have the explanation that I obviously found somewhere. Frustrating :-(
Apr 18 '12 #9
mshmyob
904 Expert 512MB
It is easy to create an update statement without using the Join clause though.

But your point of using UPDATE to insert a record (in Access) if it does not exist is new to me. Other systems use the INSERT OR IGNORE statement, or the REPLACE statement.

I will have to look up the exact syntax because I wouldn't want it to inadvertantly add a new record if I didn't want one added.

cheers,
Apr 18 '12 #10
NeoPa
32,556 Expert Mod 16PB
Indeed. That was one of the first things I thought about when I came across it originally.
Apr 18 '12 #11

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

Similar topics

2
by: aaj | last post by:
Hi all I have a small but rather annoying problem with continuos forms, and am wondering if anyone can suggest a method of getting over it. The front end is Access 2002 with the BE being SQL...
0
by: Privacy Trap | last post by:
Hi I have a slight problem in trying to set up 2 forms linked to tables. What I want to do is to inform a control on one form(Apps) as to the value of a key of a 'new' record entered via a...
2
by: terpatwork | last post by:
Hi, (1) I have an access form that allows users to enter data, and when they click a button, the OnClick code that I've written uses a SQL INSERT statement to insert the data into the database. I...
10
by: Mike | last post by:
I know this sounds strange but I am at a loss. I am calling a simple funtion that opens a connection to a SQL Server 2000 database and executes an Insert Statement. private void...
0
by: kiki | last post by:
I have two datasets with the same schema,each of which has some datarows.After i merge them into one datset i found that there were some duplicate rows in these rows.then how can i remove the...
6
by: BaWork | last post by:
I have the following to insert a new record: <% .. Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open "DSN=qqqqq;" SQLStmt = "INSERT INTO tbl_qqqqqq (main_cat, cat_fee, fee_amount)...
10
by: MLH | last post by:
Suppose, in a multi-user environment, you have append query SQL in a VBA procedure that looks like INSERT INTO MyTable... and the next line reads MyVar=DMax("","MyTable... You can never be...
0
by: Rote Rote | last post by:
Hi Guys, I have a simple Edit,Update Gridview and i'm using ObjectDatasource with dataset generated and TableAdapters I can do an update no problem. But can't get my delete to work. When i look at...
2
by: Good Guy | last post by:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Customers_Contact". The conflict occurred in database "BreakAway", table "dbo.Contact", column 'ContactID'. The statement has...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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
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...

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.