Solved

VB.net Access Check if Primary Key column is an AutoNumber

Posted on 2013-06-25
3
652 Views
Last Modified: 2013-06-25
Hi
I use the following VB.net code to check if a column is a Primary key.
How do I check whether it is an AutoNumber column


 Public Function getKeyNames(tableName As String) As String
        Try


            Dim connection As New OleDbConnection(Globals.ThisAddIn.oRIGHT.lblConnectionString.Text)
            connection.Open()

            Dim mySchema As DataTable = TryCast(connection, OleDbConnection).GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, New [Object]() {Nothing, Nothing, tableName})


            ' following is a lengthy form of the number '3' :-)
            Dim columnOrdinalForName As Integer = mySchema.Columns("COLUMN_NAME").Ordinal

            For Each r As DataRow In mySchema.Rows
                'THIS GETS ALL THE KEYS IN THE TABLE -BUT I AM JUST USING THE FIRST ONE
                ''returnList.Add(r.ItemArray(columnOrdinalForName).ToString())
                getKeyNames = r.ItemArray(columnOrdinalForName).ToString()
                GoTo Jump 'Jump out after retrieving first one
            Next

Jump:
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Function
0
Comment
Question by:murbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39274098
0
 

Author Comment

by:murbro
ID: 39274654
Hi Sedgwick

Thanks for that but what SQL query or VB.net code would I use for that?
0
 

Author Closing Comment

by:murbro
ID: 39274958
Thanks
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

738 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