Solved

Copying Record from Continous Subform

Posted on 2011-03-08
7
271 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
  • 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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 500 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now