By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,011 Members | 2,967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,011 IT Pros & Developers. It's quick & easy.

Prevent AutoIncrement of Id unless record saved

P: 1
Hey all,

I have a table name JobDescription.
In it i have my PK as JobId, which I have set as a auto increment.

I created a form based on that table.
I have disable record navigators and record selectors.

From my switchboard when i click new job, it open the form i
have created. And populates the JobId with a new id.

However if i close the form w/o entering anything, and open it
again the JobId wud have incremented by 1 again.

Hence I have loads of dummy values, since i am testing some
buttons on the form.

How do I prevent the jobId to auto increment unless the user chooses to save?

One way i thought of is, when he clicks on anything else, i ask whether to save, if he says no, I delete the current record.

But I feel there shud be a more elegant way to do this.

Would appreciate all the help.

thanks loads!
Nov 14 '08 #1
Share this Question
Share on Google+
2 Replies


DonRayner
Expert 100+
P: 489
If you are looking for sequencial number of records then an autonumberd pk is not the way to go. Basicly all the autonumbering is for in Access is to make sure that each record gets a unique number, thus Access will not reuse a number once it's been assigned to a record (even if it's not saved).

If you want to have sequencial numbering you will have to use another number field that is not a PK or autonumber. You can then set the default value for this field using the Dmax() function on itself. This will search the field in the table for the maximum number, you then add 1 to it.

This method does however have one drawback in that if you have multiple users creating records at the same time you could end up with records with the same number. You can get around this by setting the field to indexed(no duplicates), this way only the first one to save will get the number. You could have a number request button on your form then for users to request a new number using the dmax() function again.

Expand|Select|Wrap|Line Numbers
  1. = DMax("MyField","MyTable") + 1
  2.  
Nov 14 '08 #2

missinglinq
Expert 2.5K+
P: 3,532
Don's advice is right on the money, here! Autonumbers are intended to be used for one purpose and only one purpose, to provide a unique identifier for each record. Here's a post I've archived from a gentleman named John Vinson, MVP, explaining how autonumbers work:

When using Autonumber, do be aware that there will be gaps in the numbering - any record that's deleted will leave a gap; hitting <Esc> after starting a record will leave a gap; adding records using an Append query may leave a gap, often a huge one; replicating the database will make your invoice numbers random.

In short... it's best NOT to use Autonumbers for human consumption, and particularly not for consumption by accountants and auditors. Invoice sequences like 319, 321, 322, 385, 386, 221841246, -1083225152 make such people get very nervous.
For the kind of field you're using, JobID, you need, as Don said, to use an auto-incrementing number hack. Here's two more complete examples.

The first code here would be for a JobID that is defined in the table as Text datatype. "Number" fields that aren't used for math really should be defined as Text.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. If Me.NewRecord Then
  3.   If RecordsetClone.RecordCount = 0 Then
  4.    Me.JobID = "1"
  5.   Else
  6.    Me.JobID = DMax("val([JobID])", "YourTableName") + 1
  7.   End If
  8. End If
  9. End Sub
If you insist on using a Numerical field:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. If Me.NewRecord Then
  3.   If RecordsetClone.RecordCount = 0 Then
  4.    Me.JobID = 1
  5.   Else
  6.    Me.JobID = DMax("[JobID]", "YourTableName") + 1
  7.   End If
  8. End If
  9. End Sub
Welcome to Bytes!

Linq ;0)>
Nov 14 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.