Prevent Autonumber Increment If record not Saved (1 Viewer)

usm01

Registered User.
Local time
Yesterday, 21:51
Joined
Oct 4, 2009
Messages
16
I am new to Database.
i have a form with an Autonumber field and some other fields with validation rules.
If user cancels or closes the form or if record is not saved, i want prevent Autonumber from incrementing. It should Increment if a record is saved with all validation rules fulfilled.

In short I want no gaps Between autonumber Prospectively.

Plz Help.
 

John Big Booty

AWF VIP
Local time
Today, 14:51
Joined
Aug 29, 2005
Messages
8,263
Firstly Welcome to the forum

The autonumber, should not be relied upon to do anything other than provide a unique identifier for each record.

If you require sequential numbering for some purpose, then you will need to look at implementing your own code to do this.

Look into the Dmax() and search this forum as this is a subject that comes up on a regular basis.
 

MarkK

bit cruncher
Local time
Yesterday, 21:51
Joined
Mar 17, 2004
Messages
8,180
Hi, and welcome to the forum.
Autonumber doesn't work that way. Autonumber guarantees a unique ID, not a consecutive ID. In a multi-user environment it has to be impossible for two concurrent users to save a record with the same ID, so an Autonumber is only ever available once for the lifetime of the table.
You can 'roll your own' consecutive number using DMax() + 1
Cheers,
 

MarkK

bit cruncher
Local time
Yesterday, 21:51
Joined
Mar 17, 2004
Messages
8,180
Almost verbatim.
And isn't that 'Boo-tay?'
Cheers,
 

usm01

Registered User.
Local time
Yesterday, 21:51
Joined
Oct 4, 2009
Messages
16
Thanks for quick reply .
I will search the forum.
 

missinglinq

AWF VIP
Local time
Today, 00:51
Joined
Jun 20, 2003
Messages
6,423
Here's a typical Auto-incrementing Number hack. The first code here would be for an IDNumber that is defined in the table as Text datatype.

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim StartName as Long

If Me.NewRecord Then
  If RecordsetClone.RecordCount = 0 Then
  Me.IDNumber = “1"
Else
   Me.IDNumber = DMax("val([IDNumber])", "YourTableName") + 1
  End If
End If
End Sub
Here's the same code for an IDNumber defined as Numerical:

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
  If RecordsetClone.RecordCount = 0 Then
  Me.IDNumber = 1
 Else
   Me.IDNumber = DMax("[IDNumber]", "YourTableName") + 1
  End If
End If
End Sub

You'll just need to place your validation code in the BeforeUpdate event before the auto-incrementing code, and have the auto-incrementing code placed so that it will not be executed if the record is dumped.
 

usm01

Registered User.
Local time
Yesterday, 21:51
Joined
Oct 4, 2009
Messages
16
It Works.
Thanks For Helping.
 

rayhan

New member
Local time
Yesterday, 21:51
Joined
Jul 21, 2017
Messages
5
Here's a typical Auto-incrementing Number hack. The first code here would be for an IDNumber that is defined in the table as Text datatype.

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim StartName as Long

If Me.NewRecord Then
  If RecordsetClone.RecordCount = 0 Then
  Me.IDNumber = “1"
Else
   Me.IDNumber = DMax("val([IDNumber])", "YourTableName") + 1
  End If
End If
End Sub
Here's the same code for an IDNumber defined as Numerical:

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
  If RecordsetClone.RecordCount = 0 Then
  Me.IDNumber = 1
 Else
   Me.IDNumber = DMax("[IDNumber]", "YourTableName") + 1
  End If
End If
End Sub

You'll just need to place your validation code in the BeforeUpdate event before the auto-incrementing code, and have the auto-incrementing code placed so that it will not be executed if the record is dumped.

I am using MS Access 2013.
This code not work in my data base.
Please Submit a sample File.
Please Help Me.
Table name SrRegister, Field name Record_Number, Data Type - Number
 

Minty

AWF VIP
Local time
Today, 05:51
Joined
Jul 26, 2013
Messages
10,368
Doesn't work is no help to us - what is your code and what is the error message you are getting?
 

rayhan

New member
Local time
Yesterday, 21:51
Joined
Jul 21, 2017
Messages
5
Doesn't work is no help to us - what is your code and what is the error message you are getting?

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
If RecordsetClone.RecordCount = 0 Then
Me.Record_Number = 1
Else
Me.Record_Number = Nz(DMax("[Record_Number]", "SrRegister")) + 1
End If
End If
End Sub

why This code not work?
pls help me.
 

Minty

AWF VIP
Local time
Today, 05:51
Joined
Jul 26, 2013
Messages
10,368
And what error message.? Because that should work.

Sent from my SM-G955F using Tapatalk
 

rayhan

New member
Local time
Yesterday, 21:51
Joined
Jul 21, 2017
Messages
5
And what error message.? Because that should work.

Sent from my SM-G955F using Tapatalk

When I Open The Form the Record_Number Field always Show 1.
I want form will open new unique sequence record number.
If last record _Number is 5.
When again i will open the form i want to show new record_number will 6 in record_number Field.
Pls help me.
 
Last edited:

rayhan

New member
Local time
Yesterday, 21:51
Joined
Jul 21, 2017
Messages
5
When I Open The Form the Record_Number Field always Show 1.
I want form will open new unique sequence record number.
If last record _Number is 5.
When again i will open the form i want new record_number will 6 in record_number Field.
Pls help me.
I Have attached my file here
 

Attachments

  • TestStoreDatabase.accdb
    912 KB · Views: 186

Minty

AWF VIP
Local time
Today, 05:51
Joined
Jul 26, 2013
Messages
10,368
The code as used will only get the latest number when the record is saved, not before. What if two people open the record at the same time?
 

Users who are viewing this thread

Top Bottom