PC Review


Reply
Thread Tools Rate Thread

Access 2003 Create New DB Index but keep referential integrity

 
 
FrankSpokane
Guest
Posts: n/a
 
      15th Apr 2009
We have two tables linked by a Client ID field which we thought we wanted to
have as an indexed text field, so we could assign a specific number to a
customer, and to transactions.

In the Master table a customer number assigned might be 1000, 1001, etc.
The linking field is indexed text not allowing duplicates
687 records

In the transaction table the transaction number logic assigned would be
1000-1 1000-2 1000-3 etc. for each transaction,
The linking field is non-indexed text allowing duplicates
13418 records

This has not worked out at all for us, with multiple de errors, orphan
records, etc.

Is there a way to re-create this index and not lose the transaction detail ?
We would like the Customer Number to be assigned sequentially starting with 1.



 
Reply With Quote
 
 
 
 
KARL DEWEY
Guest
Posts: n/a
 
      15th Apr 2009
What about using two fields - customer and transactions. Then create an
index using both and set to unique (No Duplicates).

"FrankSpokane" wrote:

> We have two tables linked by a Client ID field which we thought we wanted to
> have as an indexed text field, so we could assign a specific number to a
> customer, and to transactions.
>
> In the Master table a customer number assigned might be 1000, 1001, etc.
> The linking field is indexed text not allowing duplicates
> 687 records
>
> In the transaction table the transaction number logic assigned would be
> 1000-1 1000-2 1000-3 etc. for each transaction,
> The linking field is non-indexed text allowing duplicates
> 13418 records
>
> This has not worked out at all for us, with multiple de errors, orphan
> records, etc.
>
> Is there a way to re-create this index and not lose the transaction detail ?
> We would like the Customer Number to be assigned sequentially starting with 1.
>
>
>

 
Reply With Quote
 
Ken Sheridan
Guest
Posts: n/a
 
      16th Apr 2009


"FrankSpokane" wrote:

> We have two tables linked by a Client ID field which we thought we wanted to
> have as an indexed text field, so we could assign a specific number to a
> customer, and to transactions.
>
> In the Master table a customer number assigned might be 1000, 1001, etc.
> The linking field is indexed text not allowing duplicates
> 687 records
>
> In the transaction table the transaction number logic assigned would be
> 1000-1 1000-2 1000-3 etc. for each transaction,
> The linking field is non-indexed text allowing duplicates
> 13418 records
>
> This has not worked out at all for us, with multiple de errors, orphan
> records, etc.
>
> Is there a way to re-create this index and not lose the transaction detail ?
> We would like the Customer Number to be assigned sequentially starting with 1.
>
>
>

 
Reply With Quote
 
Ken Sheridan
Guest
Posts: n/a
 
      16th Apr 2009
Sorry about the empty reply.

Tables are related by having a foreign key column (field) in one (The
Transactions table in your case) reference the primary key column in the
other (Master in your case). Client ID is the primary key of Master, so you
also need A Client ID column in Transactions, in the latter case indexed
non-uniquely (duplicates allowed). This should be separate from the
Transaction Number column which can be numbered from 1 upwards for each
Client ID. You should therefore also create a unique index on the ClientID
and Transaction Number columns, jointly on both columns that is, not
individually.

Rather than text it would be better to make these columns number data types
as this makes it easier to generate the numbers automatically as described
below.

A relationship between the tables should be created on the Client ID column
and referential integrity enforced. Cascade Updates should also be enforced
so that if for any reason a Client ID value in Master should be changed the
matching rows in Transactions would be updated automatically.

To generate the Client ID numbers in master automatically when a new
customer is added this can be done in the data entry form bound to the Master
table by putting the following code in the form's BeforeInsert event
procedure. This assumes that the data type has been changed to a number in
table design:

Me.[Client ID] = Nz(DMax("[Client ID]", "[Master]"),0) + 1

This looks up the highest existing value of Client ID and adds 1. If the
table is empty, the DMax method returns Null, so the Nz function converts
this to a zero, and the number for the first row thus becomes 1.

To automatically assign the correct Client ID value to a new row in
Transactions the usual approach would be to include a subform based on the
Transactions table in the form based on the Master table, linking them on
Client ID by making this the LinkMasterFields and LinkChildFields properties
of the subform control.

To assign the next sequential number per Client ID to the Transaction Number
column in Transactions when entering a new transaction record would be
similar to generating the Client ID for a new customer, but in this case the
DMax function needs a criterion limiting it to the transactions for the
current Client ID, so the the code for the subform's BeforeInsert event
procedure would be:

Dim strCriteria As String

strCriteria = "[Client ID] = " & Me.[Client ID]

Me.[Transaction Number] = _
Nz(DMax("[Transaction Number]", "[Transactions]", strCriteria),0) + 1

Using the DMax method like this is fine in a single-user environment, but
can give rise to conflicts in a multi-user environment on a network if two or
more users are adding new customer or transaction records simultaneously.
You'll find a simple solution to this by Roger Carlson at:


http://www.rogersaccesslibrary.com/f...e51989c53d7ffb


or a more complex one by me, which also allows the next number to be used to
be reset, at:


http://community.netscape.com/n/pfx/...g=ws-msdevapps


Although storing the Client ID and Transaction Number in two separate
columns in the Transactions table, you can of course always return them as a
single value in a computed column in a query or a compute control in a form
or report using the expression [Client ID] & "-" & [Transaction Number]

It goes without saying that before you start amending your tables in this
way its imperative to back up the database.

Ken Sheridan
Stafford, England

"FrankSpokane" wrote:

> We have two tables linked by a Client ID field which we thought we wanted to
> have as an indexed text field, so we could assign a specific number to a
> customer, and to transactions.
>
> In the Master table a customer number assigned might be 1000, 1001, etc.
> The linking field is indexed text not allowing duplicates
> 687 records
>
> In the transaction table the transaction number logic assigned would be
> 1000-1 1000-2 1000-3 etc. for each transaction,
> The linking field is non-indexed text allowing duplicates
> 13418 records
>
> This has not worked out at all for us, with multiple de errors, orphan
> records, etc.
>
> Is there a way to re-create this index and not lose the transaction detail ?
> We would like the Customer Number to be assigned sequentially starting with 1.
>
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Access not enforcing referential integrity!!! Danny Microsoft Access Database Table Design 11 16th Apr 2010 07:57 PM
Referential Integrity in CREATE TABLE command =?Utf-8?B?dXNlcjAx?= Microsoft Access 6 26th Sep 2007 11:01 AM
How Can I Create Referential Integrity with Dataset.XMLReader?? Workaholic Microsoft VB .NET 3 7th Aug 2007 11:06 AM
create relationships between two tables without referential integrity Yor Microsoft Access Database Table Design 3 13th Nov 2003 06:53 PM
create the relationship without referential integrity using the microsoft jet sql Yor Microsoft Access Queries 1 12th Nov 2003 08:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:36 PM.