Saturday, October 03, 2015

Alphabetize by One Field or the Other

If one is missing, use the other


Let's say you have a database that has the company name and a contacts name.

In some cases the CompanyName field is empty. If that happens, you want to continue the alpha sort using the contact's LastName.

To do this, you need to create an extra query field to provide the sort, using the NZ() function to replace the contents of one field for null values in another.

(Nz(variant, [valueifnull])
  1. Select the Queries, and then click "Create query in Design view".
  2. Choose the table you want to sort, click Add.
  3. Click Close.
  4. Drag down all the fields you want to display in your form, including the two separate fields you want to alphabetize.
  5. Insert a new column on the left side of the QBE grid.
  6. In the Field cell, enter the expression
    NZ([CompanyName],[LastName])
  7. Select Ascending for the Sort option.

When you run the query, if CompanyName is null (empty — no entry), the NZ() function uses the contents in LastName instead.

Here's another way to do it:






See all Topics

No comments: