Solved

VB6 - reset ID AutoNumber field  in ms access table

Posted on 2012-12-29
8
2,838 Views
Last Modified: 2012-12-29
Hello all

I would like to reset the ID AutoNumber field from my MS Access table when i delete a record.

How can i do that?

Thanks again for your help

Full code
Private Sub delete_Click()
    Select Case MsgBox("Do you realy want to delete that record " & ID_NUM.Text & " ?", vbYesNo Or vbQuestion Or vbSystemModal Or vbDefaultButton1, "Delete...")

    Case vbYes

        Set con = New ADODB.Connection
        con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & link_for_base.Caption
        con.Open
        con.Execute ("DELETE * From [TDM_Rate_form_no1] WHERE ID= " & ID_NUM.Text)
        con.Close

        con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;;Data Source=" & link_for_base.Caption
        con.Open
        rs.Open "select * from TDM_Rate_form_no1", con, adOpenDynamic, adLockOptimistic
        Set MSHFlexGrid1.Recordset = rs
        rs.Close

    Case vbNo

    End Select
End Sub

Open in new window

0
Comment
Question by:Wilder1626
  • 3
  • 3
  • 2
8 Comments
 
LVL 75
ID: 38729753
From VBA code, you can do this - I assume you mean the AutoNumber:

** 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,3)

You can run this against an empty existing table, or to modify the *next higher* Auto Number value and increment.
It will *not* change any existing auto number values.

This works for A2K and later.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38729758
Hi DatabaseMX

Is this something i can do from my VB6 app?
0
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) earned 200 total points
ID: 38729767
If you can run VBA code.  You might have to change

CurrentDb.Execute sSQL

to

DBEngine(0)(0),Execute sSQL

mx
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 29

Accepted Solution

by:
IrogSinta earned 300 total points
ID: 38729769
What is your purpose for reseting the autonumber field.  This is usually used as a primary key to uniquely identify each record.  This would cause problems if this table is related to another table using this key.  Where you thinking that you would run out of numbers for this field.  An autonumber field with a data type of Long can go up to over 2 billion.
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 38729795
Hi all

IrogSinta, i hunerstand your point and it make sense.

I will leave it as is.

Thanks again for all your help
0
 
LVL 75
ID: 38729811
"An autonumber field with a data type of Long can go up to over 2 billion. "
Actually ... that's not quite accurate - since we're now on that subject:

An Auto Number (Long Integer) will increment from 1 to 2,147,483,647, then jump to -2,147,483,648, and then
count up to 0. Adding one record a second, 24/7 with no holidays, you can add
records for a bit over 136 years before hitting the limit.

3600 sec/hour * 24 hour/day * 365 days/year = 31,536,000 seconds/year
2,147,483,647 + 2,147,483,648 + 1 = 4,294,967,296 auto numbers

1 year                    1 Sec               4,294,967,296 auto numbers      = 136 YEARS !
-----------------  *  ------------  * ------------------------
31,536,000 seconds      Auto Number


AN                 SomeField

-2147483648      5
-2147483647      6
-2147483645      2
-2147483646      3
-2147483647      4
-4                    a
-3                   b
-2                   c
-1                  d
0                  e
1                  x
2                    z

mx
0
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38729858
Cool explanation, mx.

:-)
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38729969
Thanks for the explanation DatabaseMX
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

786 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