954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Adding record in tables which has relationships

hi.i have problem adding record into access database because these tables i've created has relationships.i need to add record (from a single vb form) into 2 different tables. these 2 tables refer to the same record id actually.but first i need to add record into the first table because then only will the database automatically generate an id for the record.next, i need to add record into another table(which refers to the same id).i got a run-time error that says:

"You cannot add or change record because a related record is required in Table '1' "

how do i add record in a proper manner then,any suggestion?
thanx in advance..

nuBudDy
Light Poster
32 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

so simple.
jusy use the addition code two times.
first add in the master table and then add to the child.

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 

thank you for the reply choudry but how do i implement that,can u explain to me?im currently using two Adodc to refer to both tables.

With Adodc1
.Recordset.AddNew
.........................
..........................
...........................
.Recordset.Update
End With

With Adodc2
.Recordset.AddNew
..............................
.............................
.Recordset.Update


i use these addition codes twice but it gives me an error..please help..thank you in advance..

nuBudDy
Light Poster
32 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

Please follow the steps.

1.Add to master table
2.commit the transaction.
3.Add to Child table and commit.

debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
 

my database consist of 4 tables and all tables are related following the primary and foreign key.i followed the instruction but still stuck at the second table.my codes are:

With Adodc1
.Recordset.AddNew  'add to Master Table
.Recordset.Fields("reserv_date") = booking_date
.Recordset.Fields("fac_id") = fac_type
.Recordset.Fields("cust_name") = Text1.Text
.Recordset.Fields("cust_contact") = Text2.Text
.Recordset.Fields("booking_time") = Text3.Text
.Recordset.Update 'commit the transaction
End With

With Adodc2
If Check8.Value = Checked Then
.Recordset.AddNew 'add to child table
.Recordset.Fields("time_id") = 8
.Recordset.Update 'commit transaction
End If


i did both transaction in a single form.
n i'm getting the same error..
could you please explain more further about how to add n whether the table i've chosen is the right master table based on the relationships i've made?
thanx very much for any help..

nuBudDy
Light Poster
32 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

where are you getting the error?
could it be that
if check8.value = checked
should really be
if check8.value = vbchecked
?????

plusplus
Posting Whiz in Training
207 posts since Jul 2007
Reputation Points: 10
Solved Threads: 16
 

i've made correction on that part
if check8.value = vbChecked but i'm getting the same error which is
run-time error
you cannot add or change record because a related record is required in table ' *** '

With Adodc2
If Check8.Value = Checked Then
.Recordset.AddNew 'add to child table
.Recordset.Fields("time_id") = 8
.Recordset.Update 'the error stops here
End If

any idea??

nuBudDy
Light Poster
32 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

Kindly post all your table structure with relations .

debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
 

hi.attached are my table structure with relations.i have tried buliding query using both tables that i need to use,connect it to adodc and try adding data but it still does not work.
table Reservation is the master table and table TF is the child table.
thank you in advance for the help.

Attachments FRS_DB.zip (45.85KB)
nuBudDy
Light Poster
32 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

i think i might as well give details about my tables.

1.master table(reservation)
fields : reserv_id, reserv_date,cust_name, cust_contact, fac_id.

2.child table(TF)
fields : tf_id,time_id,reserv_id,fac_id,reserv_date.

these 2 tables are related by their reserv_id.

i want to add record into table1(all fields) and table2(time_id fields) in a single click.
but again i receive an error that said "you cannot add or change record because a related record is required in table 'reservation' "

anyone who knows how to solve this please help..
thanx in advance..

nuBudDy
Light Poster
32 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You