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:
Start Microsoft Office Access 2007 and select "Blank Database," then click "Create." Click "Database Tools," then select "Visual Basic."
Click the Insert field and select "Module."
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
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.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for