Thanks Ron. I initially thought it might be an issue with the DB
connection so, thanks to your advice, I *think* I am now using the
same connection. But I am still getting 0 returned. Here's a bit
more of my code. Any additional help is greatly appreciated as I am
stumped on this one!
****
Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuSave.Click
Try
cnConnection = fnOpenDPKDirectConn() ' opens connection to DB
bmcPersonMain.EndCurrentEdit()
daPersonMain.Update(dstPersonMain, "Person")
dstPersonMain.AcceptChanges()
MsgBox("Record saved successfully.", MsgBoxStyle.OKOnly)
Catch ex As Exception
MsgBox("Exception: " & (ex.ToString))
Finally
fnCloseDPKDirectConn(cnConnection)
End Try
End Sub
****
Private Sub daPersonMain_RowUpdated(ByVal sender As Object, ByVal e As
System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles
daPersonMain.RowUpdated
Dim newID As Integer
Dim idCMD As OleDbCommand = New OleDbCommand("SELECT @@IDENTITY",
cnConnection)
If e.StatementType = StatementType.Insert Then
newID = CInt(idCMD.ExecuteScalar())
e.Row("person_ID") = newID
End If
End Sub
*****
"Ron Allen" <ra****@src-us.com> wrote in message news:<uS**************@TK2MSFTNGP11.phx.gbl>...
Brian,
You have to have the same connection and be using an AutoNumber field as
the PK for this to work in Access. The call needs to be be done immediately
after the insert occurs. Also, call e.Row.AcceptChanges() afterwards to
avoid having the row marked as having been edited. You also need to keep
the connection open between calls.
This type of call works just fine for me for both Access 2K and 2002.
Is this in a OleDbRowUpdatedEventHandler routine? Or just directly after
a manual insert? microsoft.public.dotnet.framework.adonet may be more
appropriate for this type of question.
Ron Allen
"Brian" <br*********@yahoo.com> wrote in message
news:be**************************@posting.google.c om... Hello all -
I am trying to Insert a new record to an Access 2002 database.
Following the insert, I need to obtain the primary key of the inserted
row (person_ID) which is an Autonumber field. I am using the
following code (obtained from MSDN) to do this, but for some reason a
value of 0 is returned. Why is 0 returned????
Dim newID As Integer = 0
Dim idCMD As OleDbCommand = New OleDbCommand("SELECT @@IDENTITY",
cnConnection)
If e.StatementType = StatementType.Insert Then
newID = CInt(idCMD.ExecuteScalar())
e.Row("person_ID") = newID
End If
Also, the insert command is successful and does not cause any errors.
Thanks!
Brian