The Queries of a Database: Data Selection |
|
The main purpose of data filtering consists of selecting records. As you know already, records are stored in tables but they can be identified by the columns of a table. Therefore, before filtering records, you must first specify the columns that would be involved in your filtering. If you are creating a query using the wizard, first select a table (or an already created query): |
After specifying the table (or query) that holds the records, to select a column, in the Available Fields list, you can double-click the desired column(s):
The technique of selecting the fields is the same applied when creating a table using the Table Wizard. If you are creating a query in the Design View, when you are presented with the Show Table dialog box, to select a table, you can click it and click the Add button, or you can double-click the table:
If you have already selected a table and want to change it or want to add a table (or query), you can right-click the Select Query window and click Add Table. After specifying the table, to select a column, you can drag it from the table and drop it in a column in the lower section of the window:
Instead of adding one column at a time, you can select more than one column using the Ctrl or the Shift keys and dragging the selection to a column in the lower section of the window. To include all columns in a query, drag the * field and drop it in the bottom section. |
Practical Learning: Selecting Fields For a Query |
|
Data Selection Using SQL |
Data select in the SQL consists of using the SELECT keyword. The primary formula to follow is: SELECT What FROM WhatObject;
The What factor of our syntax is the name of the column(s) of a table. The WhatObject factor can be the name of a table (or a view). If you (visually) create a query, either using the Query Wizard or the Design View, the database engine would take care of creating the appropriate SQL statement for you. After saving the query, to view its statement, open the query in design view, right-click its window, and click SQL View. You can also access the SQL View option from the main menu under View. To select everything from a table, you can use the asterisk as the range of values. For example, to select all records, you would use a the statement as follows: SELECT * FROM Students; This example would select all records from a table named Students. As opposed to selecting all records, you can select one particular column whose fields you want to view. To do this, you can replace the What factor in the syntax with the name of the desired column. Here is an example: SELECT LastName FROM Students; This statement would select only the column that contains the last names. To consider more than one column in a statement, you can list them in the What factor of our syntax, separating them with a comma except for the last column. The syntax to use is: SELECT Column1, Column2, Column_n FROM WhatObject; As an example, to select the names, gender, and home phone of records from a table called Students, you would the following statement: SELECT FirstName, LastName, Gender, HomePhone FROM Students As mentioned earlier, to programmatically create a query, you can pass the SQL statement as the second argument of the CreateQueryDef() method. Here is an example that uses the Microsoft Access Library: Private Sub cmdCreateQuery_Click() Dim curDatabase As Object Dim qryEmployees As Object Dim strStatement As String ' Get a reference to the current database Set curDatabase = CurrentDb strStatement = "SELECT DateHired, FirstName, " & _ "LastName, Department FROM Employees;" ' Create a new query named EmployeesInfo Set qryEmployees = curDatabase.CreateQueryDef("EmployeesInfo", strStatement) End Sub You can use the same approach with DAO. |
Practical Learning: Viewing a SQL Statement From a Query |
|
Data Selection For a Form |
As mentioned previously, after creating and saving a query, you can use it a data source for a form. Instead of primarily creating a query, you can either type a SQL statement in the Record Source property or use its ellipsis button to create a query. Everything is done as seen above. |
Practical Learning: Selecting Data for a Form |
|
|
||
Previous | Copyright © 2005-2016, FunctionX | Next |
|