Solved

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

Posted on 2011-09-16
6
1,893 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

776 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