Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 2 / July 2007

Tip: Looking for answers? Try searching our database.

Key field with month/year and random number

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
access_newbie - 23 Jul 2007 15:32 GMT
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
Jeff Boyce - 23 Jul 2007 16:58 GMT
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

> New to this.
> I need to design a key field starting with a month and year followed by an
[quoted text clipped - 3 lines]
> Thanks
>    B
access_newbie - 23 Jul 2007 21:22 GMT
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?

> 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
[quoted text clipped - 21 lines]
> > Thanks
> >    B
Jeff Boyce - 24 Jul 2007 00:36 GMT
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
Michael Gramelspacher - 24 Jul 2007 00:56 GMT
> 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]
vicente - 24 Jul 2007 20:51 GMT
> New to this.
> I need to design a key field starting with a month and year followed by an
[quoted text clipped - 3 lines]
> Thanks
>     B
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.