Solved

Using VBA to Add an autonumber field to a make table query without a primary key

Posted on 2011-09-16
6
2,048 Views
Last Modified: 2012-05-12
I have reviewed the posts regarding autonumbering and I can't get a straight answer.  I am very new to VBA and require detailed instructions on the exact steps in making this work.

I have attached an exported version of a table I created by a make table query.  I need to add an autonumber field to this table without having to manually add a column.  This list can change and I need to be able to reset the numbering.  I already have the tabled ordered exactly the way I need it but without a primary key.  Please help
tblEleaseTaskList.xlsx
0
Comment
Question by:dcmennealy
[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
  • 4
  • 2
6 Comments
 
LVL 75
ID: 36553151


To add and AutoNumber field:
CurrentDb.Execute "ALTER TABLE TableName ADD COLUMN ColumnName COUNTER"

===

To set your own starting point and incrementing value, here are two methods.
In all cases, make a BACKUP of your table first!!

** VBA Code method - Example**
Place this code in a standard module. Do not name the module the same as the Function below.

Public Function mResetAutoNumber(lStartVal As Long, lIncrement As Long) As String
   'Example: Set the Starting Value and Increment for a table named Table1ANTest and an AutoNumber field called AutoNum
    Dim sSQL As String
    sSQL = "ALTER TABLE [Table1ANTest] ALTER COLUMN [AutoNum] COUNTER (" & lStartVal & ", " & lIncrement & ");"
    CurrentDb.Execute sSQL
    mResetAutoNumber = "Auto Number has been re-numbered"
End Function

The first number (lStartVal) is the starting value, and the second (lIncrement) is the increment.
The example above will start the Auto Number at 1000 and increment by 50.

To run the code one time, open the VBA Immediate Window, and type
this line (including the question mark, followed by Enter:

?mResetAutoNumber(<YourStartValueNumber>, <YourIncrementNumber>)

Example:
?mResetAutoNumber(500,1)

mx



0
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 250 total points
ID: 36553182
One more method to create an Auto Number field (plus a couple of other fields - as a bonus)


    Dim db As DAO.Database
    Dim fld As DAO.Field
    Set db = CurrentDb
    With db.TableDefs("Table1")
        Set fld = .CreateField("YourID", dbLong)
        fld.Attributes = dbAutoIncrField    'make it an Auto Number
        .Fields.Append fld
        .Fields.Append .CreateField("FIELD1", dbText, 35)
        .Fields.Append .CreateField("FIELD2", dbText, 50)
    End With
    Set fld = Nothing
    Set db = Nothing

mx
0
 

Author Comment

by:dcmennealy
ID: 36553209
Where do I put this code?  When I open in up VBA IDE, I can chose either Module or Class Module.  Which one to I pick?  I am a beginner.
Untitled.png
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 250 total points
ID: 36553222
Choose Module.

For creating the AutoNumber field:

Public Function mCreateAN()
    CurrentDb.Execute "ALTER TABLE TableName ADD COLUMN ColumnName COUNTER"
End Function

For resetting the Auto Number:

Public Function mResetAutoNumber(lStartVal As Long, lIncrement As Long) As String
   'Example: Set the Starting Value and Increment for a table named Table1ANTest and an AutoNumber field called AutoNum
    Dim sSQL As String
    sSQL = "ALTER TABLE [Table1ANTest] ALTER COLUMN [AutoNum] COUNTER (" & lStartVal & ", " & lIncrement & ");"
    CurrentDb.Execute sSQL
    mResetAutoNumber = "Auto Number has been re-numbered"
End Function

You can call this from the VBA Immediate window
?mResetAutoNumber(<YourStartValueNumber>, <YourIncrementNumber>)

Include the question mark ... and function name, then hit Enter

And the other method to create the Auto Number

Public Function mCreateAnMethod2()

    Dim db As DAO.Database
    Dim fld As DAO.Field
    Set db = CurrentDb
    With db.TableDefs("Table1")
        Set fld = .CreateField("YourID", dbLong)
        fld.Attributes = dbAutoIncrField    'make it an Auto Number
        .Fields.Append fld
        '  .Fields.Append .CreateField("FIELD1", dbText, 35) 'optional
        '  .Fields.Append .CreateField("FIELD2", dbText, 50) ' optional
    End With
    Set fld = Nothing
    Set db = Nothing

End Function

You can also call this from the Immediate window.

I have to sign off now ... back in the am.  All of these methods work, so your search is over :-)
0
 

Author Comment

by:dcmennealy
ID: 36553224
I'm Sorry.  I did put the code into module.  What do I do next? You told me to type ?mResetAutoNumber(<YourStartValueNumber>, <YourIncrementNumber>)
 in the immediate window.  I am not sure what you mean.
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 250 total points
ID: 36553396
Sure ... see image below.

btw ... be sure to make a Backup of your db or at least the table before trying this ... just in case :-)
Capture1.gif
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

732 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