I have found the way to copy the record that I would like, but now I am having a violation of the Primary Key constraint. Here is what I am trying to do: We make a product that comes out of our maching into 2000lbs bags and it is givne a number, e.g. 26273. We store and sell it in those bags, but we also can sell it in smaller 50lbs and 25lbs bags. When we convert the bag from 2000lbs to 25lbs the product takes up 80 bags. Only 40 bags can be put onto a pallet, making the product number 26273 take up two pallets. The problem we have is when we store the pallet we scan the barcode of the product and then scan the barcode of the warehouse location, ONE pallet per location, and only ONE location per pallet. If we have two pallets with the same number than we cannot store them in the warehouse. To solve this problem my bosses what the first pallet to be number 26273B1 and the second pallet to be 26273B2 so that the pallets still contain the original number but is slighlty different in order to store them. When the product receives a number it also goes through several tests and that data is part of the record so both of the records still nedd to contain those test results. Whne I try to copy the record and place the B2 onto the number I get a Primary Key Constraint ODBC Failure. I know why I am getting the error, and I don't what to dissable the constraint to allow duplicate records, but I still need to have the ability to create this new record when we convert to 25lbs bags. So my question: Is there any way to copy a record, slighty change the Primary Key while copying it, and still be able to save it without the Primary Key Constraint error occuring. Also the database is in SQL with the interface front-end is in Access 2007. Any help would be greatly apppreciated. Thank you.
I am not sure how you are wanting to copy a record i.e. in a form or programatically.
Note you can have a Primary Key based on more than 1 column so provided the 2 columns together are unique then you will not violate the constraint.
Simply build what is called an append query. You can pull all of the old previous values and substitute a value for any of the columns to be something of your choice.
So, the code + sql would look like:
Dim strSql As String
Dim oldPK As String
Dim newPK As String
oldPK = "26273B1"
newPK = "26273B2"
strSql = "insert into tblPallets (PalletNumber, Description, StockNumber)" & _
"select '" & newPK & "', Description, StockNumber" & _
"from tblPallets where PallentNumber = '" & oldPK & "'"
CurrentDb.Execute strSql, dbFailOnError
Why not store the original 2000 bag with the PK "26273-00-0000". That "00-0000" suffix indicates the original Bag.
For each subdivision into a smaller bag, "one-up" or increment the sequence that is suffixed at the end. You could use "00" for palletts, and "0000" for the bag-sequence number.
Hence "26273-B1-0001" - thru "26273-B1-0040" indicates the Product id that went into Pallett - one - the first 40 bags.