So far, we were passing the name of a table or query to a Recordset object. Here is an example: Private Sub cmdGetEmployees_Click() Dim dbKoloBank As Object Dim rstEmployees As Object Set dbKoloBank = CurrentDb Set rstEmployees = dbKoloBank.OpenRecordset("Employees") Set rstEmployees = Nothing Set curDatabase = Nothing End Sub Instead of passing a table or a query by name to the OpenRecordset() method, you can pass a SELECT statement. Here is an example: Private Sub cmdGetEmployees_Click() Dim dbKoloBank As Object Dim rstEmployees As Object Set dbKoloBank = CurrentDb Set rstEmployees = dbKoloBank.OpenRecordset("SELECT * FROM Employees;") Set rstEmployees = Nothing Set curDatabase = Nothing End Sub Of course, you can list only the fields you want in the record set. Here is an example: Private Sub cmdGetEmployees_Click() Dim dbKoloBank As Object Dim rstEmployees As Object Set dbKoloBank = CurrentDb Set rstEmployees = _ dbKoloBank.OpenRecordset("SELECT FirstName, LastName FROM Employees;") Set rstEmployees = Nothing Set curDatabase = Nothing End Sub With this technique, only the indicated columns would be considered in the Recordset object. The value of each of the columns produced can then be assigned to a control on a form or report. Here is an example: Private Sub cmdGetEmployee_Click()
On Error GoTo cmdFindEmployee_Error
Dim dbExercise As Database
Dim rsEmployees As Recordset
Set dbExercise = CurrentDb
Set rsEmployees = _
dbExercise.OpenRecordset("SELECT EmployeeNumber, " & _
" DateHired, " & _
" FirstName, " & _
" LastName, " & _
" HourlySalary, " & _
" DepartmentID, " & _
" EmploymentStatus " & _
"FROM Employees")
txtEmployeeNumber = rsEmployees("EmployeeNumber").Value
txtDateHired = rsEmployees("DateHired").Value
txtFirstName = rsEmployees("FirstName").Value
txtLastName = rsEmployees("LastName").Value
txtHourlySalary = rsEmployees("HourlySalary").Value
cbxDepartments = rsEmployees("DepartmentID").Value
cbxEmploymentsStatus = rsEmployees("EmploymentStatus").Value
Exit Sub
cmdFindEmployee_Error:
MsgBox "There was a problem when processing the operation. Please report the error as" & vbCrLf & _
"Error #: " & Err.Number & vbCrLf & _
"Description: " & Err.Description
End Sub
We know different ways of creating and using simple SELECT statements. Using fields from a table, we can create a SELECT statement and use it to generate a query. In Microsoft Access Object Library, a query is based on a class named QueryDef. The group of queries of a database is stored in a collection named QueryDefs. If/When you create a new query, it is added as the new item in the collection. This means that the queries are cumulative and each can be located either by its name or its index in the collection. To let you create a new query in the Microsoft Access Object Library, the Database class provides a method named CreateQueryDef. Its syntax is: Function expression.CreateQueryDef(Name, SQLText) As QueryDef expression can be a variable of type Database, previously declared, and that references either the current or an existing database. Both arguments are semi-optional. This means that you can call the method without these arguments and you would not receive an error, but it is not a good idea. After the CreateQueryDef() method has performed its action, it returns an object of type QueryDef that you can use as you see fit. For example, you can analyze of you can use it as the Record Source of a form or report. The first argument specifies the name of the query that will be created. If you call the method without this argument, the database engine would create a query in memory with an insignificant name (the name is made of random characters) and it would not show in the Navigation Pane. This means that it's a good idea to pass this argument. The name follows all the rules you know about naming objects in Microsoft Access. If a query exists already and you want to get its name, you can access the it using a read-only property named Name that belongs to the QueryDef class. Here is an example: Private Sub cmdGetQueryName_Click() Dim dbExercise As Database Dim qryEmployees As QueryDef ' Get a reference to the current database Set dbExercise = CurrentDb ' Get a reference to the third query in the queries collection Set qryEmployees = dbExercise.QueryDefs(2) MsgBox "Name of 3rd query: " & qryEmployees.Name End Sub The second argument also is optional. It represents the SQL statement that is the subject of the query. Here is an example: 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 FirstName, LastName FROM Employees;" ' Create a new query named EmployeesInfo Set qryEmployees = curDatabase.CreateQueryDef("EmployeesInfo", strStatement) End Sub If you don't pass the second argument, to let you formula the SQL statement, the QueryDef class is equipped with a string-based property named SQL. You can simply assign the desired statement to this property. Here is an example: Private Sub cmdCreateQuery_Click() Dim dbExercise As Database Dim qryEmployees As QueryDef ' Get a reference to the current database Set dbExercise = CurrentDb ' Create a new query named EmployeesInfo Set qryEmployees = dbExercise.CreateQueryDef("EmployeesInfo") qryEmployees.SQL = "SELECT FirstName, LastName FROM Employees;" End Sub
Data analysis is available in a record set. In fact, as opposed to the DoCmd.RunSQL() method, the record set presents some advantages. For example, if an error occurs when you execute a SQL statement in DoCmd.RunSQL, you may have difficulties finding out what type or error and why. On the other hand, the record set can give you some information about what is going on. As seen already, you can pass a SQL statement as argument to a record set. After opening the record set, it returns a list of records. This time, instead of visiting each field of the record set to check its name, you would know with certainty what fields are in the set. As you should know already, both the DoCmd.RunSQL() and the OpenRecordset() methods produce a list of records. One of the advantages that a record set presents over DoCmd.RunSQL() is that you can find out if the OpenRecordset() did not produce any record at all, which is difficult information to get with the former. When the OpenRecordset() method is called, if it produces an empty list, the database interpreter produces (throws) a 3021 error. This allows you to take appropriate actions.
As you may suspect, the record set relies on a SQL statement to get a list of records. In fact, we saw already that you could pass the name of a table or query to OpenRecordset(). In reality, this is equivalent to a SELECT * FROM ... statement. We also saw that you could provide a specific list of fields in the statement. When creating the SQL statement, you can provide a condition by which the necessary records would be isolated. Here is an example: Private Sub cmdGetEmployee_Click()
Dim dbExercise As Database
Dim rsEmployees As Recordset
Set dbExercise = CurrentDb
Set rsEmployees = _
dbExercise.OpenRecordset("SELECT EmployeeNumber, " & _
" DateHired, " & _
" FirstName, " & _
" LastName, " & _
" HourlySalary, " & _
" DepartmentID, " & _
" EmploymentStatus " & _
"FROM Employees " & _
"WHERE EmployeeNumber = '" & txtEmployeeNumber & "'")
With rsEmployees
txtDateHired = .Fields("DateHired").Value
txtFirstName = .Fields("FirstName").Value
txtLastName = .Fields("LastName").Value
txtHourlySalary = .Fields("HourlySalary").Value
cbxDepartments = .Fields("DepartmentID").Value
cbxEmploymentsStatus = .Fields("EmploymentStatus").Value
End With
End Sub
When creating the SQL statement, you can use everything you know about SQL to write a statement as effective as possible. The condition in the statment can use any of the Boolean or SQL operators you know already.
You can create a record set from a form that acts as the parent of a sub-form, populate that record set with records, and bind the record set to the sub-form. To make this possible, remember that every form has a property named Recordset and this property is of type Recordset. Instead of using the name of a table or query, you can create a SQL statement as complex as you want, pass it to the OpenRecordset() method of a Recordset object, then assign that record set to the subform.
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|