?
Solved

Copying Record from Continous Subform

Posted on 2011-03-08
7
Medium Priority
?
277 Views
Last Modified: 2013-11-05
I have a continous subform (see screenshot), when I click the "Create" button I want it to copy all the data on that record and copy into a new table.  This is the code I'm using:

Dim stDocName As String
Dim rs As dao.Recordset

Set rs = CurrentDb.OpenRecordset("tblSE")

rs.AddNew
    rs!intSEID = Me.txtVSENum.Value
    rs!intIncidentID = Me.intIncidentID.Value
    rs!intPeopleID = Me.intPeopleID.Value
    rs!txtFirstName = Me.txtFirstName.Value
    rs!txtMiddleName = Me.txtMiddleName.Value
    rs!txtLastName = Me.txtLastName.Value
rs.Update

rs.Close

As long as no other record on the continous subform has ever called this before... it works.  But if I call it twice (on different records), it won't copy the second time even though it's all new data.  I get this error:

The changes you requested to the table were not successful because they would create duplicate values in the index, primary key or relationship.  Change the data in the field or fields that contain duplicate data, remove the index or redefine the primary to permit duplicate entries.

See sample, I was able to add Complaint #8 no problem, go to create Complaint #5 and I get the error and it doesn't create the copy in the tblSE

I've stepped through the code and know that it's a unique ID I'm trying to pass.

I also removed the primary key on this table to allow duplicates as a test, and I still get this error.

Could it be an issue with me using a continious form?        
ee-subform-.jpg
0
Comment
Question by:gcgcit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 11

Expert Comment

by:donaldmaloney
ID: 35073844
Each time you hit create it looks as if your routine actually resets the recordset to the original input and then tries to rewrite the first record again.
My code to do this is to set a bgookmark when you go into the code and then check the record info.

This is an example that works for my ffiles.


Private Sub EventDelete1_Click()

Me.RecordsetClone.Bookmark = Me.Bookmark

Me.EventDelete = True
Me.Dirty = False
Me.Refresh
Me.Parent![Events By Customer Subform].Form.Recalc

If (MsgBox("do you want to delete this record?", vbYesNo, "Delete Record")) = vbYes Then
        If vbYes = MsgBox("Are you sure you want to delete this record?" & vbCrLf & vbCrLf & _
                          "     Event = " & DLookup("EventName", "Events", "[EventID] = " & EventTypeID) & vbCrLf & _
                          "     Location = " & DLookup("LocationName", "Locations", "[LocationID] = " & Nz(EventLocationID, 0)) & vbCrLf, _
                          vbYesNo + vbDefaultButton2 + vbExclamation, _
                          " Please Confirm") Then
   DoCmd.SetWarnings False
   DoCmd.RunSQL "Delete * From [ContractEvents] Where [ContractID] =  " & Me.[ContractID] & " And [EventTypeID] = " & Me.[EventTypeID] & " And [EventLocationID] = " & Me.[EventLocationID]
   DoCmd.SetWarnings True
   Me.Requery
        Else
        Me.EventDelete = False
        Me.EventDelete1.Requery
        End If
    Else
    Me.EventDelete = False
    Me.EventDelete1.Requery
    End If

End Sub

0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 35074259
Can you upload a sample of your db???

ET
0
 

Author Comment

by:gcgcit
ID: 35083190
donaldmaloney: how do I incorporate your bookmark code into mine?  this is what i have... not sure all the lines i'll need to make it work

Dim stDocName As String
Dim rs As dao.Recordset

Set rs = CurrentDb.OpenRecordset("tblSE")

rs.AddNew
    rs!intSEID = Me.txtVSENum.Value
    rs!intIncidentID = Me.intIncidentID.Value
    rs!intPeopleID = Me.intPeopleID.Value
    rs!txtFirstName = Me.txtFirstName.Value
    rs!txtMiddleName = Me.txtMiddleName.Value
    rs!txtLastName = Me.txtLastName.Value
rs.Update

rs.Close

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gcgcit
ID: 35083229
I tried putting

Me.RecordsetClone.Bookmark = Me.Bookmark

directly after my variable declarations but it's still coming up with error.
0
 
LVL 11

Accepted Solution

by:
donaldmaloney earned 2000 total points
ID: 35083857
qcqcit,
I replicated your code and files and updates to an access DBa nd it updates correctly, even without the bookmark.
But I did recreate the error when attempting to add a second record with the same ID (set as No duplicates).
IS it possible that you have oine of the tblSE fields set as autonumber and you are adding a number that is equal or less than what is in that field in tblSE?

Another way is to put  a stop in the code and step through each line to see what the killer value is to isolate the field creating the  error.
hitting the F8 key will go line by line.
e.g.
Dim rs As dao.Recordset
Stop
Set rs = CurrentDb.OpenRecordset("tblSE")

rs.AddNew

after
0
 

Author Closing Comment

by:gcgcit
ID: 35087156
Your a genius Donald Maloney!!! One of my fields was set to Duplicates No... BY MISTAKE!!! Thank you thank you thank you!!!
0
 
LVL 11

Expert Comment

by:donaldmaloney
ID: 35087188
qcqcit,
Just glad I could help.
but better is that your app works.
Don
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question