473,511 Members | 15,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using A Foreign Key's Value in a Table's OrderBy Property

twinnyfo
3,653 Recognized Expert Moderator Specialist
Here is a handy trick you might have a need for as you expand your projects. Forgive the length, as it can get complex. However, the solution is simple!

I have a Table, we will call it tblPeople:

Expand|Select|Wrap|Line Numbers
  1. Field     Type        Description
  2. ID        AutoNumber  PK
  3. FullName  Text        Person's Name (Last, First MI)
  4. Other fields, etc.
Properly Normalized, all that jazz.... Because FullName is one of the Text Fields, we can sort by the person's Name (should we so desire), and many times we do, depending on what we are doing. It is usually in the Order By somewhere, whenever we pull data including the name. The FullName is spit out by the MDSS (Master Data System in the Sky) and we have no control over it, so it is what it is, and since it begins with the Last Name first, it is good for sorting.

SOME (I say again, SOME) of these people have circumstances which pertain only to them. So, for proper DB normalization, we have a separate table for just these people. We will call it tblGoodPeople:

Expand|Select|Wrap|Line Numbers
  1. Field   Type        Description
  2. ID      AutoNumber  PK
  3. Person  Long        FK to tblPeople
  4. Other fields, etc.
Now, if you were to have tblGoodPeople's OrderBy Property set to [tblGoodPeople].[Person], because this field is a long integer, it will sort the records based on the value of the FK. So, your names may not be sorted properly (so you could actually find someone easily if you had to look at the table).

In tblGoodPeople, I use a LookUp field, RowSourceType = Table/Query and RowSource =

Expand|Select|Wrap|Line Numbers
  1. SELECT tblPeople.ID, tblPeople.FullName
  2. FROM tblPeople
  3. ORDER BY tblPeople.FullName;
By doing this, the Person field in tblGoodPeople is now displayed as the person's name. Again, if the OrderBy Property is set to Person, it will still sort by the numerical value of the field.

However, there is a neat feature available when you have foreign keys and you have a lookup field. Here is an example.

All you have to do is use the Prefix "Lookup_" and indicate the Lookup Field that you want to use (assuming you had multiple fields to sort on). Thus, I set my OrderBy Property to:

Expand|Select|Wrap|Line Numbers
  1. Lookup_Person.FullName
because the Field Person could have the values of either ID or FullName.

And my table now sorts by the person's name!

There is nothing ground-breaking in this insight and many of the Experts here may have already known about this trick. However, for some of the younger Jedis, this little secret about the Force is given to help out.

Enjoy this little tidbit of MS Access trickery.....
Mar 9 '15 #1
0 9121

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2981
by: Vinodh Kumar P | last post by:
I understand the number of foreign keys allowed is restricted by the DBMS I use. In a general relational schema design perspective how many foreign keys a table shall have? If I have large number...
26
14080
by: pb648174 | last post by:
I have a table called BidItem which has another table called BidAddendum related to it by foreign key. I have another table called BidFolder which is related to both BidItem and BidAddendum, based...
10
17751
by: D. Dante Lorenso | last post by:
I'd like to run a clean up command on my tables to eliminate rows that I'm no longer using in the database. I want to do something like this: DELETE FROM tablename WHERE...
6
2646
by: Tony | last post by:
Hi, I'm still new to this so if I'm sounding dumb or my premise is flawed please forgive me. I have a DB design which contains a table which has categories, each category has a parent category,...
5
3307
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
9
3892
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for...
2
2962
by: Chris | last post by:
I have a database column that stores a comma delimited list of foreign keys. Would someone show me how to do a join using the values from a list stored within a record? For example, a record in ...
1
10979
by: apax999 | last post by:
Kinda new to SQL, using SQL Server 2005. I have some foreign keys in a couple of tables. I need to drop these tables, but can't since I'll get the error: Msg 3726, Level 16, State 1, Line...
9
4595
AMT India
by: AMT India | last post by:
I am using mysql 4.2. I have a big database with lots of data. Now there is no foreign key relationship between the tables. But every one has a primary key. Can I alter these tables to bring foreign...
2
3558
JnrJnr
by: JnrJnr | last post by:
I have two SQL database tables. One contains various products(with unique primary keys) and the other contains information related to the products aswel as the product's foreign keys. What I want...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.