Question

I have an Access database that we use to house Worker's Compensation Accident Information. One of the required fields is an OSHA recordable number that is sequential starting with "01" and the two digit year of the accident (ex. 01-14).

I need to be able to programmitically look into my table see what numbers have already been used and find the next number in the sequence. It also needs to reset to 1 at the beginning of a new year.

Example: table reads

01-14 
02-14
03-14

The new number that populates the textbox should be 04-14

Help!

Was it helpful?

Solution

Given that you have a multi-user database, see insert query with sequential primary key or Access VBA: Find max number in column and add 1 for code to get the next sequential number.

You can use Year and the seed number to create the next OSHA number. You can reset the seed number on year change.

DMax is a possibility, but I strongly suggest you do not use it in a multi-user database.

OTHER TIPS

From what you have described, it sounds like the OSHA number should be generated as and when needed instead of being assigned and stored when the table row is created. My suggestion would be to just have the primary key (accident_id) be an autoincremented long integer, the standard practice in Access. And also you need an accident_date column to be a datetime or similar. Or at the very least an accident_year column. Then when you need an OSHA number (say in a report), just have some VBA to generate it using the primary key--accident_id--and the accident_date or accident_year column. You will be taking advantage of the fact that Access autoincremented primary keys are never re-used--even when their rows are deleted, those numbers are never recycled to be used in other rows. So given a long integer primary key, and a date, you can always reproduce the exact same OSHA number, with a simple algorithm something like the following:

function osha_number(accident_id as long) as string
  accident_year = ... ' get (last two digits of) accident year from accidents table using ID
  year_first_accident_id = ... ' get ID of first accident of this year
  year_this_accident_num = accident_id - year_first_accident_id + 1

  osha_number = year_this_accident_num & "-" & accident_year
end function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top