Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VB.net check the AutoIncrement property in an Access database

Hi

What VB.net code would I use to get the auto increment value of a column in
an Access database?
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

here an example:
Dim cmd As OleDbCommand = New OleDbCommand("SELECT @@IDENTITY", connection)
    Dim reader  As OleDbDataReader = cmd.ExecuteReader()
    reader.Read()
    dim nextIdentity as int = executeInsertGetIdentity = reader.Item(0).ToString()

Open in new window

Avatar of Murray Brown

ASKER

Hi

Thanks.

If I have a table called "Transactions", how would I use your code to check the if the AutoIncrement value of the ID column?
Would I use SELECT @@IDENTITY FROM Transactions
or do I actually have to execute insert statement in order to get the value
for specific table use this:
Dim cmd As OleDbCommand = New OleDbCommand("SELECT IDENT_CURRENT('Transactions')+1", connection)
    Dim reader  As OleDbDataReader = cmd.ExecuteReader()
    reader.Read()
    dim nextIdentity as int = reader.Item(0).ToString()

Open in new window

Hi. Thanks
I get the error "Undefined function ‘IDENT_CURRENT’ in expression"

when running the following code:

         Dim cs As String = Globals.ThisAddIn.oRIGHT.lblConnectionString.Text
                Dim myConnection As OleDbConnection = New OleDbConnection(cs)

                Try
                    Dim cmd As OleDbCommand = New OleDbCommand("SELECT IDENT_CURRENT('" & oTableName & "')+1", myConnection)
                    myConnection.Open()
                    Dim reader As OleDbDataReader = cmd.ExecuteReader()
                    reader.Read()
                    Dim nextIdentity As Integer = reader.Item(0).ToString()
                    Is_Identity_Column = True
                Catch ex As Exception
                    Is_Identity_Column = False
                    MsgBox(ex.Message)
                End Try
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Sedgwick
The purpose of this code is to check if the ID column in table is an AutoIncrement field
I should have mentioned that in the question, but I thought that it was apparent in my code
the code i've posted will return u the next auto increment identity.
i'm not sure i'm following you
Thanks very much