473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Prevent AutoIncrement of Id unless record saved

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
2 7288
DonRayner
489 Expert 256MB
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
3,532 Expert 2GB
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

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

Similar topics

4
by: jaYPee | last post by:
I have a table in my sqlserver 2000 that has a field IDNO. i want this field to be my primary key. however i don't want this field to use the autoincrement feature. when i access this table from...
1
by: Job Lot | last post by:
In a multi-user environment, how would I prevent users from adding Orders with identical OrderID. For instance I have following tables in my database Order ================== OrderID Identity...
9
by: Catherine Jo Morgan | last post by:
Can I set it up so that a certain combination of fields can't contain the same entries, on another record? e.g. a combination of FirstName/LastName/address? Or FirstName/LastName/phone? Or...
11
by: simonmarkjones | last post by:
Hello there! I'm Still pretty new to access only been using it for a couple of days. If a record has been partially filled out or even completly filled out and then the form is closed or the...
12
by: swingingming | last post by:
Hi, in the NorthWind sample database, when clicking on the next navigation button on the new order record with nothing on the subform (order details), we got an order with nothing ordered. How can...
1
by: Aaron Smith | last post by:
I have a parent table that has one child table. The parent has a single field (ID, AutoIncrement, Seed 0, Step -1 in the DataSet, Seed 1, step 1, in DataSource). The child is linked to this ID...
3
by: MP | last post by:
context: vb6/ ado / .mdb format / jet 4.0 (not using Access - ADO only) - creating tables via ADO (don't have access) - all tables have a primary key (PK) - many of the PK will become FK(Foreign...
1
by: Mike | last post by:
I have a form that has an embedded subform (Datasheet View) that are linked based on a 1-many ID field. I have a field (SET) in my embedded subform that I want to Autoincrement starting with...
7
by: Michiel Rapati-Kekkonen | last post by:
Hi, I would like my record to be saved only when my own close button (with all it's checks) is used. I made the winodws close button disappear, but you still can close using the right mouse...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.