Solved

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

Posted on 2011-09-16
6
1,817 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 - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) 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 - Access MVP) 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now