views:

122

answers:

2

I have a database in access 2007 accdb extension , there are more or less 30-40 tables with related primary key "local_number". it is a text primary key with 10 lenght.

How can I change the length of this primary key to 30 WITHOUT delete previosly all the 30 relationship.

  1. A similar question. I need add a compose primary key to my PK "local_number". I need Add a "Date" as composed PK. Then access just allow this if I first delete all relationships.

How can Avoid this warning and change my PK ignoring this message.

Reason of compose PK in each table is requirement of a historical data change in records.

update, Someone know if this is possible or i hace to delete all relationships?

+1  A: 

you can use access database object model and VBA code that will automatically

  1. add new fields in corresponding tables (both PKs and FKs)
  2. Based on the already existing relations, update all FK values with corresponding PK values,
  3. Break the existing relations
  4. Create the new relations

To know which relations have to be replaced, you can browse your database objects.

This code could inspire you (written on the fly/may be buggy/check corresponding help):

for i = 0 to mydatabase.Relations.count - 1
    Debug.Print mydatabase.relations(i).name
    for j = 0 to myDatabase.relations(i).fields.count -1
        debug.print myDatabase.relations(i).fields(j).name
    next j
next i
Philippe Grondier
thanks, could you explain more the 2 step. And in the 3 and 4 step, that means that every realation that i need modify, i have to delete and recreate this?
MaK
step 2: suppose that you have Tbl_Main, where PK is id_Main, and Tbl_Child, where PK is id_Child and where id_Main is a Foreign Key. You will then create a field id_MainNew both in Tbl_Main and Tbl_Child. Using the existing relation, you will update Tbl_Child.id_MainNew with Tbl_main.id_MainNew value.In step 3, the relation existing between Tbl_Main and Tbl_Child and based on id_Main is deleted.In step 4, a new relation is created between Tbl_Main.id_MainNew and Tbl_Child.id_MainNew
Philippe Grondier
ok that is clear. thanks
MaK
+1  A: 
  1. Make a copy of the database with existing relationships.

  2. Delete all the relationships in the original copy.

  3. Then use code to copy the relationships from the backup to the original copy.

I'm assuming you retain the same name for the PK field that's being edited, but even so, you can alter the code from the MS Knowledge Base at the cited URL to account for that.

David-W-Fenton
Thanks for solution, really really helps and save time.
MaK
Hi david, I retain the same name for PK, but the PK index suffer changes. Now i don´t need this field with index.but after I change this option to No index or to The indexed property of a field was changed from Yes (Duplicates OK), the relationships didn't copy from original file.But I don't have idea how modify that code. the error seems to be in this line "ThisDb.Relations.Append ThisRel"Can you help me modifying the code to allow copying relations after index change.
MaK
What is the error that occurs on that line? Likely you've got duplicate records in one of the PK fields, so you won't be able to recreate the relationship.
David-W-Fenton
the error appears because I add a date as compose primary key, then i need add in the code from microsoft some lines to add this field to an existant relation. thanks for your help. that link is very very useful.
MaK