Key field with month/year and random number

A

access_newbie

New to this.
I need to design a key field starting with a month and year followed by an
incremented number. Is there any way to do this other than creating a
segmented key? Ex. 02070001 for the first record of February 2007.

Thanks
B
 
J

Jeff Boyce

A strong recommendation -- DON'T! Good database design suggests "one fact,
one field". You are asking how to stuff month and year and sequence number
all into one field.

Instead, capture a date (not just month and year), and capture a sequence
number, each in their own fields. Then create a query that grabs both those
and concatenates them into the form you want to see displayed.

"Custom Autonumber" is a good search term to learn more about building your
own procedure for sequential numbers. Don't use Access Autonumber to do
this, as they are NOT guaranteed to be sequential (and, given time, you'll
end up with 'gaps').

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
A

access_newbie

I'm needing a primary key. This is for animal intake in an animal shelter. I
want the first part of the key ID to reflect the date such as 0702 and the
second to be a sequential number. The number to flip back to 001 the next
month. So I would have 0702001 for the first intake in Feb of 2007 and
0703001 for the first intake of March 2007. I want them to be able to input
the date part (which will also be part of the intake date) but to have the
rest autonumber.We need this to make it easy by looking at the ID to know
when the animal came in. This ID will go on charts, be used when the vet
calls, etc. Can I take 2 fields on the table and make them into one primary
key? Or should I just make the a standard sequential and make the ID not a
key?
 
J

Jeff Boyce

My suggestion (collect others' ideas, too) would be to use something else
(?an Autonumber field) as a primary key, and use a query to concatenate the
YYMM### for user consumption. Don't have them working directly in the
tables, but in forms (and reports) instead.

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
M

Michael Gramelspacher

I'm needing a primary key. This is for animal intake in an animal shelter. I
want the first part of the key ID to reflect the date such as 0702 and the
second to be a sequential number. The number to flip back to 001 the next
month. So I would have 0702001 for the first intake in Feb of 2007 and
0703001 for the first intake of March 2007. I want them to be able to input
the date part (which will also be part of the intake date) but to have the
rest autonumber.We need this to make it easy by looking at the ID to know
when the animal came in. This ID will go on charts, be used when the vet
calls, etc. Can I take 2 fields on the table and make them into one primary
key? Or should I just make the a standard sequential and make the ID not a
key?
Maybe try something like this. This uses two separate fields as a primary key.
This will probably only work with a single user database. I have read that
self-baked numbering sequences always fail in a multi-user database.

Copy sub code to a module of a new database and type
call CreateFeedings in the immediate window.
(not really tested)

Sub CreateFeedings()
With CurrentProject.Connection
.Execute _
"CREATE TABLE Animals" & _
" (animal_nbr INTEGER NOT NULL" & _
", animal_name VARCHAR (25) NOT NULL" & _
", PRIMARY KEY (animal_nbr));"

.Execute _
"CREATE TABLE Feedings" & _
" (intake_date DATETIME DEFAULT Now() NOT NULL" & _
", intake_sequence INTEGER DEFAULT 0 NOT NULL" & _
", animal_nbr INTEGER DEFAULT 1 NOT NULL" & _
", CONSTRAINT FK_Animals" & _
" FOREIGN KEY(animal_nbr)" & _
" REFERENCES Animals(animal_nbr)" & _
", CONSTRAINT PK_Feedings" & _
" PRIMARY KEY (intake_date,intake_sequence));"
End With
End Sub

form: frmFeedings
txtIntakeDate
txtIntakeSequence enabled and locked
txtAnimalNbr

Private Sub txtIntakeSequence_GotFocus()
If Me.NewRecord Then
Me.txtIntakeSequence = Nz(DMax("intake_sequence", _
"Feedings", "DateDiff('m',0, #" & Me.txtIntakeDate & "#)" & _
" = Datediff('m',0,intake_date)")) + 1
End If
End Sub
 
J

Jeff Boyce

Michael

"always fail" may be a bit strong, and may be specific to a particular
approach/solution.

It will take some extra work on the developer's part, but there's no reason
it couldn't work, as far as I know.

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
M

Michael Gramelspacher

Michael

"always fail" may be a bit strong, and may be specific to a particular
approach/solution.

It will take some extra work on the developer's part, but there's no reason
it couldn't work, as far as I know.

Regards

Jeff Boyce
Microsoft Office/Access MVP

Yes, you are right. Most eventually fail might be a better phrase, but that is
just based on reading, not experience.
 
J

John W. Vinson

Maybe try something like this. This uses two separate fields as a primary key.
This will probably only work with a single user database. I have read that
self-baked numbering sequences always fail in a multi-user database.

<shrug> I've been using a variant of Getz and Litwin's Developer's Handbook
custom counter for years. Five customers have the PawTrax database installed,
adding up to tens of thousands of records in each of six tables using the
code. No failures yet.

John W. Vinson [MVP]
 
V

vicente

access_newbie said:
New to this.
I need to design a key field starting with a month and year followed by an
incremented number. Is there any way to do this other than creating a
segmented key? Ex. 02070001 for the first record of February 2007.

Thanks
B
 
Top