oli merge said:
Hello,
basically I have a table of addresses with a field for a company name and
a
field for an individual name. several records may have the same company.
I want to select only the first three records for records that have the
same
company.
How can I go about this?
I would also like to have another field which adds a sequence number based
on how many duplicates are in the company field (i.e. If I had 4 records
with
the company as "McDonalds", I would like to have another field which
basically numbers them from 1-4)
Thanks then!
You have described a spreadsheet. Storing data redundantly is not the best
way in a relational database such as Access. For one thing, if the company
name changes, you need to change the company name for several records rather
than one.
It sounds as if you are describing contacts for a company. In a handwritten
address book you would probably have a company name, followed by a list of
contacts related to that company. That's how to do it in Access, too. You
need a Company table, and a Contacts table (tblCompany and tblContact).
tblCompany
CompanyID (primary key, or PK)
CompanyName, etc.
tblContact
ContactID (PK)
CompanyID (foreign key, or FK)
FirstName
LastName
Phone
etc.
If CompanyID in tblCompany is autonumber, CompanyID in tblContact must by
Number (Long Integer); otherwise, both fields must be the same Data Type as
defined in table design view.
Click Tools > Relationships. Add both tables. Drag CompanyID from one
table to another. Click Enforce Referential Integrity. Close the
Relationship window.
Create a form (frmCompany) based on tblCompany, and another (fsubContact)
based on tblContact. Set the Default View of tblContact to Continuous. To
do that, open the table in design view, and click View > Properties. This
opens the Property Sheet. (To see the properties for a control such as a
text box, click the text box while the Property Sheet is open.) Back to the
form's Property Sheet, click the Format tab, and set the Default View.
With frmCompany open in design view, drag the fsubContact icon onto
frmCompany. Click the border of fsubContact, and again click View >
Properties if the Property Sheet isn't still open. Verify that you are
looking at the properties for the Subform/Subreport (at the top of the
Property Sheet). Click the Data tab, and verify that the Link Child and
Link Master Fields are set to CompanyID.
Now when you add a contact, the contact is associated with a particular
company.
Back to your original question, if you want to see the first four records,
you need to decide what counts as "first". The first one you entered? The
person with the highest rank? Once you have decided that, create a query
based on the form, and sort by that field. Open the query in design view,
and click View > Properties. Set the Top Values property to 4.
As for a sequence number, it is quite easy in a report, but rather more
complex in a query. In a report, set the control source of a text box to
=1, and set its Running Sum property to Over All (or maybe Over Group, if
you are grouping in the report). For a query, try a groups search. The
matter has been discussed extensively.