MustangPriMe
IS-IT--Management
I have a form with some code which adds a row to a SQL table and then uses the ID of the newly created row to go on to do other things.
The code for adding the row and getting the new ID is along the lines of:
This worked fine until I added a Insert trigger on tblName which itself creates a row in an audit table:
Now what happens in the VB code is that after the rs.Update line, instead of rs("fldID") containing the ID of the newly inserted row in tblName, it actually contains the ID of the row inserted into tblAudit via the trigger, even despite the fact that a field called fldID does not exist in tblAudit.
I require a trigger due to that fact that some people need direct access to the tables, but need a way of getting the ID number from a newly added row in a VB recordset.
Can anyone help?
Paul
The code for adding the row and getting the new ID is along the lines of:
Code:
rs.Open "tblName", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
rs.AddNew
rs("fldOne") = varOne
rs("fldTwo") = varTwo
rs.Update
varID = rs("fldID")
This worked fine until I added a Insert trigger on tblName which itself creates a row in an audit table:
Code:
CREATE TRIGGER trgCreateAuditRecord ON dbo.tblName
FOR INSERT
AS
BEGIN
DECLARE @varNote NVARCHAR(2000)
SET @varNote = 'New record created' + GETDATE()
INSERT INTO tblAudit (fldDate, fldUserID, fldNote)
VALUES (GETDATE(), dbo.fnZenithUserID(), 'Name entered - ' + @varNote)
END
Now what happens in the VB code is that after the rs.Update line, instead of rs("fldID") containing the ID of the newly inserted row in tblName, it actually contains the ID of the row inserted into tblAudit via the trigger, even despite the fact that a field called fldID does not exist in tblAudit.
I require a trigger due to that fact that some people need direct access to the tables, but need a way of getting the ID number from a newly added row in a VB recordset.
Can anyone help?
Paul