Professional Documents
Culture Documents
A Set of Records
Creating a table provides you with a structure in which you can create a list of values. When a table has been created, it is initially empty but an empty placeholder is automatically created for each column: Family Members Name Age Gender Relationship
Therefore, after creating a table, you can populate it. Since an empty placeholder is created under each column, you can start by adding one value for each category. When a value has been created for each column, even if you skip some columns, you are said to have created a record. Here is an example:
In the same way, you can keep adding records as you see fit. Every time you have created a record, a placeholder for a new record is automatically made available so you don't have to wonder when a record would go. This also implies that each new record you create is appended, that is, it is added to the end, of the table. Here is an example of a table with records: Family Members Name Judie Ernest Bill David Hermine Age 18 24 52 36 Male Gender Relationship Female Male Uncle Brother Unknown Niece Sister
2. On the opening dialog box, click Standard EXE and click Open 3. Save the project in a new folder inside of your ADO Lessons folder and name it MVA2 4. Save the file of the form as Main and save the project as Motor Vehicle Administration1
5. Double-click an empty area of the form to access its Load event 6. Use the same approach as done previously to create a new database named MVA and store it in the above Tables folder (don't forget to add a reference to ADOX using the References dialog box): Private Sub Form_Load() Dim conCatalog As ADOX.Catalog Set conCatalog = New ADOX.Catalog conCatalog.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source='C:\ADO Lessons\MVA2\MVA.mdb'" MsgBox "A new Microsoft JET database named MVA.mdb has been created" Set conCatalog = Nothing End Sub 7. Execute the application
8. Click the OK button then close the form 9. To create a new table, change the code of the Load event event as follows: Private Sub Form_Load() Dim conEmployees As ADODB.Connection Set conEmployees = New ADODB.Connection conEmployees.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source='C:\ADO Lessons\MVA2\MVA.mdb'" conEmployees.Execute "CREATE TABLE Employees(" & _ "EmplNumber Text(6)," & _ "DateHired DateTime," & _ "FirstName VarChar(20)," & _ "LastName varchar(20)," & _ "Department varchar(40)," & _ "EmailAddress varchar(50));" MsgBox "A table named Employees has been added to the MVA.mdb database" Set conEmployees = Nothing End Sub 10. Add a reference to your most recent ADO driver using the References dialog box 11. Execute the application
12. Click OK then close the form 13. Delete the whole code of the Load event
adOpenKeyset
adOpenDynamic
adOpenStatic
deletions by other users are not visible The default value of this argument is adOpenForwardOnly. The fourth argument specifies the type of locking system that would be used on the set of records. The available options are stored in an enumerator called LockTypeEnum and they are: Constant adLockUnspecified adLockReadOnly adLockPessimistic Value Description -1 1 2 Does not specify a type of lock. For clones, the clone is created with the same lock type as the original Indicates read-only records. You cannot alter the data Indicates pessimistic locking, record by record. The provider does what is necessary to ensure successful editing of the records, usually by locking records at the data source immediately after editing Indicates optimistic locking, record by record. The provider uses optimistic locking, locking records only when you call the Update method Indicates optimistic batch updates. Required for batch update mode
adLockOptimistic
adLockBatchOptimistic
The last argument is an option you specify about the first argument.
Record Creation
One of the actions you can perform using a Recordset object is to perform data entry, that is, to create new records. To support data entry, the Recordset object is equipped with theAddNew() method. Its syntax is: recordset.AddNew FieldList, Values The first argument is a name of a column and must be identified as belonging to an array calledFields. After specifying the name of the column in the parentheses of the Fields array, you can assign it the desired value. You can do this for each column or for selected columns of the table. After creating a record, you should remember to save it. This is done by calling the Update()method of the Recordset object.
2. Double-click the Submit Record button and implement its Click event as follows: Private Sub cmdSubmitRecord_Click() Dim rcsEmployees As New ADODB.Recordset Dim conEmployees As New ADODB.Connection conEmployees.Open "Provider='Microsoft.JET.OLEDB.4.0';" & _ "Data Source='C:\ADO Lessons\MVA2\MVA.mdb'" rcsEmployees.Open "Employees", conEmployees, adOpenStatic, adLockOptimistic If rcsEmployees.Supports(adAddNew) Then With rcsEmployees .AddNew .Fields("EmplNumber") = Me.txtEmployeeNbr.Text .Fields("DateHired") = Me.txtDateHired.Text .Fields("FirstName") = Me.txtFirstName.Text .Fields("LastName") = Me.txtLastName.Text .Fields("Department") = Me.txtDepartment.Text .Fields("EmailAddress") = Me.txtEmailAddress.Text .Update End With MsgBox "A new record has been added to the Employees table" End If Me.txtEmployeeNbr.Text = "" Me.txtDateHired.Text = "" Me.txtFirstName.Text = "" Me.txtLastName.Text = "" Me.txtDepartment.Text = "" Me.txtEmailAddress.Text = "" Me.txtEmployeeNbr.SetFocus rcsEmployees.Close Set rcsEmployees = Nothing conEmployees.Close Set rcsEmployees = Nothing End Sub 3. Execute the application and create a new record:
Record Insertion
Besides, or as opposed to, adding a new record, if you don't want to formally use theRecordset class to create a record, you can use the Execute() method of the Connectionobject. To create a new record using the Connection object, you can call the Execute()method and pass it a string that contains the following formula: INSERT TableName VALUES(Name1, Name2, Name_n) or INSERT INTO TableName VALUES(Name1, Name2, Name_n)