Add Record To Linked Table

G

Guest

I am using a form command button and “DoCmd.GoToRecord , , acNewRec†to add
a record to a FRIENDS table. I need to also automatically add a history
record to a (linked) second EVENTS table. I’m using the following code:

DoCmd.GoToRecord , , acNewRec '---- Add New Member Record
Me.txtNewRecord = Me.ID '---- Set New record AutoNUMBER
Me.State = "CT" ' default State -CT
Me.CITY = "New Milford" ' default city -New Milford
Me.Zip = "06776" ' Default zipcode -06776
Me.CActive = 1 ' default Classification -Active

'========================================= Add Event History Record
Dim rs1 As New ADODB.Recordset
rs1.Open "tblEvents", CurrentProject.Connection, adOpenStatic,
adLockOptimistic
rs1.AddNew
rs1!rnFriends = Me.ID
rs1!entDate = Date
rs1!Event = "New Member"
rs1.Update
rs1.Close
Set rs1 = Nothing

I get an error
“You cannot add or change the record because not a related record is
required in FRIENDS tableâ€

I know this is a timing problem because the FRIENDS record has not as yet
been written to the FRIENDS table when the linked EVENTS table record is
being created. I do however have the AutoNumber (Me.ID) of the parent table.


I assume I have to somehow write, the parent FRIENDS record before I can add
the EVENTS history record, but I don’t know how to accomplish this.

Any help would be appreciated.
 
B

Bill

HTH

Dick Hob said:
I am using a form command button and "DoCmd.GoToRecord , , acNewRec" to add
a record to a FRIENDS table. I need to also automatically add a history
record to a (linked) second EVENTS table. I'm using the following code:

DoCmd.GoToRecord , , acNewRec '---- Add New Member Record
Me.txtNewRecord = Me.ID '---- Set New record AutoNUMBER
Me.State = "CT" ' default State -CT
Me.CITY = "New Milford" ' default city -New Milford
Me.Zip = "06776" ' Default zipcode -06776
Me.CActive = 1 ' default Classification -Active
DoCmd.RunCommand acCmdSaveRecord
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top