When copy / pasting cells from Excel into an Access Subform, when no parent record has been created, will result in an error - as expected.
The problem, is that after this error occurs access gets locked in a state where all subsequent data that is entered results in an Operation not supported in transactions error. When you open the table, you can see that the newly added data is not yet in the table - so it appears as if Access is in fact engaged in some sort of transaction.
I have tried hitting 'Save' .. 'Refresh' .. and even adding an AfterInsert event to force a commitTrans() but have had no luck - and Access claims there is no transaction underway
There is no error if the records are entered manually. The problem only seems to occur when pasting records. I'm guessing that Access is creating a transaction to handle the multiple record operations and is not properly recovering from the error.
I can remove the 'Required' flag and it will work - but then you have orphan records. I was thinking that perhaps an After Insert Data Macro could be used to add a new Batch with a default batch Name, and auto-fill the new BatchID into the Items table. I am not certain how that would be done however.
I did try to remove the 'Required' flag and trap for the error using a Before Change Data Macro - but while it cut down on the errors - it still produced the same Operation not supported in transactions error.
I have looked up the error on the Microsoft Knowledgebase, but did not find anything specific to my circumstances. I searched stackoverflow for the error message and found nothing.
I created a new database and was able to replicate the issue.
Steps to Replicate
Set up the Database
- Create a new ACCDB database in Access 2010
- Create a Table called 'Batches', with the following fields:
- BatchID (AutoNumber) (Primary Key)
- BatchName (Text)
- Create a Table called 'Items', with the following fields:
- RecordID (AutoNumber) (Primary Key)
- BatchID (Long Integer)
- Set Required attribute to True
- Data - Text
- Create a Relationship, linking Batches.BatchID to Items.BatchID
- Include all Records from Batches, and matching records from Items
- Enforce Referential Integrity
- Cascade Updates / Deletes
- Create a Form called 'Form'
- Set the Recordsource to Batches
- Add in the BatchID and Batch name Textboxes
- Add in a SubForm/SubReport Control
- Set Source Object to "Table.Items"
- Set Link Master Fields to "BatchID"
- Set Link Child Fields to "BatchID"
- Set "Filter On Empty Master" = Yes
- Create sample data (Using the Form)
- Create a New Record in Batches.
- Set BatchName = "Test"
- Create a New Record in Items.
- Reference the Batch Record.
- Set Data = "Test"
- Create a New Record in Batches.
As you can see, by hand this works fine.
Copy and Paste Data From Excel
- In Excel
- From A1-A10 enter one letter per cell running down: A,B,C,D,E,F,G,H,I,J
- Highlight the cells A1-A10
- Copy (Control+C)
- In Access, using the Form:
- Add a New Batch Record. It should say "(New)" in BatchID TextBox
- Do NOT enter a Batch Name
- In the Sub-Form, click the record selector (*) for the new record to select the entire row. Type Control+V to paste.
- Click OK for "You must enter a value in the 'Data.BatchID' field. error
- This error may repeat. Keep Clicking OK
- If it asks "Do you want to suppress further error messages..." answer Yes
- Click OK for the "Records that Microsoft Access was unable to paste have been inserted into a new table called 'Paste Errors.' notification
- Fill in the Batch Name Textbox with "TestName"
- Try to gracefully recover. Hit Escape. Change the Record.
At this point - you should see the BatchID entered, the Batch Name, and the New Data. Everything appears to be working as expected. If you try to refresh or navigate to another batch record - you will get the error Operation not supported in transactions. Access will continue to display this error message until we close and reopen the form. The data you pasted will not have made it into the database.
Normally someone with a bit of tech savvy will realize something isn't going well and close out of the database and re-open ... but unfortunately I have users that play "whack-a-mole" with any popup boxes and then try to continue on - so I'd like to make things as bulletproof as possible.
Desired Solution
I'd like a workaround to the problem, that won't eventually lead to other quirks with access, duplicate values, etc.
In my own experience, using VBA to 'fix-up' keys isn't reliable. Data macros seem to be a lot more reliable - but they can be tricky to set up - they aren't very mainstream yet (I'd say there should be a ms-access-data-macros tag on stackoverflow but there isn't yet)
.accdb
file. When I paste I get the "You must enter a value in the 'Items.BatchID' field." message. After I dismiss it the subform is still dirty but anything I do after that simply re-displays that message until I hit [Esc] to undo the subform changes. Then things are back to normal. I tried it on both 32-bit and 64-bit Access 2010 just to see if it made any difference (it didn't). – Gord Thompson*
) for the new record to select the entire row, then type [Ctrl-V] to paste." (Previously I had just clicked in theData
field on the new record and then pasted because that's the field into which I was going to be pasting the data.) – Gord Thompson