Fraz:
If you want a flexible search form which allows you to optionally select
multiple criteria, i.e. you might select a company, a contact, a postcode or
any of these in combination (or even none of them to return all records),
then the easiest way is to create query which references the controls on the
search form as parameters.
Create an unbound search form, frmSearch say, and add unbound combo boxes
for selecting a company, contact or postcode. Dave Hargis has shown you how
to do this for companies. Contacts would be similar, e.g.
RowSource: SELECT ContactID, FirstName & " " & LastName FROM Contacts
ORDER BY LastName, FirstName;
BoundColum: 1
ColumnCount: 2
ColumnWidths 0cm;8cm
If your units of measurement are imperial rather than metric Access will
automatically convert the last one. The important thing is that the first
dimension is zero to hide the first column and that the second is at least as
wide as the combo box.
For postcodes it would be slightly different as I don't imagine you have a
separate postcodes table, but just a column in the Companies table, so it
would be set up like this:
RowSource: SELECT DISTINCT PostCode FROM Companies ORDER BY PostCode;
BoundColum: 1
ColumnCount: 1
Now create the query, e.g.
SELECT Company, AddressLine1, AddressLine2, City, PostCode,
FirstName, LastName
FROM Companies INNER JOIN Contacts
ON Contacts.CompanyID = Companies,CompanyID
WHERE (Companies.CompanyID = Forms!frmSearch!cboCompany
OR Forms!frmSearch!cboCompany IS NULL)
AND (ContactID = Forms!frmSearch!cboContact
OR Forms!frmSearch!cboContact IS NULL)
AND (PostCode = Forms!frmSearch!cboPostCode
OR Forms!frmSearch!cboPostCode IS NULL);
Note how each OR operation is parenthesised in the above. That's important
as it forces each to evaluate independently of the AND operations. This is
what makes the combo boxes on the search form optional. You can set the
query up in design view if you wish and then switch to SQL view and add the
WHERE clause.
Base a form and/or report on this query. Add a button, or buttons, to the
search form to open the form and/or report based on the query. You can now
select from any combination of the combo boxes optionally and click the
button to open the form or report.
For your other reports concentrate on creating queries to return the correct
data from the relevant tables. Once you have the queries creating the
reports is simple; in fact the report wizard can do all the work for you.
Ken Sheridan
Stafford, England