Solved

How can I update foreign key in child table

Posted on 2002-07-18
6
217 Views
Last Modified: 2012-05-04
I have tables called "tblCustomers" and "tblOrders". tblCustomers has a autoNumbered primary key tblCustomers.CustID which linked to foreign key tblOrders.CustID. These CustIDs are hidden from user (not on forms).

When I add new record to tblOrders, how can I update the foreign key with primary key value. It seems form doesnt provide any function to syncronise both key. There is no obvious code to update foreigh key in class module when I look into sample database.

I just dont know how to do that. Please advise.


0
Comment
Question by:mmiya
  • 3
  • 2
6 Comments
 

Expert Comment

by:emeel
Comment Utility
You could define constraints in your SQL Server database on the used tables. This constraints will not allow you to use a foreign key if there is no corresponding primary key. Also, you wont be able to delete a primary key if there is still a corresponding foreign key.
Adding or modifying records that violate on these constraints, will result in an error in your interface. You could test on the error codes/description and give an appropiate warning to the end-user.
good luck
0
 

Author Comment

by:mmiya
Comment Utility
Thanks for the advice emeel.

What I'm asing is that is there any way to update foreign key implictly. MS Access takes the data mostly from forms. And it will be added to the corresponding table implicitly (without writing code) when you moved the pointer or close the form. But doing it implicitly, is seems that all data must be on the form.

What I'm looking for is that when I added new record to the "tblOrder", how can I copy primary key value to the foreign key of the added record IMPLICITLY.

Thanks
0
 
LVL 7

Expert Comment

by:ildc
Comment Utility
Hi,

There are two ways to do this :

1. Create a main form for your customers and put a subform (by using the subform wizard) into this main form. The two forms will be linked with a master anc child linkfields, and the child linkfield is filled in automatically.

2. If you only want to have a order form, you only have to put a combobox showing all customers on that form and whilest your making the order, you'll only have to select the right customer. (therefore you can use also a wizard, but don't forget to state in the last step of the wizard "store this value in this field : select your foreign key)

If you need help, just give a buzz...

Regards
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 7

Accepted Solution

by:
ildc earned 50 total points
Comment Utility
BTW,

If you do not like emeel's answer, just reject it.


Emeel, it is policy on this forum that all questions are answered with a comment, and not with an answer because this blocks the question and puts it in a section that is often visited by other experts.

Regards
0
 

Author Comment

by:mmiya
Comment Utility
Thanks ildc for your comment. I was away from my PC for a while. My apology for delay in respond.

I think I can work out with subform's link chiled/parent fields.

Regards
0
 
LVL 7

Expert Comment

by:ildc
Comment Utility
Glad to be of assistance.

(Hope y had a nice ?Holiday?)

Regards
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now