Finding, Selecting, and Ordering Records

Description

Alpha Anywhere provides a variety of tools for finding, selecting, and ordering records, that let you easily perform powerful and complex searches, and group records into lists that match your specified criteria. In this lesson, you learn how to find, select, and order records, and how to use indexes and queries.

Key Terms and Concepts

The following table describes some of the key terms and concepts in record selecting and ordering.

  • Ordering

    Arranging records according to the values in their fields, in a sequence that you specify. Ordering is also referred to as sorting.

  • Order Criteria

    Parameters you specify to sort records.

  • Selecting

    Choosing particular records with fields that match criteria that you specify, and excluding the records that do not match (filtering out). Selecting is also referred to as filtering.

  • Selection Criteria

    Parameters you specify for selecting records.

  • Filter Expression

    An expression used to specify record matching criteria. You can enter filter expressions manually or create them using the Filter Builder, Query Genie, or Expression Builder.

  • Key

    A value by which a record can be identified.

Ordering Records

When you add records to a table, they are stored in the order in which you entered them, and are assigned a unique, sequential record number. When you first open a table browse or form, the default order of the records is the record number order. You can change the order in which the records appear, but not the order in which they are stored. Queries, filters, and sorting are three ways to specify changes in the display order. You can save and reuse display order changes. Refer to the Sorting and Selecting Overview. You often use ordering and filtering together, resulting in an ordered list of selected records, that you can use in a report, promotional mailing, monthly billing, or other project. When you view, print, or process records, Alpha Anywhere uses this ordered list of selected records, called a record list. When you open a form or browse, the record display is the most recently used record list, if you have not set the form or browse filter or order properties.

Filtering Records

Filtering involves matching record field data to a specified criteria, and creating a list of the matching records. You can restrict your view of table data by filtering out some records. This process does not change the data stored in the table. Records that are filtered out are just temporarily hidden from view. You can filter records without ordering them. This produces an unordered list of the records in the table that match the filter criteria you specify.

Indexes

Alpha Anywhere uses indexes to determine the order in which records appear in a table, and to quickly find records. An index is a table that contains keys representing every record in the table. The keys are stored in order. By looking up a key in the index, Alpha Anywhere can quickly find the corresponding record in the table. Refer to the Overview of Indexes and Queries. Typically, when you enter records in a table, you do not enter them in any particular order. For example, the records in an imaginary Sales_Rep table are in a random order.

  • Sales_Rep Table

    images/atable3.png
  • If the table is indexed on the Name field, Alpha Anywhere creates a separate index in which the entries are ordered alphabetically, by name. A table can have several different indexes, each on different fields. All of the indexes are physically stored in a single CDX file.

  • Name Index

    images/atable4.png
  • In the Name index, the entries are sorted by name; each entry points to the corresponding record in the table. You can think of the index as a separate, sorted table with just two fields: Key and Record Number. When Name is the current index, the table records appear sorted by name, though they are not physically in this order in the table. When you add a new record, Alpha Anywhere adds it at the end of the table. The record's key is also added to the index, but in the index, it is added in the correct (sorted) location. For example, if you add Mr. Black from San Diego, the Sales_Rep table and the Name index appear as follows.

  • Sales_Rep Table

    images/atable5.png
  • Name Index

    images/atable6.png
  • Because Alpha Anywhere maintains the index in sorted order, the Name index always appears ordered by name. Alpha Anywhere updates the index any time you add or change a record.

Using Keys and Key Expressions

The entries in the index are called keys. A key can be a single field or you can create the key from several fields. For example, to see the Sales_Rep table, ordered by State, and within State, ordered by City, you create an index on State+City. In this example, the key is constructed by concatenating the State and City fields.

  • Index

    images/atable7.png
  • The State+City index is called a multi-level index. You can use the Index Builder to create a key expression with multiple fields. Keys and key expressions are the basis for indexes and queries.

Indexing and Querying Records

Using an index, you can quickly locate a particular record in a table. For example, if you do not have a Name index, and you want to find the record for Mr. Black, Alpha Anywhere would have to examine all the records in the table, until it reached the last record, Mr. Black. In a table with very many records, this would take a long time. When you use the index, finding Mr. Black's record is very fast, because the Name index is a much smaller table; it has only two fields, the key and record number. Alpha Anywhere searches the smaller index table much faster. This advantage increases with the size of the main table. Once Black is found in the index, Alpha Anywhere quickly locates the corresponding record in the Sales_Rep table, by using the index record pointer. By creating an index, you speed up queries and other operations that use the indexed fields. Indexed fields are fields that are commonly used to find data in a table. However, creating indexes on every field is likely to slow down data entry, as Alpha Anywhere updates each index whenever you enter or change a record. Two ways that Alpha Anywhere creates an ordered list of selected records is by using indexes and queries.

  • Index

    You define an index by creating a key expression in the Index Builder. Alpha Anywhere builds the index after you define it. Alpha Anywhere maintains and updates the index, adjusting its ordered list of selected records, when you add, change, or delete records.

  • Query

    You define a query by entering field names and expressions in the Query Genie. You run the query, producing an ordered list of selected records, called a query list. You can save the query definition for reuse. If the record data changes, you must re-run the query to update the query list.

Query Lists

Query lists and indexes are similar, in that they both contain a sorted list of key values. However, Alpha Anywhere updates indexes when you add or change records; query lists are static, and become out of date when you add and change records. To update a query list, you must refresh the current query, or generate a new query list by re-running the query. In the following procedures, you learn how to select and order records, and to use queries and indexes.

Query Lists

Query lists and indexes are similar, in that they both contain a sorted list of key values. However, Alpha Anywhere updates indexes when you add or change records; query lists are static, and become out of date when you add and change records. To update a query list, you must refresh the current query, or generate a new query list by re-running the query. In the following procedures, you learn how to select and order records, and to use queries and indexes.

Finding Records

Alpha Anywhere has many options for finding, selecting, and ordering records. In the following lessons, you use several of them. You can quickly access Alpha Anywhere searching and sorting capabilities using the Form View Toolbar and Browse View Toolbar buttons.

images/View_Toolbar_Commented.gif

Using Find Text

The Find > Find Text... command displays the Locate dialog, which allows you to search sequentially through field data for records containing matching text, numbers, dates, or a series of characters that you specify. The search criteria you specify does not need to match exactly; you can match word fragments. For example, if you search for the word data, Locate returns both data and database. When finding records, Alpha Anywhere returns the first match in the record list. In the next procedure, you are looking for Mr. Smith's customer record. To use Find Text:

  1. Double-click the Customer table in the AlphaSports Control Panel, to open the Default Browse.

  2. Select Find > Find Text... from the menu bar, or press CTRL+L. The Locate dialog box appears.

    • You have the option to search in All Fields or only the Current Field, and whether to search Forwards or Backwards.

    images/Locate_dialog_box.gif
  3. Keep the default options: All Fields and Forwards.

  4. Type "smith" in the Search For text box, and click Find. Alpha Anywhere highlights the first record it finds for a customer containing the name "smith", which is "James Smithson".

  5. Click Find again, to locate another customer record with the name, Smith. Alpha Anywhere highlights the record for "Robert Smith".

Using Find by Key

The Find > Find by Key... command displays the Find by Key dialog, which allows you to search for a record whose key most closely matches the key value you specify. For example, in a table with records ordered by Lastname, you could enter the letter "s", and Alpha Anywhere returns the first record in which the Lastname field begins with an "s". In this example, the key value for which you are searching is "s", and the key is Lastname. In the next procedure, you use Find by Key to locate Mr. Smith's customer record. In this example, the key value for which you are searching is "Smith"; the key is Lastname. To use Find by Key :

  1. With the Customer table Default Browse open, select , Find > Find by Key..., or press CTRL+K. The Find by Key dialog box appears.

    • images/Find_by_Key_button.gif
    • images/Find_by_Key_dialog_box.gif
    • Next to the Search By button, the dialog box shows the current key.

  2. Click the Search By button. The Select Index/Query or Field dialog box appears.

    • images/indexQueryField.gif
    • The Index/Query List tab lists the defined indexes and last four queries run. as you select each index, Alpha Anywhere displays its key expression. The expression for the Lastname index is Lastname+Firstname. The expression for the Lastname1 index is Lastname.

  3. Select the Fields tab. The list of fields in the table appears.

    • images/findKeyFields.gif
    • You can select any field and Alpha Anywhere orders the data in that field. This lets you perform a Find by Key, using the selected field as the key.

  4. Select "Lastname" from the list of fields, and click OK. Lastname appears next to the Search By button.

  5. Type the letter "s" in the Search For text box.

  6. Alpha Anywhere fills in the last name, "Smallwood".

  7. Next to the first letter "s", type the letters "m" and "i". Alpha Anywhere fills in the last name "Smith".

    images/findKeyS.gif
  8. Click Find.

  9. The first record with the last name "Smith" appears at the top of the browse.

Locating records using Find Text uses a method of searching records sequentially, to find records that matches criteria you specify, similar to the Find command present in many word processors. Using Find by Key is much faster than Find Text (when an appropriate index exists), particularly when a table has very many records.

Sorting Records

The AlphaSports Customer table Default Browse shows the customer records in record number order. You want to view the records ordered by state, so that the customers in the same state are grouped together. You can order records by their field values using single and multiple sorting techniques. When you issue a sort command, Alpha Anywhere uses an index, if one exists, or a query, to order the records. The Alpha Anywhere Status Bar, at the bottom of the screen, shows you which one is being used. To sort customer records:

  1. Open the Default Browse for the AlphaSports Customer table.

  2. Place the cursor in the Bill State/Region field of any record, and click the Ascending Sort button, or select Query > Ascending Sort from menu bar. Alpha Anywhere orders the records alphabetically, using the state field values.

    images/Sort_Ascending_button.gif
    images/Quick_Sort.gif

The Status Bar shows that Alpha Anywhere used a query.

images/UG_Status_Bar.gif

To sort the records on more than one field:

  1. Select Query > Multi-field Sort. The Custom Sort dialog box appears.

  2. Select Sort On: "Bill_state_region", then, sort on: "Bill_city", and click OK.

    images/sortMulti.gif

Alpha Anywhere sorts the table data by state first, then by city within each state.

  • Filtering Records

    When you filter records, you block from view the records that do not match the criteria you specify.

  • Showing All Records (Clearing a Filter)

    After applying a filter in a form or browse, clicking the Show All button on the toolbar will re-display all the records. If a filter is in effect, the Show All button is enabled. After clicking Show All, the button becomes unavailable (dimmed).

    images/Show_All_button.gif
images/Sorted_Browse.gif

Using Quick Filter

You use Quick Filter when you want to quickly find records in a form or browse with matching field data. In a multi-sorting procedure you might sort customer records alphabetically, according to city and state. Here you will create a record list with only customers from MA. To use Quick Filter :

  1. Place your cursor in any Bill_state_region field with a "MA" value, and click Quick Filter or select Query > Quick Filter from the menu. Alpha Anywhere displays only records containing a "MA" field value, and filters out the other records.

    images/Quick_Filter_button.gif
    If you perform a consecutive Quick Filter, Alpha Anywhere filters records within the current filter result.
  2. Place your cursor in a Bill_city field with a "Boston" value, and click . Alpha Anywhere displays only the records that have "Boston" and "MA", as the Bill_city and Bill_state_region field values.

    images/Quick_Filter_button.gif
  3. To revert to the default record order and cancel the filters, click the following button on the toolbar. Alpha Anywhere displays the records in their default order. If you have not set an order criteria in the layout, the default order will be record number order.

    images/Show_All_button.gif

Using Multi-field Quick Filter

You can use filters to include records, exclude records, and specify that records match any or all of the selected criteria. To use Multi-field Quick Filter :

  1. In the Customer table Default Browse, place your cursor in the first customer record.

  2. Select Query > Multi-field Quick Filter. The Multi-field Quick Filter dialog box appears.

    images/filterMulti.gif
  3. Select the following fields on which to filter records: Bill_city, and Bill_state_region. Alpha Anywhere displays the field values of the selected record.

  4. Select the following matching criteria "Find records that match ANY condition" in the drop-down list box.

    images/quickFilterMulti.gif
  5. Click OK.

  6. Alpha Anywhere filters records according to the criteria you specify.

  7. When you are done viewing the records, click to view all the records.

    images/Show_All_button.gif

In the next section, you learn to create queries that you can save, edit, and reuse.

Querying Records

Alpha Anywhere gives you several ways to create query expressions for ordering and filtering records. The Query by Form Helper and the Query Genie provide selection dialogs with which you can build query expressions. Advanced users can define queries by entering filter and order expressions directly in the Expression Builder.

Using Query by Form

Query by Form lets you specify search and sort criteria in the same form that you use for viewing and entering data. Alpha Anywhere determines which records appear and their order, and generates a query list based on your criteria. You can use Query by Form with default and custom forms. In the next procedure, you use the AlphaSports Customer Information form to select customers from "Cambridge, MA" and "Boston, MA", and order them by last name. To use Query by Form :

  1. Double-click the Customer Information form in the AlphaSports Control Panel, found on the Forms tab. The Customer Information form appears.

  2. Click the Query by Form button on the toolbar, or select Query > Query by Form. A blank Customer Information form, the Query by Form (QBF) Helper, and the Query by Form toolbar appear.

    images/Query_by_Form_button.gif
    images/queryByForm.gif
  3. In the blank form, enter "Cambridge" in the City field, and "MA" in the State field.

    images/queryByFormCambridge.gif
  4. Click the Next Page button on the query toolbar. A new, blank Customer Information form appears.

  5. Enter "Boston" in the City field and "MA" in the State field.

  6. To specify order criteria, select Query > Order Criteria. Buttons appear next to each field on the form.

    • Click the button next to the Lastname field. The number one and an upward arrow appear.

      images/queryByFormAscending.gif
    • This indicates that the Lastname field is the primary field on which to sort the records, and they will be sorted in ascending order.

  7. Click the Save Query button on the toolbar.

  8. Enter the name "QueryQBF" in the Save Query As dialog box, and click OK.

  9. Click Run Query! or press F9. The Query by Form status shows the records selected by the query. Picture

    images/QBFstatus.gif
  10. Use the PageDown and PageUp keys to view the records, or press F8, to view them in a browse.

  11. When you are done viewing the records, click to view all the records.

    images/Show_All_button.gif

Using the Order Criteria View

Using Query by Form, you can select up to five fields for record ordering. You specify the record ordering criteria in Order Criteria view, where number boxes appear next to the Form fields. To specify order criteria:

  1. With the Customer Information form open, click the Query by Form button on the toolbar.

  2. Click the Order Criteria 'A..Z' button, on the Query by Form toolbar.

    images/UG_Query_Toolbar.gif
  3. To select additional fields, click on subsequent order number boxes for other fields, to assign the numbers 2, 3, 4, and 5, indicating the hierarchy for ordering records. The arrows next to the numbers specify ascending (up arrow) or descending (down arrow) order.

  4. Click the Clear Criteria button at any time to reset ordering.

Using the Query by Form Helper

Query by Form has a special syntax and using the QBF Helper helps you learn it. The QBF Helper is a modeless dialog box that you use to create query expressions. You can use the QBF Helper on any of the form fields. Use the Genie button on the Query by Form toolbar to toggle its appearance on and off. To use the QBF Helper :

  1. In the Customer Information form, click in the State field, and click the Is Between button in the QBF Helper. The QBF Helper lets you define up to five selection criteria.

  2. Enter "CA" as the starting value and "MA" as the ending value, and click OK. Alpha Anywhere fills in the State field with "CA..MA".

  3. Click the Run button on the QBF Helper and view the results.

  4. Use the QBF Helper to find records with State values that contain characters "A" and "M". Alpha Anywhere fills in the State field with "*a*, *m*".

  5. Try using the QBF Helper to find a city that sounds like "New".

Using Inclusive and Exclusive Queries

To search for records that match your search criteria, click the '=' icon Include Records button on the Query by Form toolbar. When run, the query returns only records that contain the search criteria. To search for records that do not match your search criteria, click the Exclude Records button on the toolbar. When run, the query returns only records that do not contain the search criteria.

Saving Queries

To save a query, define your search criteria, and click the Save button on the Query by Form toolbar. Alpha Anywhere prompts you to name the query, and saves it. Saved queries appear on the Operations tab in the Control Panel. To open a previously saved query, click the Open button on the toolbar. Alpha Anywhere prompts you for the location of the query, and opens it.

Updating Query Lists

After you run Query by Form, the query list you generate remains active until you specify otherwise. It is important to remember that a query list is static, and becomes out of date when you add and change records. When a query list becomes out of date, you can do the following:

  • Refresh the current query by pressing F5, or by selecting Form> Refresh Display or Browse > Refresh Display.

  • Display all records in the default order, by selecting Query > Show All.

  • Generate a new query list by running another query.

Using the Query Genie

The Query Genie is another, easy-to-use method for creating queries. You can use the Query Genie to create one-time searches or run saved searches. In addition, the Query Genie lets you perform additive queries. For example, if you run a query and are viewing the returned records, and you decide to run another query using the Query Genie. A pop-up menu appears allowing you to specify whether Alpha Anywhere searches in all available records, or just within the current selection of records. You access the Query Genie from a form or browse, by selecting Query > Query Genie, or by clicking the 'lamp' icon Query Genie button on the toolbar. To create a query using the Query Genie :

  1. Open the Customer table Default Browse.

  2. Click the 'lamp' on the toolbar. The Query Genie appears.

    images/Genie_button.gif
  3. Select the "Bill_city" field, the condition "is equal to", and click the 'up' icon to display the upper right pop-up list box. The Value dialog box appears.

  4. Select the Select an existing value from the field option, and click the pop-up list box. The list box displays the Bill_city field values.

    images/List_Button_Blue_Up.gif
  5. Select the Select an existing value from the field option, and click the pop-up list box. The list box displays the Bill_city field values.

    images/queryGenieValue18.gif
  6. Click the 'A..Z' sort button, to see the fields listed in ascending alphabetical order.

    images/Sort_button.gif
  7. Select "Boston" and click OK.

  8. Select "Boston" and click OK.

  9. In the pop-up box below Boston, select "Cambridge" and click OK.

    images/UG_Query_Genie.gif
  10. Display the Order By tab, and select "Sort By Lastname".

  11. Click the Show Expression button. The Filter/Order Expressions window appears.

    • images/Preview_Button.gif
      images/filterOrderExpress.gif
    • You see the filter and order expressions you created in the Query Genie.

  12. Click Close.

  13. Click the Switch from Genie Mode to Expression Mode button. A message appears warning you that once you convert your filter to an expression, you can no longer edit it using the Genie.

  14. Click OK. The Filter/Order Expressions window appears displaying the filter and order expressions. Advanced users can use the Expression Builder to construct additional filter and order expressions.

  15. Click Run Query > Search All Records.

  16. Click to save the query as "QueryQG" and click OK.

    images/Save_As_button.gif
  17. Click the Rollup Window button to reduce the Genie window, and move it out of the way. The record list appears with customers from Boston and Cambridge, listed in ascending alphabetical order, by last name.

    images/Rollup_button.gif
    images/queryGenieResult.gif
  18. Click Show All to display all the records.

    images/Show_All_button.gif

Running a Saved Query

In the next procedure, you run a saved query and compare record lists. To run a saved query:

  1. Click the Expand Query Genie button, and click the Load a Saved Query button at the bottom of the Genie window. The Saved Queries dialog box appears.

    images/Folder_button.gif
  2. You select the saved query that you created using Query By Form. Select "QueryQBF" and click OK. Alpha Anywhere displays the filter and order expressions.

    • When you load a query created using Query By Form, Alpha Anywhere displays the query in Expression Mode. When you load a query created using the Query Genie, Alpha Anywhere displays the query in Genie Mode.

    images/GS_Query_Saved.gif
  3. Click Run Query. Alpha Anywhere returns the same record list of customers from Boston and Cambridge, listed in ascending alphabetical order, by last name.

Using Query by Expression

If you are an advanced user, you might prefer to create expressions for querying records, by typing them directly in the Query by Expression dialog box. To use Query by Expression :

  1. Click the Query by Expression button on the toolbar. The Query by Expression dialog box appears.

    • The following table describes the dialog box options.

      • Filter expression

        Determines the records to display. Enter an expression, or click 'xy' to open the Expression Builder.

      • Order expression

        Determines the order in which to display the filtered records. Enter an expression, or click to open the Expression Builder.

      • Sub-select

        Select an option to display a portion of the filtered records.

      • Duplicates

        Select whether to allow or disallow records with duplicate sort keys.

    images/Query_by_Expression_button.gif
    images/UG_Query_By_Expression.gif
  2. Enter (or build) the following filter expression in the Expression Builder : Bill_city = "Boston" .OR. Bill_city = "Cambridge".

    images/espressionBuilderBosCamb.gif
  3. Click OK.

  4. In the Order Expression enter Lastname.

    images/QBEBosCamb.gif
  5. Click Run.

  6. The same record list appears, with customers from Boston and Cambridge, listed in ascending alphabetical order by last name.

  7. Click Current Query. Alpha Anywhere saves the query as the current, active query.

Using Current Query

You can use the Current Query button to see the current filter and order expression. If you close and re-open a browse or form, the record list is controlled by the current query. Alpha Anywhere displays the last used query list or index, if you have not set the form or browse filter or order properties. When a form or browse is in Design Mode, you can access the Properties window by pressing F12. The Show All Records button turns off the current query and shows all the records in the table, if the form or browse filter or order properties were not set in the Properties window.

images/Show_All_button.gif

Summary

In this lesson, you learned about finding, selecting, and ordering records. You learned how to change the record display order for use when viewing records. You used quick sorting and filtering techniques, as well as techniques using multiple sorting and filtering options. You learned that finding records sequentially versus finding records using an indexed list, results in substantial performance differences, particularly when you have a large number of records in a table. You learned about query lists and indexes. You learned how to create and save queries, using Query by Form, Query by Expression, and by using the Query Genie. You saw that all queries have underlying filter and order expressions, whether you create them using Genie Mode or Expression Mode. You learned that when you run a query, Alpha Anywhere selects an existing index (if an index definition matches the query definition) or builds a new query list. You also learned that you must update query lists when you add or change records, whereas, Alpha Anywhere updates your indexes, whenever you change or add a record. See Creating a Database to learn how to create a new database, and specify the fields that define your database.

See Also