Update Recordset Adding Extra row

Hi there,

I have a contacts table which is related to the company table using FK and PK of the company ID in both tables. In other words one company can have many contacts etc. I have code which adda a new contact into the "CustomerContactsMain" table and also adds the company_id with the row as to maintain the appropriate relationship between the tables.

So the code currently adds the record as desired but also seems to be adding an extra row beneath the inserted row in the "CustomerContactsMain" table which has the same FK (company_id) and nothing else.

I need it to add only the row I have committed and not add an extra row. Could this be cause by defaults perhaps? Or is my coding all wrong.

Private Sub cmdSaveNew_Click()
On Error GoTo Err_cmdSaveNew_Click
newRec = Me.NewRecord
If newRec = False Then

    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.GoToRecord , "", acNewRec
    DoCmd.RunCommand acCmdRefresh
    
    ElseIf IsNull(Me.txtFirstName.Value) Then
    MsgBox ("You have not entered a first name!")
    
ElseIf IsNull(Me.txtEmailAddress.Value) Then
    MsgBox ("An e-mail address is required")
Else
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.RunCommand acCmdRefresh
    
    Dim rs6 As DAO.Recordset
    Dim curDb As DAO.Database
   
    Set curDb = CurrentDb
   
 
    Set rs6 = curDb.OpenRecordset("CustomerContactsMain") 'setting rs to reference the CustomerContactsMain table
    rs6.AddNew
    rs6!company_ID.Value = txtIDReplicated.Value
    rs6.Update
    
    Set rs6 = Nothing
    curDb.Close
    newRec = False
    DoCmd.GoToRecord , "", acNewRec
    

    End If
    
    Me.Parent![sfrMain].Form.Requery
    
    
Exit_cmdSaveNew_Click:
    Exit Sub

Err_cmdSaveNew_Click:
    MsgBox Err.Description
    Resume Exit_cmdSaveNew_Click

End Sub

Open in new window

databarracksAsked:
Who is Participating?
 
omgangIT ManagerCommented:
databarracks, take a look at the changes I made and let me know if this is what you want.
OM Gang
SampleExperts.accdb
0
 
omgangIT ManagerCommented:
Your code is adding two new records.  See my comments below
OM Gang

Else
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.RunCommand acCmdRefresh
   
    Dim rs6 As DAO.Recordset
    Dim curDb As DAO.Database
   
    Set curDb = CurrentDb
   
 
    Set rs6 = curDb.OpenRecordset("CustomerContactsMain") 'setting rs to reference the CustomerContactsMain table
    rs6.AddNew     '<---- you add a new record here
    rs6!company_ID.Value = txtIDReplicated.Value
    rs6.Update
   
    Set rs6 = Nothing
    curDb.Close
    newRec = False
    DoCmd.GoToRecord , "", acNewRec     '<---- you add a new record here
   

0
 
databarracksAuthor Commented:
Hi there,

Sorry for the late response, I am trying your code now.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
databarracksAuthor Commented:
I have removed   DoCmd.GoToRecord , "", acNewRec  and it is still doing it?
0
 
databarracksAuthor Commented:
I have the same code for another table and it doesn't do that. It only adds the one record that I have saved. Really out of idea now, the only difference between my other table and this is that this table doesn't have a one to one relationship.
0
 
databarracksAuthor Commented:
If this helps I have attached code included on my form when the user presses the button to Go to new record which may be affecting it perhaps?
Private Sub cmdNewAdmin_Click()
DoCmd.GoToRecord , , acNewRec
newRec = True
End Sub

Open in new window

0
 
omgangIT ManagerCommented:
databarracks, is it possible for you to post a copy of your db?  Make sure it doesn't contain any confidential information or, better yet, remove all data from all tables except for a few representative records in each.
OM Gang
0
 
databarracksAuthor Commented:
One minute almost done giving you a small sample
0
 
databarracksAuthor Commented:
Here you go, please note that the main form is the one call frmAdminMain and you will see exactly what my problem is? Thank you for your help
SampleExperts.accdb
0
 
databarracksAuthor Commented:
Any luck yet?
0
 
omgangIT ManagerCommented:
I'm looking at it now.  The problem occurs when I create a new contact (me).  I set a break point on the first line of executable code (On Error GoTo Err_cmdSaveNew_Click) and then step through the procedure.  The test for new record
newRec = Me.NewRecord
If newRec = False Then
returns True because I am creating a new record, e.g. the record I am entering hasn't been saved yet so it's still new.  So the procedure jumps to the Else statment and proceeds.  The rs6.AddNew statement is what is actually adding the second record you see in your table.  I'm checking on the easiest fix.


Else
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.RunCommand acCmdRefresh
   
    Dim rs6 As DAO.Recordset
    Dim curDb As DAO.Database
   
    Set curDb = CurrentDb
   
 
    Set rs6 = curDb.OpenRecordset("CustomerContactsMain") 'setting rs to reference the CustomerContactsMain table
    rs6.AddNew
    rs6!company_ID.Value = txtIDReplicated.Value
    rs6.Update
   
    Set rs6 = Nothing
    curDb.Close
    newRec = False
    DoCmd.GoToRecord , "", acNewRec

OM Gang
0
 
omgangIT ManagerCommented:
databarracks, isn't that exactly what you want the button to do?  Save the current record and create a new, blank one with the same companyID?  If not, what is your intent?
OM Gang
0
 
databarracksAuthor Commented:
Many thanks for all of your help, if you have a better way of approaching this then by all means please suggest these to me. I really would be delighted if you could help me get past this problem as I have been at it for 3 solid days to no avail.
0
 
databarracksAuthor Commented:
Yes and no. Yes I want it to save the current 'mew record' that I am committing but not insert another one straight away until I tell it to commit.

This will end up looking like this:

Contact1
,
Contact2
,
Contact3
,

This isn't ideal as when running a contacts report for the customer or viewing my contact list pain, you will have to see all the annoying"," everywhere? A contact should have a name in it hence my message box prompting the user to add one before they can save anything.
0
 
omgangIT ManagerCommented:
So, you have a Save and New button and a Save button.  Please tell me what should happen when each of these are clicked.
Thanks,
OM Gang
0
 
databarracksAuthor Commented:
To be honest we can do away with the Save and New as I already have a Add new button. Save button should save the visible record and if the record has a new idea should add the record to the table and add the company_id in the same table.
0
 
omgangIT ManagerCommented:
I guess I'm a little confused.  If the user is creating a new record, for a contact that doesn't already exist in the table, you simply want to save the record.  If the user is editing an existing record you simply want to save the changes.  What do you mean by "if the record has a new idea should add the record to the table...."?  Where can the user select/set the company ID?
OM Gang
0
 
databarracksAuthor Commented:
I haven't seen the database yet but the customer  doesn't choose the company as this would be a subform on the company form so it would pick the ID of the current company  displayed if that makes sense. And what I meant is that if the contact form has a new contactadmin_id then it would know that it is new and add it to the contacts table accordingly.

Please bear with me while I try your sample.
0
 
databarracksAuthor Commented:
That worked perfectly and with much simpler code I must say. Great job and sincerely appreciate all of the time spent working on this.
0
 
omgangIT ManagerCommented:
You're welcome.  Good luck with the rest of your project.
OM Gang
0
 
databarracksAuthor Commented:
very good job
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.