View Full Version : Trying to create a multi-criteria incrementing number


gold007eye
07-03-2006, 07:52 AM
Is there any way it would be possible to have a number automatically created using NZ() or DMax function; based on certain criteria and also when creating the number preventing a duplicate number creation in the even of a simultaneous record creation?

Here's my example:

I have a CCN Number (Currently the employee manually types it in; and it is the primary key. What I would like is to take out the human element (error possibilities) and let access create it for the employee.

Here is how the CCN looks and the breakdown of each section (the important part's will be the first 3 sets of numbers):

96-06-184-100-000

96 = Region Code
06 = Current Year
184 = Julian Calendar Date (based on the receipt date field)
100 = Filler number based on day of the week (Eg. Monday = 100, Tuesday =
200, etc.)
000 = Filler numbers (Should always be Zero's (000)

I need to find away to pull this information from a table of some sort and combine it to create the CCN and to then add 1 (incriment) to the 4th set of numbers. Example 100 (1st record of the day) 101, 102, 103, etc.

Any help or ideas would be greatly appreciated.

Pat Hartman
07-03-2006, 08:03 AM
This topic has been covered here extensively. Usually there is only one prefix field but having multiples doesn't affect the solution, you just need additional criteria in the where clause of the DMax(). Your primary key needs to be defined to include all 5 fields. Do not use a single mushed field. It will not be pleasent to work with.

There is no way to prevent duplicates automatically. You need to detect the duplicate by trapping the error message and loop until your code generates a non-duplicate. My suggestion is to generate the sequence number at the last possible moment before the record is inserted (the form's BeforeUpdate event) to minimize the potential for duplicates but there is no way to actually prevent them ahead of time in a busy multi-user environment.

gold007eye
07-03-2006, 08:14 AM
Do you know if there are any sample databases that shows examples of this or of the code so I can get an idea of what I need to do (table structure, code, etc. wise)?

Pat Hartman
07-03-2006, 08:20 AM
I don't know if there are any sample databases but there is definitely lots of sample code. Try searching for "DMax(" The correct solutions are all based on DMax(). Do NOT choose a solution based on DLast().

gold007eye
07-03-2006, 08:33 AM
I am trying to sift through search results now.. Still no luck yet. I will try using your search criteria to see what I can find.