C
Chris Burnette
I am having a problem inserting data into a table from a bound form in
Access. I am trying to insert the Primary Key of the Book table (BookID)
into the same field in the Author table, in which it is a Foreign Key. I do
this whenever a new record is created and data is entered for that record,
hence the AfterInsert event.
I have no problem accessing either the datasheet or the form for any of my
linked tables, however for some reason my code seems to be generating an ODBC
error.
My code looks like this:
Private Sub Form_AfterInsert()
Dim rst As DAO.Recordset
Set rst = CurrentDb().OpenRecordset("Author", dbOpenDynaset, dbSeeChanges)
rst.AddNew
rst!BookID = Me.BookID.Value
rst.Update
rst.Close
Set rst = Nothing
End Sub
The error looks like this:
ODBC--insert on a linked table 'Book' failed.
[Microsoft][ODBC SQL Server Driver][SQL Server]Explicit value must be
specified for identity column in table 'Book' either when IDENTITY_INSERT is
set to ON or when a replication user is inserting into a NOT FOR REPLICATION
identity column. (#545)
For some reason it seems to be trying to open the Book table even though I
specify Author as the record source in VBA. I'm not sure why it's doing
this, although I would speculate that it's because the form is bound to the
Book table.
So, the question is, is there a way to make it so that the PK of one table
is inserted into another as an FK? I have the relationships defined in
Access, so I don't think that's a problem.
If anyone could offer any suggestions about how best to do this, I would
appreciate it.
Thanks,
Chris
Access. I am trying to insert the Primary Key of the Book table (BookID)
into the same field in the Author table, in which it is a Foreign Key. I do
this whenever a new record is created and data is entered for that record,
hence the AfterInsert event.
I have no problem accessing either the datasheet or the form for any of my
linked tables, however for some reason my code seems to be generating an ODBC
error.
My code looks like this:
Private Sub Form_AfterInsert()
Dim rst As DAO.Recordset
Set rst = CurrentDb().OpenRecordset("Author", dbOpenDynaset, dbSeeChanges)
rst.AddNew
rst!BookID = Me.BookID.Value
rst.Update
rst.Close
Set rst = Nothing
End Sub
The error looks like this:
ODBC--insert on a linked table 'Book' failed.
[Microsoft][ODBC SQL Server Driver][SQL Server]Explicit value must be
specified for identity column in table 'Book' either when IDENTITY_INSERT is
set to ON or when a replication user is inserting into a NOT FOR REPLICATION
identity column. (#545)
For some reason it seems to be trying to open the Book table even though I
specify Author as the record source in VBA. I'm not sure why it's doing
this, although I would speculate that it's because the form is bound to the
Book table.
So, the question is, is there a way to make it so that the PK of one table
is inserted into another as an FK? I have the relationships defined in
Access, so I don't think that's a problem.
If anyone could offer any suggestions about how best to do this, I would
appreciate it.
Thanks,
Chris