As you may know already, a table or a query contains a series of records. To perform some operations on the series, the records may have to be treated as a group. Such a group or series of records is called a record set. As you may know and as we will review later, one of the most common operations you will perform on a record set consist of navigating among the records. When doing this, the result you get depends on the type of record set. Based on their behaviors, there are two primary categories of record sets:
To support a group of records as a set, the DAO library provides the DAO.Recordset class. Therefore, the primary way to create a record set is to declare a variable of type DAO.Recordset. How you get the set of records depends on the technique you use. Once again, remember that the MAOL and DAO do things the same way. This means that the way you create a DAO record set is the same we saw with the MAOL:
After using a DAO.Recordset object, you should (strongly) close it. To close a record set, you can call its Close() method. Here is an example: Private Sub cmdCreateRecordSet_Click() Dim dbExercise As DAO.Database Dim rsCustomers As DAO.Recordset Dim tblCustomers As DAO.TableDef Set dbExercise = CurrentDb Set tblCustomers = dbExercise.TableDefs("Customers") Set rsCustomers = tblCustomers.OpenRecordset rsCustomers.Close dbExercise.Close Set dbExercise = Nothing End Sub After closing the record set, you should release the resources it was using and make them available to other applications that would need them. To do this, assign the Nothing value to the DAO.Recordset object. Here is an example: Private Sub cmdCreateRecordSet_Click() Dim dbExercise As DAO.Database Dim rsCustomers As DAO.Recordset Dim tblCustomers As DAO.TableDef Set dbExercise = CurrentDb Set tblCustomers = dbExercise.TableDefs("Customers") Set rsCustomers = tblCustomers.OpenRecordset rsCustomers.Close Set rsCustomers = Nothing dbExercise.Close Set dbExercise = Nothing End Sub
Before performing actions on a table, you first need to get a reference to that table, which is equivalent to opening a record set. We already saw different ways to do that, such as calling the OpenRecordset() method of the Database class. Remember that there are other ways to create or open a record set. Once you have the record set, you can perform data entry on it. This is done using the same steps for the MAOL. To start, call the AddNew() method of the Recordset class. Here is an example of calling it: Private Sub cmdCreateEmployee_Click() Dim dbExercise As DAO.Database Dim rsEmployees As DAO.Recordset Set dbExercise = CurrentDb Set rsEmployees = dbExercise.OpenRecordset("Employees") rsEmployees.AddNew dbExercise.Close Set dbExercise = Nothing End Sub To let you specify the value of a field, first access that field using either its index or its name applied to the Recordset variable. Use its Value property to assign the desired value. You can do this for the various fields whose values you want to create. To finalize the action, call the Update method of the Recordset class.
The way you provide a value for a field depends on its type. For example, the value of a text-based column must be provided in double-quotes. Here is an example: Private Sub cmdCreateEmployee_Click() Dim dbExercise As DAO.Database Dim rsEmployees As DAO.Recordset Set dbExercise = CurrentDb Set rsEmployees = dbExercise.OpenRecordset("Employees") rsEmployees.AddNew rsEmployees("EmployeeName").Value = "John Schwartz" rsEmployees.Update dbExercise.Close Set dbExercise = Nothing End Sub To specify value or a Boolean field, assign True or False to its Value property. Here is an example: Private Sub cmdCreateEmployee_Click()
Dim dbExercise As DAO.Database
Dim rsEmployees As DAO.Recordset
Set dbExercise = CurrentDb
Set rsEmployees = dbExercise.OpenRecordset("Employees")
rsEmployees.AddNew
rsEmployees("EmployeeName").Value = "John Schwartz"
rsEmployees("IsFullTime").Value = True
rsEmployees.Update
dbExercise.Close
Set dbExercise = Nothing
End Sub
To specify the value of a number-based field, simply assign the desired value to its Value property. Here is an example: Private Sub cmdCreateEmployee_Click()
Dim dbExercise As DAO.Database
Dim rsEmployees As DAO.Recordset
Set dbExercise = CurrentDb
Set rsEmployees = dbExercise.OpenRecordset("Employees")
rsEmployees.AddNew
rsEmployees("EmployeeNumber").Value = 92073
rsEmployees("EmployeeName").Value = "John Schwartz"
rsEmployees("IsFullTime").Value = True
rsEmployees.Update
dbExercise.Close
Set dbExercise = Nothing
End Sub
To perform data entry of date or time fields in DAO, create the date and/or time value between # and #, then assign it to the Value property of the DAO.Field object. Here is an example: Private Sub cmdCreateEmployee_Click()
Dim dbExercise As DAO.Database
Dim rsEmployees As DAO.Recordset
Set dbExercise = CurrentDb
Set rsEmployees = dbExercise.OpenRecordset("Employees")
rsEmployees.AddNew
rsEmployees("EmployeeNumber").Value = 92073
rsEmployees("DateHired").Value = #4/12/2010#
rsEmployees("EmployeeName").Value = "John Schwartz"
rsEmployees("IsFullTime").Value = True
rsEmployees.Update
dbExercise.Close
Set dbExercise = Nothing
End Sub
You can also provide the value in double-quotes. Here is an example: Private Sub cmdCreateEmployee_Click()
Dim dbExercise As DAO.Database
Dim rsEmployees As DAO.Recordset
Set dbExercise = CurrentDb
Set rsEmployees = dbExercise.OpenRecordset("Employees")
rsEmployees.AddNew
rsEmployees("EmployeeNumber").Value = 94055
rsEmployees("DateHired").Value = "10/05/2008"
rsEmployees("EmployeeName").Value = "Alain Binam"
rsEmployees("IsFullTime").Value = False
rsEmployees.Update
dbExercise.Close
Set dbExercise = Nothing
End Sub
The DAO library uses the same approach as the Microsoft Access Object Library to specify the number of characters of a text-based field. Therefore, when creating a text-based column, to specify the maximum number of characters it can have, pass a third argument to the DAO.TableDef.CreateField() method as a number. Here is an example: Private Sub cmdCreateTable_Click() Dim dbExercise As DAO.Database Dim tblEmployees As DAO.TableDef Dim fldEmploymentStatus As DAO.Field ' Specify the database to use Set dbExercise = CurrentDb ' Create a new TableDef object. Set tblEmployees = dbExercise.CreateTableDef("Employees") Set fldEmploymentStatus = _ tblEmployees.CreateField("EmploymentStatus", DB_TEXT, 80) tblEmployees.Fields.Append fldEmploymentStatus ' Add the new table to the database. dbExercise.TableDefs.Append tblEmployees dbExercise.Close Application.RefreshDatabaseWindow End Sub Private Sub cmdCreateEmployeesTable_Click() Dim dbFunDS As DAO.Database Dim colEmployee As DAO.Field Dim tblEmployees As DAO.TableDef Set dbFunDS = CurrentDb Set tblEmployees = dbFunDS.CreateTableDef("Employees") Set colEmployee = tblEmployees.CreateField("EmployeeNumber", dbLong) tblEmployees.Fields.Append colEmployee Set colEmployee = tblEmployees.CreateField("DateHired", dbDate) tblEmployees.Fields.Append colEmployee Set colEmployee = tblEmployees.CreateField("FirstName", dbText, 25) tblEmployees.Fields.Append colEmployee Set colEmployee = tblEmployees.CreateField("LastName", dbText, 25) tblEmployees.Fields.Append colEmployee Set colEmployee = tblEmployees.CreateField("FullName", dbText, 50) tblEmployees.Fields.Append colEmployee Set colEmployee = tblEmployees.CreateField("Title", dbText, 50) tblEmployees.Fields.Append colEmployee Set colEmployee = tblEmployees.CreateField("HourlySalary", dbDouble) tblEmployees.Fields.Append colEmployee Set colEmployee = tblEmployees.CreateField("Notes", dbMemo) tblEmployees.Fields.Append colEmployee dbFunDS.TableDefs.Append tblEmployees dbFunDS.Close Set dbFunDS = Nothing Application.RefreshDatabaseWindow End Sub
You can create an integer-based field whose values automatically increase with each new record. To support this, use the Attributes property of the DAO.Field class and apply the dbAutoIncrField constant to it. Here is an example: Private Sub cmdTable_Click()
Dim curDatabase As DAO.Database
Dim tblEmployees As DAO.TableDef
Dim colEmployeeID As Field
Dim colFullName As Field
Set curDatabase = CurrentDb
Set tblEmployees = curDatabase.CreateTableDef("Employees")
Set colEmployeeID = tblEmployees.CreateField("EmployeeID", dbLong)
colEmployeeID.Attributes = dbAutoIncrField
tblEmployees.Fields.Append colEmployeeID
Set colFullName = tblEmployees.CreateField("FullName", dbText)
tblEmployees.Fields.Append colFullName
curDatabase.TableDefs.Append tblEmployees
MsgBox "A table named Employees has been created"
curDatabase.Close
Set curDatabase = Nothing
Application.RefreshDatabaseWindow
End Sub
After creating the field, when performing data entry, remember that you will not provide a value for the auto-incrementing field.
In the DAO library, the DAO.Field class is equipped with the DefaultValue property. To specify a default value for a new field you are creating, assign that value to it. Here is an example: Private Sub cmdCreateTable_Click() Dim dbExercise As DAO.Database Dim tblEmployees As DAO.TableDef Dim fldEmployeeNumber As DAO.Field Dim fldEmployeeName As DAO.Field Dim fldEmploymentStatus As DAO.Field ' Specify the database to use Set dbExercise = CurrentDb ' Create a new TableDef object. Set tblEmployees = dbExercise.CreateTableDef("Employees") Set fldEmployeeNumber = tblEmployees.CreateField("EmployeeNumber", DB_TEXT, 10) fldEmployeeNumber.Required = True tblEmployees.Fields.Append fldEmployeeNumber Set fldEmployeeName = tblEmployees.CreateField("EmployeeName", DB_TEXT, 100) tblEmployees.Fields.Append fldEmployeeName Set fldEmploymentStatus = _ tblEmployees.CreateField("EmploymentStatus", DB_TEXT, 20) fldEmploymentStatus.DefaultValue = "Full Time" tblEmployees.Fields.Append fldEmploymentStatus ' Add the new table to the database. dbExercise.TableDefs.Append tblEmployees dbExercise.Close Set dbExercise = Nothing Application.RefreshDatabaseWindow End Sub Once a field has a default value, it can be skipped during data entry, in which case the default value would be used for its value.
In the DAO library, the Field class provides the Required property. You can use this property to allow the user to skip a field during data entry. You can also use this property to make sure a value is entered for the field before the record is considered complete. The default value of this property is False. If you set it to True, a value must always be entered for the field. Here is an example: Private Sub cmdCreateTable_Click() Dim dbExercise As DAO.Database Dim tblEmployees As DAO.TableDef Dim fldEmployeeNumber As DAO.Field Dim fldEmployeeName As DAO.Field Dim fldEmailAddress As DAO.Field ' Specify the database to use Set dbExercise = CurrentDb ' Create a new TableDef object. Set tblEmployees = dbExercise.CreateTableDef("Employees") Set fldEmployeeNumber = tblEmployees.CreateField("EmployeeNumber", DB_TEXT) fldEmployeeNumber.Required = True tblEmployees.Fields.Append fldEmployeeNumber Set fldEmployeeName = tblEmployees.CreateField("EmployeeName", DB_TEXT) tblEmployees.Fields.Append fldEmployeeName Set fldEmailAddress = tblEmployees.CreateField("EmailAddress", DB_TEXT) tblEmployees.Fields.Append fldEmailAddress ' Add the new table to the database. dbExercise.TableDefs.Append tblEmployees dbExercise.Close Application.RefreshDatabaseWindow End Sub If you try or the user tries skipping a field whose Required property is set to True, the database would produce an error.
We saw that, to create a record set, you could call the OpenRecordset() method of a DAO.Database variable. Its syntax is expression.OpenRecordset(Name, Type, Options, LockEdit) A record set follows some rules in the way it is created or the way it is used. The rules concern the name or source of data and the type of record set, etc. So far, we provided the source of data as a name of a table. A record set must specify the way its records would be accessed and used. This information is provided by the second argument of the OpenRecordset() method. The value of this argument is (must represent) a member of the RecordsetTypeEnum enumeration. Its values follow the description we saw for the MAOL. The values are:
Further options are used to control the actions that can be performed on the Recordset object. These options are specified through the third argument of the OpenRecordset() method. The options are members of the RecordsetOptionEnum enumeration and they can be provided as an OR combination. The available values are:
Remember that the value you specify for this argument depends on the second and may depend on the fourth arguments.
The last argument controls how to deal with simultaneous connections to the same database. The value of this argument is a member of the LockTypeEnum enumeration. The available values are:
Besides creating a record set, probably the most common operation you perform on a record set is to navigate among records. The DAO.Recordset class supports the operations throught various method:
When navigating among records, you must avoid moving below the first record or above the last record. To assist you with this, the DAO.Recordset class is equipped with the BOF() and the EOF() methods. The BOF() method is used to check whether navigation is trying to reach below the first record. The EOF() method is used to check whether navigation is getting higher than the last record. Both methods return a Boolean value.
So far, when creating a record set, we used the name of a table. This is equivalent to selecting all records of the table. As an alternative, you can specify only some fields and/or some records that would constitute the record set. To do this, when calling the OpenRecordset() method of the DAO.Recordset class, pass the desired SQL statement as the first argument. To create the SQL statement, use everything you know about SELECT operations:
To further restrict the records or values, you can add a condition to the SQL statement.
A query is a technique of creating and saving a SQL statement for later use. To let you programmatically create a query in DAO, the DAO.Database class is equipped with the CreateQueryDef() method. The syntax of this method is: CreateQueryDef(NewQueryName, SQLStatement) The first argument is the name you will give to the new query. The second argument is a SQL statement that specifies the contents of the query. Here is an example that uses the Microsoft Access Object Library: Private Sub cmdCreateQuery_Click() Dim dbExercise As DAO.Database Dim qryEmployees As DAO.QueryDef Set dbExercise = CurrentDb Set qryEmployees = _ dbExercise.CreateQueryDef("StaffMembers", _ "SELECT EmployeeNumber, EmployeeeName " & _ "FROM Employees") dbExercise.Close Set dbExercise = Nothing Application.RefreshDatabaseWindow End Sub |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|