How to update foreign keys in a table.

mahenkj2

Registered User.
Local time
Today, 13:03
Joined
Apr 20, 2012
Messages
459
Dear All,

I have made a small database and used unique text as primary key and so far dB is working fine. I have gone through some other post on this site and that says choosing text as primary key should not be an issue neither the max size of 255 for text is an issue. Irrespective of preferred "Long integer" requirement for a primary key. Anyway, before going to my database at large scale, I want to ensure that this thing does not affect my dB in future.

Now, even I can use the autonumber as my primary key and i do have any problem in understanding generating foreign key in related tables, but I am struggling on how to populate foreign keys automatically. It seems a very basic thing for making a sound database but at the moment it is a big issue for me. Please advise.
 
Your Main Table contains the Primary Key.
Your next table contains the Foregin Key.

These should be Joined in the Relationships View.

Then a Form would have a Header from the main Table with a Sub Form from the second Table.

These should also be joined in the Properties box of the Main Form. Look for Master Child.

Then when you create a new record in the Sub Form the Foregin Key will be automatically populated.
 
If you know how to use an autonumber as a PK, I'm not sure why you wouldn't want to. If you have a business rule that requires uniqueness for some other attribute or combination of attributes, then create a unique index to enforce the business rule.

That said, I occassionally use text PKs but I would NEVER use a 255 character PK!!! That's way too long. You are not just taking up all that room in the main table but also in the child tables. Surely, you can make it shorter. Twenty characters would be my outer limit. If you can't limit it to 20 characters or less, change to an autonumber.
 
Many thanks for all the supports.

In fact, I am very new to Access but gaining knowledge rapidly due to very prompt responses and information at this website.

I have made new database by using autonumber as PK and now I have 7 master tables and I use information from these tables to one table by using combo boxes. I took one parent table in main form and child table in sub form and could see that foreign key for that parent table is updating automatically. But, what if I want all 7 FK to be updated by this form. In this case, I tried making manually combo boxes from parent tables (not by wizard) and then in subform I entered date and time stamps of entry. This time all FK got updated.

My question, I have nothing left in my subform other than date and time. Is that normal and is this the only way to solve this issue.
 
The Question is why would you update the other Tables when you are not adding information.

I would venture to say that the design of your tables is incorrect.

Can you post your Database with some dummy data. Just the Tables will do.

Or at least post a J peg of your relationships. But the Database would be better.

Please convert back to 2003 as not everyone has the later versions.
 
Last edited:
I forgot to mention that the Combo boxes may be a problem. Are they in Tables or Forms.

If they are in Tables then they should be removed. They are misleading. I can post a link about this if you wish.
 
May be I am doing totally wrong. In that case, I have attached my dB. Kindly have a look and advise your comments.

at the moment, I could do in accdb. If 2003 is must, please revert.

best regards.
 

Attachments

I can't do anything with the Version that you posted.

I need it in 2003. Can you please convert and reattach.
 
Forget the Form Sub Form suggestion.

Do change in the Table the Combo Box to Text Box.

With the Form Change the Control source to the Table MR. Nothing else but include all Fields.

Fix the form by selecting the correct Control Source.

Finally Fix the Row Source.

You may not know how to do all this so I will fix it for you then Post for you.

More to Come.
 
Many thanks for your kind co-operation.

In fact, I am putting efforts with the help of a book by "Paul McFedries" and this site. Still, may be weak on basics. If you have any recommendation on step by step settings of this kind of things, kindly advise.

best regards.
 
I changed a few things. Hope you can work it out.

Please ask about things that concern you.
 

Attachments

I have gone through the revised dB attached by you and made as per my requirement. I have following queries which I request you to reply upon:

1. I was practicing making provision of combo box in table's look up wizard. In that case, when we create a form based on that table, all field of combo appears in drop down automatically. But as you said, it should not be like that. Please explain how it affects?

2. Also, I have to edit all label's caption manually (since includes FK). Is this OK?

3. Now, my MR table includes date and time stamp with all foreign key fields. Earlier, I was making reports based on this table, but now should I make a query first with all the desired fields and then make a report based on that query. Is this the standard way?

regards.
 
Naming Conventions.

Why use a Naming Convention?
Using a naming convention when creating your Access Database is vital. A good naming convention will help stop errors that can occur due to badly named objects and will make the initial development quicker and easier. The naming convention will also make life easier when revisiting a database to add new functionality, which maybe in a few months or even years time.
The following rules for naming objects in your Access Database should always be observed. These apply to the naming of everything within your database from tables, queries, forms, reports, macros and VBA modules to field names within tables and controls within forms and reports: (not following rules 1 and 2 can cause errors within your database, the other rules are optional but very highly recommended)

1 Do Not Use Spaces or Special Characters
If you have never done any type of development or programming before you may be tempted to use spaces or special characters when naming database objects. This can make it difficult (or produce errors) when referring to them in queries or VBA. The use of spaces or special characters will end up causing you problems. Always avoid them when naming objects in your database.
Special characters / \ | @ £ ^ ( ) [ ] { } ; : ! # & = * + - ? " ' $ %

2 Do Not Use Reserved Words
Reserved words are words that are reserved for use by built in Access functionality or SQL functionality. The most common reserved words that new developers use when naming database objects are: Date, Day, Month, Year; so for example a developer may have a field in the Sales table called ‘Date’ this should be avoided and should be called something like ‘SaleDate’.

3 Keep Names Short yet Informative
A table or query etc… needs to have a name that is informative but not too long or too short. For example a query named ‘qryListOfSalesForEachDepotGroupedByMonth’ should be called something like ‘qryMonthlySalesByDepot’ and should definitely not be called ‘qryMSD’.

4 Use Title Case also known as CamelHump
A query named ‘qryMonthlySalesByDepot’ is easier to read than a name written in all lower or upper case i.e. ‘QRYMONTHLYSALESBYDEPOT’ or ‘qrymonthlysalesbydepot’

5 Use Name Prefixes
The following are a list of prefixes and example names to use when naming objects. By using a prefix you can easily distinguish between different object types that have the same name. I.e. if you have a table named ‘Staff’ and a report named ‘Staff’ it is difficult to tell which is which, so name them ‘tblStaff’ and ‘rptStaff’.
Another key reason we name our tables and queries with a prefix is that when you are working with these two object types in the query window, Microsoft Access simply lists all tables and queries together without differentiating them. By using the tbl and qry prefixes on these two objects, it always ensures that the tables are shown separately from. queries
.Other objects that need to be named include the bound and unbound controls found on forms and reports. When you create a form or report using the wizard or AutoForm or AutoReport each of the controls are named the same as the field names. Now whilst this is by MS Access default, it is not really an acceptable way of naming controls. One of the key reasons we don't want to do this is that sometimes when we are working with forms and reports we want to refer to the control rather than the field. By having the field names the same as the object names, you can often have a situation where the wrong control is referred to. The way we overcome this, is by naming our controls based on the control type. For example if our field was called Postcode and the control we are using for this field is a text box, then we would name the text box txtPostcode and the label lblPostcode. Note how we refer to text boxes as a control. It is not a field as many think. There are no fields in Reports and Forms. They are controls.

HIERARCHY OBJECTS
Tables tbl
Queries qry
Forms frm
Reports rpt
Macros mcr
Modules mod, mdl or bas

TABLES.
Tables are usually plural and are preceded with tbl
e.g. tblClients
tblSales
tblCities

QUERIES
Queries are preceded with qry
e.g. qryClients
qrySales
qryCities

For greater clarity one can describe what the query is used for and also to gather like types together.

eg. qryFrmClients Main Form
qryFrmSubClients Sub Form
qryRptClients Report
qryRptSubClients Sub Report

Notice the use of CamelHump. This will sort the queries into like groups and also distinguish a query from a table.

Forms follow the same convention and so do Reports.

Eg. frmClients Main Form
frmSubClients Sub Form
rptClients Report
rptSubClients Sub Report

Macros are preceded with mcr
Modules are preceded with mdl or mod. Some programmers of old use bas

Naming Fields in Tables. Usually singular
City
SaleDate
FirstName
LastName
Adding prefixes can become confusing and really don’t add any value.

Primary Keys and Foreign Keys.
Foreign Keys should share the same name with the Primary Key.
The Primary Key name should be unique within the Database.
The Primary Key should be tagged as the Primary Key and the Foreign Key should be tagged as the Foreign Key.
e.g. ClientPK as Primary Key
ClientFK as Foreign Key.
The commonly used suffix of ID serves little except to confuse.

Naming Fields in Queries.

Queries use the table’s field name.
Calculated Fields need naming and should observe the basic naming convention of short, simple and descriptive.

Form and Report Control Objects.

Text Boxes txt
Labels lbl
Command Buttons cmd
Combo Boxes cbo

Toggle Buttons tgb
Option Buttons opb
Option Groups opg
List Boxes lst
Images img
Unbound Object Frame uof
Bound Object Frame bof
Page Break pgb
Tab Controls tab
Sub Forms sbf
Sub Reports sbr
Lines lin
Rectangles rct


Variables
Strings str
OLE object types ole
Integers int
Decimal dec
Dates dte

Module Objects
Subroutines sub
Private Functions fn
Public Functions pfn

Using the naming standards shown above will assure that if you do require assistance from a professional database developer that they will take you far more seriously than before because you have shown a professional understanding on how to name your database object. This will certainly give you credibility in the eyes of professional database developers.
 
Last edited:
I have gone through the revised dB attached by you and made as per my requirement. I have following queries which I request you to reply upon:

1. I was practicing making provision of combo box in table's look up wizard. In that case, when we create a form based on that table, all field of combo appears in drop down automatically. But as you said, it should not be like that. Please explain how it affects?

2. Also, I have to edit all label's caption manually (since includes FK). Is this OK?

3. Now, my MR table includes date and time stamp with all foreign key fields. Earlier, I was making reports based on this table, but now should I make a query first with all the desired fields and then make a report based on that query. Is this the standard way?

regards.

I believe I have answered question 1 with the Link to "The Evils of LookUps"

Question 2. Yes you have to rename all the controls on Forms and Reports Manually. MS Access can only supply a basic default naming convention which is not suitable for serious programers.

Question 3. Based upon the information contained in your sample Database I redesigned. If you need something explained please ask. Try to make your question clear and use an example were warranted.

Both Forms and Reports usually contain information from more than one table. You can Join Tables together by using a query or an SQL statement. Queries are easier and can display graphically the result of you enquiry, including Filters and Sorting.
 
Last edited:
There is one other subject I have not touched on.

That of Normalisation.

Have you read up on this as yet. If not let me know and I will post a good quality link.
 
1. I was practicing making provision of combo box in table's look up wizard. In that case, when we create a form based on that table, all field of combo appears in drop down automatically. But as you said, it should not be like that. Please explain how it affects?
It is important to follow the link to understand why experienced developers recommend against them. The bottom line is they fail to work properly too much of the time. What was left unsaid though was that lookups on FORMS are a good thing. They reduce typing and minimize data entry errors. The warnings against lookups ONLY apply to using lookups that you define in a table. The only "good" thing that table level lookups do for you is to tell Access to automatically create a combobox control when you create a form based on that table. Given that the number of forms you create based on a table will probably be one and the number of queries you create will probably be many. Give up the 1-side benefit and go for the many:)

To convert a textbox to a combo on a form, right-click and choose "change to".
 
Many thanks for informing about naming conventions. Since I am not a developer and just creating a database for my requirement. I will try to adopt as much as possible.

I could also understood (probably) about the look ups in tables, at least I can avoid these as much as possible. I can change control type in a form manually. To avoid some probable issues in future, it should not be a big deal.
 

Users who are viewing this thread

Back
Top Bottom