Solved

Copying Record from Continous Subform

Posted on 2011-03-08
7
273 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
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 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…

831 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