Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-16
6
Medium Priority
?
2,342 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 1000 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 1000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

610 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