vb.net and access get id of the last inserted row

hi
i am accessing access using vb.net
i need to insert a row and get the id of this inserted row.
for now i am counting the elements and taking the last one.
is there a better practice??
thanks
LVL 3
mte01Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
JRossi1Connect With a Mentor Commented:
Try this:


SQLString = "select @@Identity as id"
DBCommand = New OleDb.OleDbCommand(SQLString, conn)
Dim dr As OleDb.OleDbDataReader
dr = DBCommand.ExecuteReader()
ID = dr.Item("id")
0
 
Arthur_WoodConnect With a Mentor Commented:
for now i am counting the elements and taking the last one.
is there a better practice??


In general NO, that is not a good idea, UNLESS - the table has a Primary Key which is declared as AUTONUMBER, so that every newly added row gets the next higher PrimaryKey value.

AW
0
All Courses

From novice to tech pro — start learning today.