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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding new record to a recordset

Status
Not open for further replies.

TryTryAgain

Programmer
Mar 17, 2005
15
US
I have two tables - parent and child - trying to add a new record to the child table. I create a parent record using addnew and update but when I try to add child record I get the message that it can't be added because there isn't a related parent record. When I look at the parent table the record is there....
 
If you're using DAO there shouldn't be anything special that needs to be done if the relationship is setup right. See if your code is similar to this:
Code:
Sub AddCustomerJob()
On Error GoTo ErrHandler
  Dim db      As DAO.Database
  Dim rst     As DAO.Recordset
  Dim lngID   As Long

  Set db = CurrentDb()
  Set rst = db.OpenRecordset("Customers", DAO.RecordsetTypeEnum.dbOpenTable)

  With rst
    .AddNew
     lngID = .Fields("CustID")  [green]'capture autonumber for new record[/green]
    .Fields("CustName") = "Arnold Schwarznegger"
    .Fields("Gender") = "Male"
    .Fields("Age") = "49"
    .Update
  End With
  
  rst.Close
  Set rst = db.OpenRecordset("Jobs", DAO.RecordsetTypeEnum.dbOpenTable)
  
  With rst
    .AddNew
    .Fields("CustID") = lngID  [green]'foreign key link to parent table[/green]
    .Fields("StartDate") = "1/1/04"
    .Fields("CompleteDate") = "12/31/04"
    .Fields("JobName") = "World Gym"
    .Fields("Cost") = "650000"
    .Update
  End With

ExitHere:
  On Error Resume Next
  rst.Close
  Set rst = Nothing
  Set db = Nothing
  Exit Sub
ErrHandler:
  Debug.Print Err, Err.Description
  Resume ExitHere
End Sub

VBSlammer
redinvader3walking.gif

"You just have to know which screws to turn." - Professor Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top