Solved

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

Posted on 2011-09-16
6
1,940 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
  • 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
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.
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

790 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