PC Review


Reply
Thread Tools Rate Thread

Autonumber Macro

 
 
jhicsupt
Guest
Posts: n/a
 
      13th Apr 2009
I am not that great in Access. I found this code to reset the Autonumber
field in a table. However I am unsure of how to substitute my fields in the
code.

Table Name = IP-1
Autonumber field in Table = "Primary"

Can you tell me how to substitute the Table name and the autonumber field in
the below code?

Thanks in advance.

Private Sub DeleteData()
Dim db As DAO.Database
Dim Rec As DAO.Recordset
Dim a As Long
Set db = CurrentDb()
Set Rec = db.OpenRecordset("YourTable", dbOpenDynaset)
?Make sure there are records to delete
If Not Rec.EOF Then
Rec.MoveFirst
Do Until Rec.EOF
Rec.Delete
Rec.MoveNext
Loop
End If
?Set a to the number before the number you want to start with
?Set it to 0 if you want to start at 1
?This will start the count at 15
a = 14
Rec.AddNew
'Set your autonumber field to a
Rec![ID] = a
?Make sure you use data that matches Field(1)
Rec.Fields(1) = "a"
?Update it then delete it
Rec.Update
Rec.MoveFirst
Rec.Delete
Rec.Close
Set Rec = Nothing
End Sub

 
Reply With Quote
 
 
 
 
Dorian
Guest
Posts: n/a
 
      14th Apr 2009
Why do you need to do this?
To reset the autonumber, empty the table and do a compact and repair.
-- Dorian
"Give someone a fish and they eat for a day; teach someone to fish and they
eat for a lifetime".


"jhicsupt" wrote:

> I am not that great in Access. I found this code to reset the Autonumber
> field in a table. However I am unsure of how to substitute my fields in the
> code.
>
> Table Name = IP-1
> Autonumber field in Table = "Primary"
>
> Can you tell me how to substitute the Table name and the autonumber field in
> the below code?
>
> Thanks in advance.
>
> Private Sub DeleteData()
> Dim db As DAO.Database
> Dim Rec As DAO.Recordset
> Dim a As Long
> Set db = CurrentDb()
> Set Rec = db.OpenRecordset("YourTable", dbOpenDynaset)
> ?Make sure there are records to delete
> If Not Rec.EOF Then
> Rec.MoveFirst
> Do Until Rec.EOF
> Rec.Delete
> Rec.MoveNext
> Loop
> End If
> ?Set a to the number before the number you want to start with
> ?Set it to 0 if you want to start at 1
> ?This will start the count at 15
> a = 14
> Rec.AddNew
> 'Set your autonumber field to a
> Rec![ID] = a
> ?Make sure you use data that matches Field(1)
> Rec.Fields(1) = "a"
> ?Update it then delete it
> Rec.Update
> Rec.MoveFirst
> Rec.Delete
> Rec.Close
> Set Rec = Nothing
> End Sub
>

 
Reply With Quote
 
jhicsupt
Guest
Posts: n/a
 
      14th Apr 2009
This is for a multi-user environment. Compacting and repairing will prohibit
users from using database while this is going on.

I need this so that the person that covers for me will not have to go into
Design mode.

Thanks so much.

"Dorian" wrote:

> Why do you need to do this?
> To reset the autonumber, empty the table and do a compact and repair.
> -- Dorian
> "Give someone a fish and they eat for a day; teach someone to fish and they
> eat for a lifetime".
>
>
> "jhicsupt" wrote:
>
> > I am not that great in Access. I found this code to reset the Autonumber
> > field in a table. However I am unsure of how to substitute my fields in the
> > code.
> >
> > Table Name = IP-1
> > Autonumber field in Table = "Primary"
> >
> > Can you tell me how to substitute the Table name and the autonumber field in
> > the below code?
> >
> > Thanks in advance.
> >
> > Private Sub DeleteData()
> > Dim db As DAO.Database
> > Dim Rec As DAO.Recordset
> > Dim a As Long
> > Set db = CurrentDb()
> > Set Rec = db.OpenRecordset("YourTable", dbOpenDynaset)
> > ?Make sure there are records to delete
> > If Not Rec.EOF Then
> > Rec.MoveFirst
> > Do Until Rec.EOF
> > Rec.Delete
> > Rec.MoveNext
> > Loop
> > End If
> > ?Set a to the number before the number you want to start with
> > ?Set it to 0 if you want to start at 1
> > ?This will start the count at 15
> > a = 14
> > Rec.AddNew
> > 'Set your autonumber field to a
> > Rec![ID] = a
> > ?Make sure you use data that matches Field(1)
> > Rec.Fields(1) = "a"
> > ?Update it then delete it
> > Rec.Update
> > Rec.MoveFirst
> > Rec.Delete
> > Rec.Close
> > Set Rec = Nothing
> > End Sub
> >

 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      14th Apr 2009
On Mon, 13 Apr 2009 14:13:01 -0700, jhicsupt
<(E-Mail Removed)> wrote:

> I found this code to reset the Autonumber
>field in a table.


Why do you need to do so?

Autonumbers have one purpose and one purpose ONLY: to provide a guaranteed
unique key. If you're assigning other meaning to them, you're skating on thin
ice.

312271, 312272 and 312295 are *just* as good as unique identifiers for three
rows as are 1, 2 and 3.
--

John W. Vinson [MVP]
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create a Sub Autonumber that re-assigns autonumber to each data groupin a sorted field. Junwenren Microsoft Access 1 4th Oct 2008 05:30 AM
Macro to autonumber cells =?Utf-8?B?SmltRw==?= Microsoft Excel Programming 2 8th Dec 2005 10:47 PM
how do I add an autonumber field in a macro? =?Utf-8?B?U25vcmt5Mms=?= Microsoft Access Macros 1 15th Dec 2004 09:10 AM
Adding autonumber field using a macro Beginner Microsoft Access Macros 4 11th Sep 2003 08:37 PM
Changing next value of Autonumber using a module or macro Robert Miller Microsoft Access VBA Modules 7 22nd Aug 2003 11:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:49 PM.