Most or all of the criteria we have specified with the WHERE keyword had to exactly match the specified criterion. In some cases, you may not remember the exact desired value of records but you want to specify some type of approximation. To do this, you use the LIKE operator. If you are visually creating the statement, in the Select Query window, click the Criteria box corresponding to the column on which the condition would be applied and type. In a SQL statement, the LIKE operator is used in a formula as follows: Expression LIKE pattern The Expression factor is the expression that will be evaluated. This must be a clear and valid expression. The pattern factor can be a value to be found in Expression. For example, it can be the same type of value used in a WHERE statement. In this case, the equal operator would be the same as LIKE. For example SELECT Employees.DateHired, Employees.FirstName, Employees.LastName, Employees.Department FROM Employees WHERE (((Employees.LastName)="Scott")); is equivalent to SELECT Employees.DateHired, Employees.FirstName, Employees.LastName, Employees.Department FROM Employees WHERE (((Employees.LastName) LIKE "Scott")); The idea of using a LIKE operator is to give an approximation of the type of result you want. The available wildcards to se with the LIKE operator are:
If you want to match any character, in any combination, for any length, use the * wildcard. If you precede it with a letter, as in S*, the condition would consist of finding any string that starts with S. Imagine that you want to create a list of employees whose first names start with H. You would type the condition as LIKE "H*" (or LIKE 'H*'). Here is an example Private Sub cmdUseJoin_Click() RecordSource = "SELECT Employees.EmplNbr, " & _ " Employees.FirstName, " & _ " Employees.LastName, " & _ " Departments.Department " & _ "FROM Employees " & _ "INNER JOIN Departments " & _ " ON Employees.DeptCode = Departments.DeptCode " & _ "WHERE Employees.FirstName LIKE 'H*';" txtEmployeeNumber.ControlSource = "Employees.EmplNbr" txtFirstName.ControlSource = "FirstName" txtLastName.ControlSource = "LastName" txtDepartment.ControlSource = "Department" End Sub This would produce:
To negate the condition, you can precede the criterion with NOT. That is type the NOT operator just after WHERE. Here is an example: Private Sub cmdUseJoin_Click() RecordSource = "SELECT Employees.EmplNbr, " & _ " Employees.FirstName, " & _ " Employees.LastName, " & _ " Departments.Department " & _ "FROM Employees " & _ "INNER JOIN Departments " & _ " ON Employees.DeptCode = Departments.DeptCode " & _ "WHERE NOT(Employees.FirstName LIKE 'H*');" txtEmployeeNumber.ControlSource = "Employees.EmplNbr" txtFirstName.ControlSource = "FirstName" txtLastName.ControlSource = "LastName" txtDepartment.ControlSource = "Department" End Sub This would produce:
As you can see, this results in the list of employees whose first names don't start with H. You can type the NOT operator before the LIKE expression. Here is an example: Private Sub cmdUseJoin_Click() RecordSource = "SELECT Employees.EmplNbr, " & _ " Employees.FirstName, " & _ " Employees.LastName, " & _ " Departments.Department " & _ "FROM Employees " & _ "INNER JOIN Departments " & _ " ON Employees.DeptCode = Departments.DeptCode " & _ "WHERE Employees.FirstName NOT LIKE 'H*';" txtEmployeeNumber.ControlSource = "Employees.EmplNbr" txtFirstName.ControlSource = "FirstName" txtLastName.ControlSource = "LastName" txtDepartment.ControlSource = "Department" End Sub When you precede the * character with a letter, only that letter would be considered. Alternatively, you can specify a group of characters that would precede the * symbol. For example, if you have some last names that start with San in a list but you don't remember the end of the name you are looking for, to create the list, you can specify that the first name would start with San and end with whatever. In this case, you would use San* as follows: The corresponding SQL statement is: Private Sub cmdUseJoin_Click() RecordSource = "SELECT Employees.EmplNbr, " & _ " Employees.FirstName, " & _ " Employees.LastName, " & _ " Departments.Department " & _ "FROM Employees " & _ "INNER JOIN Departments " & _ " ON Employees.DeptCode = Departments.DeptCode " & _ "WHERE Employees.FirstName LIKE 'Wall*';" txtEmployeeNumber.ControlSource = "Employees.EmplNbr" txtFirstName.ControlSource = "FirstName" txtLastName.ControlSource = "LastName" txtDepartment.ControlSource = "Department" End Sub This would produce:
Instead of ending a letter or a group of letters with *, you can begin the LIKE statement with *. An example would be LIKE "*on". In this case, all strings that end with on, such as Bastion or Cranston, would be considered. If you remember neither the beginning nor the end of a string you want to search for but you know a sub-string that is probably included in the type of string you are looking for, you can precede it with * and end it with *. An example would be LIKE "*er*". In this case, all strings that include er anywhere inside, such as Berg or Merck, would be considered. Like the other SQL statements, you can also negate this one.
The * wildcard is used to precede or succeed a specific character or a group of characters, that is, any character. If you want to consider only a range of characters from the alphabet, you can include the range in square brackets. To do this, type [, followed by the lowest character of the range, followed by -, followed by the highest character of the range, followed by ]. For example, to get a list of employees whose last names start with letters between E and H, you would specify the criterion as LIKE "[E-H]*". Here is an example: The SQL statement of this query is: Private Sub cmdUseJoin_Click() RecordSource = "SELECT Employees.EmplNbr, " & _ " Employees.FirstName, " & _ " Employees.LastName, " & _ " Departments.Department " & _ "FROM Employees " & _ "INNER JOIN Departments " & _ " ON Employees.DeptCode = Departments.DeptCode " & _ "WHERE Employees.FirstName LIKE '[K-N]*';" txtEmployeeNumber.ControlSource = "Employees.EmplNbr" txtFirstName.ControlSource = "FirstName" txtLastName.ControlSource = "LastName" txtDepartment.ControlSource = "Department" End Sub This would produce:
Once again, remember that you can negate this expression by preceding it with NOT.
Imagine that you know a certain pattern in the string but you are forgetting only one letter. For example, if you know that the employee's first name sounds like Saul, Haul, or Paul. In other words, you don't know for sure what the first letter is but you are sure about the other characters. To match a character in a string, you can use the ? symbol as the wild card. Here is an example: SELECT Employees.DateHired, Employees.FirstName, Employees.LastName, Employees.Department FROM Employees WHERE Employees.FirstName Like "?aul");
In the previous sections or lessons, we stated the conditions one at a time. This made their interpretation easier. Sometimes, you will need to test a condition that depends on another. Boolean algebra allows you to combine two conditions and use the result, or to test two conditions but consider if either is true. Suppose you get an assignment to create a database used for a company that rents apartments to customers. The operations would consist of registering customers who become tenants and allocating them an apartment based on their needs and the availability.
When you perform filtering on a form, it fires the On Filter event. When you apply the filter on a form, it fires an On Apply Filter event. Here is an example: Private Sub Form_Open(Cancel As Integer) Filter = "(PropertyTypeID = 1) AND (OccupiedVacant = 'Vacant')" FilterOn = True End Sub
To express the logical conjunction, the SQL uses the AND operator. To create a logical conjunction in SQL, type one condition on the left and the other condition on the right sides of the AND keyword using the following formula: SELECT WhatColumn(s) FROM WhatObject WHERE Condition1 AND Condition2 The WhatColumn(s) and the WhatObject factors are the same we have used so far. The AND keyword is the new one. Each condition is written as a SQL operation using the formula: Column operator Value In this case, the WHERE operator resembles the If conditional statement. The Condition1 is the first that would be examined. Remember that, from our discussion earlier, if the first condition is false, the whole statement is false and there is no reason to examine the second condition. If the first condition is true, then the second condition would be examined. Based on this, the SQL statement used to get a list of movies released in 1994 but rated PG-13 is: SELECT Title, Director, CopyrightYear, Rating FROM Videos WHERE CopyrightYear = "1994" AND Rating = "PG-13"; The equivalent SQL statement of the above query in SQL as written by Microsoft Access is: SELECT Videos.Title, Videos.Director, Videos.CopyrightYear, Videos.Rating FROM Videos WHERE (Videos.CopyrightYear)="1994") AND ((Videos.Rating)="PG-13");
A logical disjunction is used to check that either of two outcomes is true. The comparison is done using the OR operator.
If you have a logical range of values and you want to know if a certain value is contained in that range, you can use the BETWEEN operator. The BETWEEN operator is combined with AND to get a list of records between two values. The basic formula of this operator is: Expression BETWEEN Start AND End The Expression placeholder of our formula is usually the name of the column whose values you want to examine. The Start factor is the starting value of the range to consider. The End factor is the highest value to consider in the range. After this condition is executed, it produces the list of values between Start and End. Here is an example: SELECT Videos.Title, Videos.Director, Videos.CopyrightYear, Videos.Rating FROM Videos WHERE (Videos.CopyrightYear) Between 1994 And 2004;
If you have a series of records and want to find a record or a group of records among them, you can use the IN operator. The IN operator is a type of various OR operators. It follows this formula: IN(Expression1, Expression2, Expression_n) Each Expression factor can be one of the values of a column. This is equivalent to Expression1 OR Expression2 OR Expression3, etc. Here is an example that shows the list of movies directed by either Oliver Stone or Ron Howard: SELECT Videos.Title, Videos.Director, Videos.CopyrightYear, Videos.Rating FROM Videos WHERE (Videos.Director) In ("Oliver Stone","Ron Howard");
A parameter query is one that requests a value from the user and displays its result based on the user's choice. As its name implies, this query expects a parameter, like the arguments we reviewed for procedures. This means that, when creating such a query, you must prepare to display a request to the user. To create a parameterized query, you can use the BETWEEN, LIKE, NOT, or IN operators. For example, to let the user enter part of a name of a director, you could set the Criteria of the Director to: LIKE "*" & [A director name that includes] & "*" When the query runs, if the user enters a name such as Phillip, the list would include the 6th and the 12th videos. Instead of requesting just one value as a parameter, you can request more than one. To do this, you can use the BETWEEN operator that requests an additional AND. For example, to ask the user to specify a range of years whose videos you want to see, you would set the Criteria of a CopyrightYear to BETWEEN [Enter a starting year] AND [Enter an ending year] |
|
|||||||||||||||||||||||||||||||||||||||||||||
|