Solved and access get id of the last inserted row

Posted on 2006-04-21
Last Modified: 2012-06-22
i am accessing access using
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??
Question by:mte01
    LVL 6

    Accepted Solution

    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")
    LVL 44

    Assisted Solution

    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.


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now