How do I Create Autoincrement Fields in Visual Basic for Access?

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How do I Create Autoincrement Fields in Visual Basic for Access?
It's simple to create autoincrement fields in Access using VBA. (binary background image by Attila Toro from Fotolia.com)

Knowing how to create an autoincrement field in an Access table using Visual Basic for Applications, or VBA, can make your application more dynamic. An autoincrement field is a field in a table that automatically increments for every new record. Microsoft Office Access is a relational database management system used for smaller databases. VBA is a programming language developed by Microsoft and is used to automate routine tasks in Microsoft Office applications.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Start Microsoft Office Access 2007 and select "Blank Database," then click "Create." Click "Database Tools," then select "Visual Basic."

  2. 2

    Click the Insert field and select "Module."

  3. 3

    Copy and paste the following code into your module:

    Private Sub autoIncrementField()

    Set dbs = Application.CurrentDb
    
    Set tblDef = dbs.TableDefs("addressTbl")
    
    
    
    Set Newfield = tblDef.CreateField("AutoField", dbLong)
    
    With Newfield
    
        .Attributes = dbAutoIncrField
    
    End With
    
    
    
    With tblDef.Fields
    
        .Append Newfield
    
        .Refresh
    
    End With
    

    End Sub

  4. 4

    Edit the following line of code and type in the name of your table:

    Set tblDef = dbs.TableDefs("addressTbl")

    Press "F5" to execute your subroutine.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.