Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting dupe records on an add routine with ID values

Status
Not open for further replies.

sk8er1

Programmer
Jan 2, 2005
229
US
In the following routine, I add a record to the first table and store its ID. I want to add a record to another table but I want to store the ID from the first table into a linking field in the second table.

Whats happening is, the second table is getting 2 records added. 1 has a linkID = 0 then there is another record with the proper value. So in essence, its duping on the
rs.update in the attached code in the function WriteQRRecord.

Can someone shed some light on this,...Thanks much.
I added the code....


'-- add record to first table to link to and works fine

Dim db As Database
Dim rs As Recordset
Dim lngID As Long

Set db = CurrentDb
Set rs = db.OpenRecordset("Inv")
rs.AddNew
rs("Inve") = Me.txtInv
rs("FamNo") = cboFamily.Column(0)
rs("FamName") = Me.cboFamily.Column(1)

lngID = rs("ID")
rs.Update

Set db = Nothing
Set rs = Nothing

'-- add record to the next table

WriteQRRecord (lngID)




Exit_Command33_Click:
Exit Sub

Err_Command33_Click:
MsgBox Err.Description
Resume Exit_Command33_Click

End Sub


Private Sub WriteQRRecord(lngID As Long)

' This routine has duped records


On Error GoTo WriteQRRecord_Err

Dim db As Database
Dim rs As Recordset


Set db = CurrentDb
Set rs = db.OpenRecordset("Questions_Responses")
rs.AddNew
rs("question") = Me.txtQuestion
rs("Response") = Me.txtResponse
rs("link_id") = lngID
rs.Update

Set db = Nothing
Set rs = Nothing

End Sub
 
Is that your full code? Doesn't it error at not finding WriteQRRecord_Err?

I ran this code fine except for the On Error GoTo bit. Are you sure you're not calling the second function a second time from another place?

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244. Basics at
 
Oh, I simply did not include the error code.
I dont think i am calling it from two locations.
I will check, but I doubt it.
You are not getting two records.
 
Yep, code works fine for me too.

Have fun! :eek:)

Alex Middleton
 
Is your default ID = 0? I made mine that too, but there was not a second record. There's no reason from this code alone that there should be. Where are you calling this from? Why do you have it split into to subroutines? Are you in the middle of adding a record to the second table when you push a button to run this or something?

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244. Basics at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top