473,699 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Prevent AutoIncrement of Id unless record saved

1 New Member
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 7339
DonRayner
489 Recognized Expert Contributor
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 Recognized Expert Specialist
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
17437
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 vb.net and try to add a record this field is autoincrementing. how can i disable the autoincrement of this field yet serves this as my primary key? thanks in advance
1
1746
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 field CustomerID OrderDate DeliveryAddress
9
14579
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 FirstName/LastName/email? Or is it possible to allow this but to throw up an alert message? Warning that this person is probably already in the database? TIA
11
4838
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 user clicks to view an alternative record i do not want the values they had typed into that record to be saved. I only want the valuse to be saved if the user clicks my save button on my page.
12
12938
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 we prevent this from happening? Thanks. ming
1
2212
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 column in the parent. I have the parent fields in textboxes and the child is a DataGrid. When I add a new parent record, then go to add child records, I am getting an exception that says, "ForeignKeyConstraint requires the child key values (0) to...
3
3039
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 Key) in other table(s) - record entries will be made via ADO I am soliciting opinions on the pros and cons of using AUTOINCREMENT versus code generated GUID or LONG value (as far as my limited understanding goes, if I enter a record, and need...
1
3432
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 1...and adding 1 to every new record. The Autoincrement needs to be separate for each ID. For example when I go to the form and enter a new record in the main form, the SET field should start over at 1 as a default and continue (+1) for each new...
7
5234
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 click. also when designing, going to design mode saves the record. So I tried: OnClose : Me.Undo. My own cancel button uses it and it works ok,
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7745
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.