Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with auto number 2 tables

Newbie
 
Join Date: Aug 2009
Posts: 18
#1: Sep 1 '09
Making progress with my data base but have run into a snag.

Working with an unbound form that place data into more than one table using a "save" button with this code.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Commandsaverecord_Click()
  2.  
  3.     Set db = CurrentDb
  4. Set rst = db.OpenRecordset("Table1")
  5. With rst
  6. .AddNew
  7. !DATE = Me.DATE
  8. !SHIFT = Me.SHIFT
  9. !SUPERVISOR = Me.SUPERVISOR
  10. !CATEGORY = Me.CATEGORY
  11. !MedicalAttention = Me.MedicalAttention
  12. !ActionTaken = Me.ActionTaken
  13. !Other = Me.Other
  14. .Update
  15. End With
  16. rst.Close
  17. Set db = CurrentDb
  18. Set rst = db.OpenRecordset("Tbl_Staff1")
  19. With rst
  20. .AddNew
  21. !Staff1 = Me.Staff1
  22. .Update
  23. End With
  24. rst.Close
  25.  
  26. End Sub
Works great.

My problem is that each table has key field that is auto numbered. I have a one to one relationship with the ID numbers. How do I get them to have the same ID #?

Expert
 
Join Date: Jul 2009
Location: KY
Posts: 253
#2: Sep 1 '09

re: Problem with auto number 2 tables


You cannot create a relationship between 2 tables where they are related to each others by auto numbers. Auto numbers have no regard to what is in another table.
In my opinion an auto number should only be used as a PK, not as a FK.
What you need to do is write the data to the primary table then grab the ID and insert it into the second table with the data you need.

Let me know if you need any help with this.
-AJ
Expert
 
Join Date: Jul 2008
Location: Maryland
Posts: 1,176
#3: Sep 1 '09

re: Problem with auto number 2 tables


In this case, both your tables can't use autonumbered fields as ID numbers because they will be out of sync. You can get the number assigned to the new record in Table1, and specifically insert it into the ID field in Tbl_Staff1, or you can use Long Int for the ID field in both tables, and find your own next ID number with a function like DMAX("ID", "Table1") + 1.
Newbie
 
Join Date: Aug 2009
Posts: 18
#4: Sep 1 '09

re: Problem with auto number 2 tables


ChipR

How do I do this. The relationship must be maintained
Expert
 
Join Date: Jul 2009
Location: KY
Posts: 253
#5: Sep 1 '09

re: Problem with auto number 2 tables


You create a FK in your Tbl_Staff1 table that has a 1-1 or 1- many relationship to your first table, while assigning the Tbl_Staff1 table it's on primary key. The relationship will be maintained while also helping to normalize the tables.

-AJ
Newbie
 
Join Date: Aug 2009
Posts: 18
#6: Sep 2 '09

re: Problem with auto number 2 tables


I can't get this to work. Do i need to add some code n my on-Click to add the id from my primary table to other tables so I can keep the relationship?
Newbie
 
Join Date: Aug 2009
Posts: 18
#7: Sep 3 '09

re: Problem with auto number 2 tables


I keep playing with this and can not get the autonumber from Table1 to propagate to other tables. I know that this must be simple, I must be too simple
Expert
 
Join Date: Jul 2009
Location: KY
Posts: 253
#8: Sep 8 '09

re: Problem with auto number 2 tables


Please post a sanitized version of your DB, and I will have a look at it. I need to see the big picture of what you are trying to accomplish.

-AJ
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#9: Sep 8 '09

re: Problem with auto number 2 tables


What would be better than a database is a description of your data, and possibly a question explained more clearly.

There should be no need for a database for something as simply defined as this.
Reply