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?
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
> I'm needing a primary key. This is for animal intake in an animal shelter.
> I
[quoted text clipped - 42 lines]
>> > Thanks
>> > B
> 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
[quoted text clipped - 33 lines]
> > > Thanks
> > > B
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
Jeff Boyce - 24 Jul 2007 15:55 GMT
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
>> I'm needing a primary key. This is for animal intake in an animal
>> shelter. I
[quoted text clipped - 90 lines]
> End If
> End Sub
Michael Gramelspacher - 24 Jul 2007 16:26 GMT
> Michael
>
[quoted text clipped - 8 lines]
> 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.
John W. Vinson - 24 Jul 2007 17:06 GMT
>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]