A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Database Design
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Composite Primary Key with an ID and Index



 
 
Thread Tools Display Modes
  #1  
Old August 18th, 2009, 09:31 PM posted to microsoft.public.access.tablesdbdesign
Pete
external usenet poster
 
Posts: 384
Default Composite Primary Key with an ID and Index

I would like to create a composite primary key using two fields: a random
integer ID, and a "line number" index. I want the index to be incremental,
start with 1, and reset for each unique ID from the first field. For example:

ID INDEX
5127 1
5127 2
5127 3
6435 1
9921 1
9921 2

Is this possible in Access?

The context is a project management database. Each project has a unique ID.
Projects and their descriptions are stored in one table. Progress updates
are stored in a second table. I would like to be able to incrementally
identify the updates to for each project.

Thanks in advance for any help!

--Pete
  #2  
Old August 18th, 2009, 10:43 PM posted to microsoft.public.access.tablesdbdesign
John W. Vinson
external usenet poster
 
Posts: 18,261
Default Composite Primary Key with an ID and Index

On Tue, 18 Aug 2009 13:31:01 -0700, Pete
wrote:

I would like to create a composite primary key using two fields: a random
integer ID, and a "line number" index. I want the index to be incremental,
start with 1, and reset for each unique ID from the first field. For example:

ID INDEX
5127 1
5127 2
5127 3
6435 1
9921 1
9921 2

Is this possible in Access?

The context is a project management database. Each project has a unique ID.
Projects and their descriptions are stored in one table. Progress updates
are stored in a second table. I would like to be able to incrementally
identify the updates to for each project.

Thanks in advance for any help!

--Pete


The simplest way to do this is in VBA code on a Form (bound to the projects
table) with a Subform (bound to the updates table). In the subform's
BeforeInsert event use code like

Private Sub Form_BeforeInsert(Cancel as Integer)
Me![INDEX] = NZ(DMax("[INDEX]", "ProgressTable", "[ID] = " & Me!ID)) + 1
Me.Dirty = False ' immediately write the record to disk
End Sub

--

John W. Vinson [MVP]
  #3  
Old August 20th, 2009, 07:07 PM posted to microsoft.public.access.tablesdbdesign
Pete
external usenet poster
 
Posts: 384
Default Composite Primary Key with an ID and Index

I'll give it a try -- thank you very much!

"John W. Vinson" wrote:

On Tue, 18 Aug 2009 13:31:01 -0700, Pete
wrote:

I would like to create a composite primary key using two fields: a random
integer ID, and a "line number" index. I want the index to be incremental,
start with 1, and reset for each unique ID from the first field. For example:

ID INDEX
5127 1
5127 2
5127 3
6435 1
9921 1
9921 2

Is this possible in Access?

The context is a project management database. Each project has a unique ID.
Projects and their descriptions are stored in one table. Progress updates
are stored in a second table. I would like to be able to incrementally
identify the updates to for each project.

Thanks in advance for any help!

--Pete


The simplest way to do this is in VBA code on a Form (bound to the projects
table) with a Subform (bound to the updates table). In the subform's
BeforeInsert event use code like

Private Sub Form_BeforeInsert(Cancel as Integer)
Me![INDEX] = NZ(DMax("[INDEX]", "ProgressTable", "[ID] = " & Me!ID)) + 1
Me.Dirty = False ' immediately write the record to disk
End Sub

--

John W. Vinson [MVP]

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 04:48 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.