How to renumber an Autonumber field...

rahulgty

Registered User.
Local time
Today, 07:13
Joined
Aug 27, 2005
Messages
99
Hi

I want to renumber an autonumber field to remove the gap appear after deleting the record.

I know it is possible by deleting field and creating a new autonumber field having same name.

But all this I want to do with code.

I will be gratefull if any one help me.

Regards

:confused: Rahulgty
 
Hi rahulgty,
What you will find is that you are using the autonumber field for a purpose to which it was never designed. It is for unique numbers *only* and should never be seen by the user.
 
Problem resolved

Thanks RuralGuy and Ghudson. :)

My problem has been resolved. I was planning to use autonumeber field for generating 5 digit Voucher No. in a accounting database. As per user requirement I have to keep the provision for deletion of Voucher No. And it was user's requirement that after deletion there must not be any gap between numbers.

Instead of autonumber field I have decided to use normal field (Text or integer). I have generated autoincremental Number by folowing functin:

Private Sub AutoNumber()
Dim rs As DAO.Recordset
Dim db As Database
Dim VrNo As String

Set db = CurrentDb
Set rs = db.OpenRecordset("Main", dbOpenSnapshot)

If rs.BOF = True Then
VrNo = rs.RecordCount + 1
Me.txtVrNo = VrNo
Me.txtVoucherNo = "Pymt " & String(5 - Len(VrNo), "0") & VrNo
Me.txtVoucherDate = "01/04/2005"

Else
rs.MoveLast
VrNo = rs.RecordCount + 1
Me.txtVrNo = VrNo
Me.txtVoucherNo = "Pymt " & String(5 - Len(VrNo), "0") & VrNo
Me.txtVoucherDate = rs.Fields("Date")
End If

rs.Close
Set rs = Nothing
Set db = Nothing
End Sub


And after delete command I have added the following :

Private Sub Renumber()
Dim db As Database
Dim rs As DAO.Recordset
Dim VrNo As Integer

VrNo = 1
Set db = CurrentDb
Set rs = db.OpenRecordset("Main", dbOpenDynaset)

rs.MoveFirst
Do Until rs.EOF
rs.Edit
rs!VrNo = VrNo
rs!VoucherNo = "Pymt " & String(5 - Len(VrNo), "0") & VrNo
rs.Update
VrNo = VrNo + 1
rs.MoveNext
Loop
rs.Requery
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub

This is working well at this time. Any way thanks for your reply.

Regards

Rahulgty
 
Last edited:
Thanks Pat Hartman

I welcome your valuable advise. Actually I am not a certified access programmer. I am learning all from scratch so I require this type of valuable advises in future too.

Rahulgty
 
Hi Pat Hartman

I just have the same problem as Mr Rahulgty. I like the approach you have given in your post. But I am not being able to apply in practice. I have read help files but unable to use the DMax function in table field. If it is not too much pain for you then can you please elaborate the step please. Actually I need an auto-incremental number for my inspection note which doesn't allow any skipped value(no.). Also I have another field in the same table called current year in which I have successfully used DatePart function to extract the year. My requirement is that the Inspection note no should again return to 1 when a new year begins else it should continue with a new number increased by one from the last record.

Please help me if it is not too much work for you. I am really stuck with this for a long time.

Regards...
 
Hi Pat Hartman

I just have the same problem as Mr Rahulgty. I like the approach you have given in your post. But I am not being able to apply in practice. I have read help files but unable to use the DMax function in table field. If it is not too much pain for you then can you please elaborate the step please. Actually I need an auto-incremental number for my inspection note which doesn't allow any skipped value(no.). Also I have another field in the same table called current year in which I have successfully used DatePart function to extract the year. My requirement is that the Inspection note no should again return to 1 when a new year begins else it should continue with a new number increased by one from the last record.

Please help me if it is not too much work for you. I am really stuck with this for a long time.

Regards...
rahulgty had this problem almost 5 years ago. If you had posted on a new thread or done a quick search you would have found a solution ;)

Have a look at this link:
http://www.access-programmers.co.uk/forums/showthread.php?t=91574
 

Users who are viewing this thread

Back
Top Bottom