Microsoft Access Database Development With VBA

Introduction to the ADO Library

 

The Microsoft ActiveX Data Objects Library (ADO)

 

Introduction

Microsof created a library called ActiveX Data Objects or ADO to allow other development environments, including those that were not from Microsoft, to create and manage databases that could communicate with Microsoft Access.

Practical LearningPractical Learning: Introducing ADO

  1. Start Microsoft Access
  2. Click Blank Desktop Database
  3. In the File Name text box, type (replace Database1 with) BethesdaCarRental1
  4. Click Create
  5. On the Ribbon, click CREATE
  6. In the Tables section, click Table Design
  7. Create the fields of the table as follows:
     
    Field Name Data Type Field Size
    Category Short Text 30
    Notes Long Text  
  8. Right-click Category and click Primary Key
  9. To switch the table to Datasheet View, in the Views section of the Ribbon, click the View button Datasheet View
  10. When asked whether you want to save the table, click Yes
  11. Type Categories and click OK
  12. Close the table. If you want, create a form named Categories, whose Record Source is the Categories table, and design it as follows:
     
    Bethesda Car Rental - Cars Categories
  13. Create the values of the categories as follows:
     
    Category
    Economy
    Compact
    Standard
    Full Size
    Mini Van
    SUV
    Truck
    Grand Van
  14. Close the Categories form or table
  15. On the Ribbon, click CREATE and, in the Tables section, click Table Design
  16. Create the fields of the table as follows:
     
    Field Name Data Type Caption Field Size
    TagNumber Short Text Tag Number 20
    Category Short Text   30
    Make Short Text   40
    Model Short Text   40
    Doors Number   Integer
    Passengers Number   Integer
    Condition Short Text   40
    Available Short Text   10
    Pictures Attachment    
    Notes Long Text    
  17. Right-click TagNumber and click Primary Key
  18. Right-click the title bar or tab of the table and click Save
  19. Type Cars and click OK
  20. Set the Data Type of the Category column to Lookup Wizard...
  21. In the first page of the wizard, make sure the first radio button is selected and click Next
  22. In the second page of the wizard, click Table: Categories
  23. Click Next
  24. In the third page of the wizard, double-click Category
  25. Click Next
  26. Click Next
  27. Click Finish
  28. When asked to save the table, click Yes
  29. Set the Data Type of the Available column to Lookup Wizard...
  30. In the first page of the wizard, click the second radio button
  31. Click Next
  32. Under Col1, type Yes and press the down arrow key
  33. Type No
  34. Click Next
  35. Click Finish
  36. Close the table
  37. On the Ribbon, click CREATE and, in the Forms section, click Form Design
  38. Using the Properties window, set the Record Source to Cars
  39. Save the form as Cars
  40. Design the form as follows:
     
    Bethesda Car Rental - Cars
  41. Save the form and switch it to Form View
  42. Create the records for cars as follows (set all conditions to Excellent and all available to Yes) (the pictures of cars are provided; to add the pictures, right-click the Pictures placeholder and click Manage Attachments, then click Add, locate the picture, select it, and click Open):
     
    Tag Number Category Make Model Doors Passengers
    4FF975 Standard Honda Accord 4 4
    6DL882 Economy Honda Civic 4 4
    6DR840 Truck Ford F-150 2 2
    3AH579 Compact Kia Rio 4 4
    8DL248 Full Size Mercedes-Benz C350 4 4
    7ZZ846 Compact Lamborghini Gallardo 2 2
    6DG224 SUV Honda CR-V 4 4
    9FB200 Mini Van Toyota Sienna 4 7
    9DT273 Compact Mazda Miata 2 2
    5FA208 Standard Ford Taurus 4 4
    2EG270 Mini Van Volvo XC70 4 5
    8EN294 Economy Honda Civic 4 4
    6DQ208 Compact Kia Rio 4 4
    8FG882 Standard Honda Accord 4 4
    2LR927 Truck Ford F-150 2 4
    9BE425 Grand Van Chevrolet Express 3 20
    1CH249 Mini Van Toyota Sienna 4 7
    5DB274 Compact Nissan Sentra 4 4
    6FH225 Full Size Buick Lacrosse 4 4
    4FP208 SUV Acura RDX 4 4
    8CT208 Truck Chevrolet Silverado 2 4
  43. Close the Cars form
  44. On the Ribbon, click CREATE and, in the Tables section, click Table Design
  45. Create the fields of the table as follows:
     
    Field Name Data Type Caption Field Size
    EmployeeNumber Short Text Employee Number 20
    FirstName Short Text First Name 25
    LastName Short Text Last Name 25
    FullName Calculated    
  46. After setting the data type of FullName to Calculated, in the Expression Builder, type LastName & ', ' & FirstName
  47. Click OK
  48. Complete the definition of the table as follows:
     
    Field Name Data Type Caption Field Size
    EmployeeNumber      
    FirstName      
    LastName      
    FullName Short Text Full Name  
    Title Short Text   50
    Notes Long Text    
  49. Right-click EmployeeNumber and click Primary Key
  50. Close the ttable
  51. When asked whether you want to save it, click Yes
  52. Type Employees and click OK
  53. On the Ribbon, click CREATE and, in the Forms section, click Form Design
  54. Using the Properties window, set the Record Source to Employees
  55. Save the form as Employees
  56. Design the form as follows:
     
    Employees
  57. Save the form and switch it to Form View
  58. Create the records using the following values:
     
    EmployeeNumber First Name Last Name Title
    48058 Daniel Keller General Manager
    96350 Margareth Gremore Accounts Associates
    28005 Patrick Starr Accounts Associates
    27479 William Schenke Accounts Associates
    62094 Laura Walters Accounts Manager
    94286 Sonia Wallers Accounts Associates
  59. Close the Employees form
  60. On the Ribbon, click CREATE and, in the Forms section, click Form Design
  61. Save the form as NewCustomer
  62. Save and close the form
  63. Using the Properties window, change the following characteristics:
    Auto Center: Yes
    Navigation Buttons: No
    Dividing Lines: Yes
  64. Design the form as follows:
     
    Bethesda Car Rental - New Customer
    Control Name Caption
    Text Box Text Box txtDrvLicNumber Driver's Lic. #:
    Text Box Text Box txtFirstName First Name
    Text Box Text Box txtLastName Last Name:
    Text Box Text Box txtAddress Address:
    Text Box Text Box txtCity City:
    Text Box Text Box txtState State:
    Text Box Text Box txtZIPCode ZIP Code:
    Text Box Text Box txtNotes Notes:
    Button Button cmdSubmit Submit
    Button Button cmdReset Reset
    Button Button cmdClose Close
  65. Right-click the Reset button and click Build Event...
  66. Double-click Code Builder
  67. Implement the event as follows:
    Private Sub cmdReset_Click()
        txtDrvLicNumber = ""
        txtFirstName = ""
        txtLastName = ""
        txtAddress = ""
        txtCity = ""
        txtState = ""
        txtZIPCode = ""
        txtNotes = ""
    End Sub
  68. Return to Microsoft Access
  69. Close the NewCustomer form
  70. When asked whether you want to save, click Yes
  71. On the Ribbon, click CREATE and, in the Forms section, click Form Design
  72. Save the form as UpdateRentalOrder
  73. Using the Properties window, change the following characteristics:
    Auto Center: Yes
    Navigation Buttons: No
    Dividing Lines: Yes
  74. Design the form as follows:
     
    Bethesda Car Rental - Update Rental Order
    Control Name Caption Other Properties
    Text Box Text Box txtReceiptNumber Receipt #:  
    Button Button cmdFind Find  
    Text Box Text Box txtEmployeeNumber Employee #:  
    Text Box Text Box txtEmployeeName    
    Text Box Text Box txtDrvLicNumber Drv. Lic. #:  
    Text Box Text Box txtTagNumber Tag Number:  
    Text Box Text Box txtCustomerName Full Name:  
    Text Box Text Box txtMake Make:  
    Text Box Text Box txtCustomerAddress Address:  
    Text Box Text Box txtModel Model:  
    Text Box Text Box txtCity City:  
    Text Box Text Box txtDoorsPassengers Doors/Seats:  
    Text Box Text Box txtState State:  
    Combo Box Combo Box cbxConditions Condition: Column Count: 1
    Column Widths: 1"
    List Width: 1"
    Row Source: "Excellent";"Driveable";"Unknown"
    Row Source Type: Value List
    Text Box Text Box txtZIPCode ZIP Code:  
    Combo Box Combo Box cbxTankLevels Tank Levels: Column Count: 1
    Column Widths: 1"
    List Width: 1"
    Row Source: "Empty";"1/4 Empty";"Half";"3/4 Full";"Full"
    Row Source Type: Value List
    Text Box Text Box txtMileageStart Mileage Start:  
    Text Box Text Box txtMileageEnd End:  
    Text Box Text Box txtTotalMileage Total Miles: Format: General Number
    Text Box Text Box txtStartDate Start Date:  
    Text Box Text Box txtEndDate End:  
    Text Box Text Box txtTotalDays Total Days: Format: General Number
    Text Box Text Box txtRateApplied Rate Applied:  
    Text Box Text Box txtSubTotal Sub Total: Control Source: =Nz([txtRateApplied])*Nz([txtTotalDays])
    Text Box Text Box txtTaxRate Tax Rate: Default Value: 0.075
    Text Box Text Box txtTaxAmount Tax Amount: Control Source: =CLng(Nz([txtSubTotal])*Nz([txtTaxRate])*100)/100
    Combo Box Combo Box cbxOrdersStatus Orders Status: Column Count: 1
    Column Widths: 1"
    List Width: 1"
    Row Source: "Reserved";"Processing";"Rented - Car On Road";"Car Returned/Order Complete"
    Row Source Type: Value List
    Text Box Text Box txtRentTotal Rent Total: Control Source: =Nz([txtSubTotal])+Nz([txtTaxAmount])
    Text Box Text Box txtNotes Notes:  
    Button Button cmdSubmit Submit  
    Button Button cmdReset Reset  
    Button Button cmdClose Close  
  75. Right-click the Reset button and click Build Event...
  76. Double-click Code Builder
  77. Implement the event as follows:
    Private Sub cmdReset_Click()
        txtReceiptNumber = ""
        txtEmployeeNumber = ""
        txtEmployeeName = ""
        txtDrvLicNumber = ""
        txtCustomerName = ""
        txtCustomerAddress = ""
        txtCustomerCity = ""
        txtCustomerState = ""
        txtCustomerZIPCode = ""
        txtTagNumber = ""
        txtMake = ""
        txtModel = ""
        txtDoorsPassengers = ""
        cbxConditions = ""
        cbxTankLevels = ""
        txtMileageStart = ""
        txtMileageEnd = ""
        txtTotalMileage = ""
        txtStartDate = Date
        txtEndDate = Date
        txtTotalDays = ""
        txtRateApplied = ""
        cbxOrdersStatus = ""
        txtNotes = ""
    End Sub
  78. Close Microsoft Visual Basic and return to Microsoft Access
     
    Bethesda Car Rental - Update Rental Order
  79. Close the form
  80. When asked whether you want to save, click Yes
  81. In the Navigation Pane, right-click UpdateRentalOrder and click Copy
  82. Right-click any section of the Navigation Pane and click PasteForm Design
  83. Type NewRentalOrder as the name of the new form
  84. Click OK
  85. In the Navigation Pane, right-click NewRentalOrder and click Design View
  86. Change the design of the form as follows (you will mostly delete some controls that are not necessary, then you will adjust the design):
     
    Bethesda Car Rental - New Rental Order
  87. Right-click the Reset button and click Build Event
  88. Change the code of the event as follows:
    Private Sub cmdReset_Click()
        txtEmployeeNumber = ""
        txtEmployeeName = ""
        txtDrvLicNumber = ""
        txtCustomerName = ""
        txtCustomerAddress = ""
        txtCustomerCity = ""
        txtCustomerState = ""
        txtCustomerZIPCode = ""
        txtTagNumber = ""
        txtMake = ""
        txtModel = ""
        txtDoorsPassengers = ""
        cbxConditions = ""
        cbxTankLevels = ""
        txtMileageStart = ""
        txtStartDate = Date
        txtRateApplied = ""
        cbxOrdersStatus = ""
        txtNotes = ""
    End Sub
  89. Return to Microsoft Access
  90. Close the form
  91. When asked whether you want to save, click Yes

Using ADO

Although treated as one entity, Microsoft ActiveX Data Object (ADO) is really many libraries grouped under one name. To use ADO in your database, open the References dialog box, locate the latest version of Microsoft ActiveX Data Objects 6.1 library and click its check box:

References

Although you don't have to, to bring Microsoft ActiveX Data Objects X.X Library up to the list of loaded libraries, you can click the up pointing button above Priority. Once you are ready, click OK.

To use an object of the ADO library, you will declare a variable for it.

Practical LearningPractical Learning: Using ADO

  1. On the Ribbon, click Database Tools and click Visual Basic
  2. On the main menu of Microsoft Visual Basic, click Tools -> References...
  3. Scroll down in the Available References list box until you find Microsoft ActiveX Data Objects...
    Double-click the one with one with the highest version:
     
    References
  4. Click OK
 
 
 

The Connection to a Database

 

Introduction

To use or access a database in the ADO library, you must first establish a connection. To support this, the ADO library provides a class named Connection. Before establishing a connection, declare a variable of type ADODB.Connection. Here is an example:

Private Sub cmdConnector_Click()
    Dim connector As ADODB.Connection
End Sub

Before using the connection, you must initialize the variable. This is done using the New operator. You have two options. You can initialize the variable after declaring it. In this case, precede the variable with the Set operator. This would be done as follows:

Private Sub cmdConnector_Click()
    Dim connector As ADODB.Connection
    
    Set connector = New ADODB.Connection
End Sub

A faster alternative is to apply the New operator when declaring the variable. Here is an example:

Private Sub cmdConnector_Click()
    Dim connector As New ADODB.Connection
End Sub

After using the Connection variable, to release the resources it was using, assign Nothing to it. This would be done as follows:

Private Sub cmdConnector_Click()
    Dim connector As ADODB.Connection
    
    Set connector = New ADODB.Connection

    Set connector = Nothing
End Sub

Opening a Connection

After declaring and initializing the Connection object, you can open the connection. To support this, the Connection class is equipped with a method named Open. The syntax of the Connection.Open() method is:

Connection.Open ConnectionString, UserID, Password, Options

The first argument to this method is called a connection string.

Introduction to the Connection String

A connection string is text made of various sections separated by semi-colons. Each section is made of a Key=Value expression. Based on this, a connection string uses the following formula:

Key1=Value1;Key2=Value2;Key_n=Value_n;

The Data Provider of a Database

The first part of the connection string is called the provider. It specifies the platform used to handle the database. To specify it, assign the desired name to the Provider key. This would be done as:

Private Sub cmdConnector_Click()
    Dim connector As ADODB.Connection
    
    Set connector = New ADODB.Connection
    connector.Open "Provider=

    Set connector = Nothing
End Sub

Although there are various providers in the industry, there are two primary providers used in ADO. One of them is Microsoft SQL Server and it is represented by SQLOLEDB. Therefore, if your database will reside on a Microsoft SQL Server, you can specify the provider as follows:

Private Sub cmdConnector_Click()
    Dim connector As ADODB.Connection
    
    Set connector = New ADODB.Connection
    connector.Open "Provider=SQLOEDB"

    Set connector = Nothing
End Sub

The most common provider we will use is the Microsoft JET database engine. It can represented as Microsoft.JET.OLEDB.4.0 (case insensitive). You can use it to specify the provider as follows:

Private Sub cmdConnector_Click()
    Dim connector As ADODB.Connection
    
    Set connector = New ADODB.Connection
    connector.Open "Provider=Microsoft.Jet.OLEDB.4.0"

    Set connector = Nothing
End Sub

You can also include the name of the provider as its own string. To do that, you can include it in single-quotes:

Private Sub cmdConnector_Click()
    Dim connector As ADODB.Connection
    
    Set connector = New ADODB.Connection
    connector.Open "Provider='Microsoft.JET.OLEDB.4.0'"

    Set connector = Nothing
End Sub

You can also specify the provider as Microsoft.ACE.OLEDB.12.0.

Introduction to the Data Source of a Database

The second part of the connection string is referred to as the data source. It is represented by a key named Data Source. To specify it, you can assign the name of the database with its extension to the Data Source factor. Here is an example:

Private Sub cmdConnector_Click()
    Dim connector As ADODB.Connection
    
    Set connector = New ADODB.Connection
    connector.Open "Provider='Microsoft.ACE.OLEDB.12.0';Data Source=Example.accdb"

    Set connector = Nothing
End SubP

If you provide (only) the name of the database, the database engine would look for it in the same folder as the application that called it. In the above example, the database engine may look for it in the My Documents folder. If you want to refer to a database that is outside of the folder of the application that called this method, provide a complete path  of the database. This can consist of the drive, the folder(s), and the name of the database with its extension.

 Here is an example:

Private Sub cmdConnector_Click()
    Dim connector As ADODB.Connection
    
    Set connector = New ADODB.Connection
    connector.Open "Provider='Microsoft.ACE.OLEDB.12.0';Data Source=C:\Exercises\Example.accdb"

    Set connector = Nothing
End Sub

If you are referring to a Microsoft Access Office 2007 or later database, specify the extension as .accdb. If you are referring to a database compatible with previous versions of Microsoft Access, specify the extension as .mdb.

To be safer, you can include the value of the data source in single-quotes:

Private Sub cmdConnector_Click()
    Dim connector As ADODB.Connection
    
    Set connector = New ADODB.Connection
    connector.Open "Provider='Microsoft.ACE.OLEDB.12.0';'Data Source=Example.accdb'"

    Set connector = Nothing
End Sub

Instead of directly passing a string to the Open() method, you can first declare a String variable, initialize it with the necessary provider/data source, and then pass that string variable to the Create() method. Here is an example:

Private Sub cmdConnector_Click()
    Dim strConnection As String
    Dim connector As ADODB.Connection
    
    Set connector = New ADODB.Connection
    strConnection = "Provider='Microsoft.ACE.OLEDB.12.0';'Data Source=Example.accdb'"
    connector.Open strConnection

    Set connector = Nothing
End Sub

Of course, you can create the string using any of the appropriate techniques you want. Here is an example:

Private Sub cmdConnector_Click()
    Dim strConnection As String
    Dim connector As ADODB.Connection
    
    Set connector = New ADODB.Connection
    strConnection = "Provider='Microsoft.ACE.OLEDB.12.0';"
    strConnection = strConnection & "'Data Source=Example.accdb'"
    connector.Open strConnection

    Set connector = Nothing
End Sub

Using an ADO Connection

 

A Connection to the Current Database

The connection allows you to create a connection to either the database you are working on or another database that may be currently closed. You must know the path to the database you want to access. In some cases, you may simply want to connect to the current database. To support this, the CurrentProject property of the Application object is equipped with a property named Connection. This gives you a convenient access to the connection of the currently opened database. Here is an example of invoking it:

Private Sub cmdCurrentConnection_Click()
    Dim conCurrent As ADODB.Connection
    
    Set conCurrent = Application.CurrentProject.Connection
End Sub

Remember that, after using a connection, you should release the resources it was using:

Private Sub cmdCurrentConnection_Click()
    Dim conCurrent As ADODB.Connection
    
    Set conCurrent = Application.CurrentProject.Connection

    Set conCurrent = Nothing
End Sub

When this code executes, it identifies the connection to the current database and stores it in a declared variable named conCurrent.

Executing a SQL Statement

After creating a connection to a database, the next step is usually to specify what you want to do on the database. One of the most usual operations you can perform is to submit a SQL statement to it (the connection). This is also equivalent to executing the statement.

To execute a statement, the Connection class is equipped with a method named Execute. Its syntax is:

Execute CommandText, RecordsAffected, Options

The first argument, CommandText, can be a SQL statement. It can also be something else as we will see. The second and the third arguments are optional.

Here is an example:

Private Sub cmdConnector_Click()
    Dim connector As ADODB.Connection
    Dim strConnection As String
    Dim strStatement As String
    
    strStatement = "Blah Blah Blah"

    Set connector = New ADODB.Connection
    strConnection = "Provider='Microsoft.ACE.OLEDB.12.0';"
    strConnection = strConnection & "Data Source='C:\Programs\Exercise1.accdb';"    

    connector.Open strConnection
    connector.Execute strStatement 

    Set connector = Nothing
    
End Sub

In this case, the strStatement string would be passed to the Execute() method of the Connection object that would execute it. Because the statement is created as a string and doesn't "belong" to Microsoft Access, it will not be checked until it must be executed. This means that it is (quite completely) your responsibility to formulate a valid statement. Microsoft Access cannot check, and will not assist you with, the validity of the statement, until the statement executes.

Closing a Connection

When using a connection, it consumes resources that other applications may need. Therefore, after using it, you should close it and free the resources it was using so they can be made available to the other parts of the computer.

To provide the ability to close a connection, the Connection class is equipped with a method named Close. Therefore, to close a connection, call its Close() method. Here is an example:

Private Sub cmdDataSource_Click()
    Dim connector As ADODB.Connection
    
    Set connector = New ADODB.Connection
    
    . . .

    connector.Close
    Set connector = Nothing
End Sub

The Tables of an ADO Connection

 

Creating a Table Using an ADO Connection

There are many types of operations you can perform on an ADO connection. As mentioned already, to perform an operation, create its SQL statement and pass it to the Execute() method of the Connection class. This means that the ADO's Connection class relies on the SQL to perform most of its database operations.

To create a table using the ADO's Connection class, formulate a SQL's CREATE TABLE ... statement and pass it to the Execute() method. Here is an example:

Private Sub cmdTable_Click()
    Dim conCurrent As ADODB.Connection
    
    Set conCurrent = Application.CurrentProject.Connection
    
    conCurrent.Execute "CREATE TABLE Customers(FirstName Text, " & _
                                              "LastName Char);"
    
    MsgBox "A table named Customers has been created."
    conCurrent.Close
End Sub

Practical LearningPractical Learning: Creating a Table in ADO

  1. On the Ribbon, click CREATE and, in the Forms section, click Form Design
  2. From the Controls section of the Ribbon, click the Button Button and click the form. If the wizard starts, click Cancel
  3. Using the Properties window, change the following characteristics of the button:
    Name: cmdCreateTables
    Caption: Create Tables
  4. Right-click the button and click Build Event...
  5. Double-click Code Builder
  6. Implement the event as follows:
    Private Sub cmdCreateTables_Click()
        Dim dbConnection As New ADODB.Connection
        
        Set dbConnection = CurrentProject.Connection
        
        dbConnection.Execute "CREATE TABLE RentalRates" & _
                             "(" & _
                             "   Category Text(32), " & _
                             "   Daily Double, " & _
                             "   Weekly Double, " & _
                             "   Monthly Double, " & _
                             "   Weekend Double " & _
                             ");"
        
        dbConnection.Execute "CREATE TABLE Customers" & _
                             "(" & _
                             "   CustomerNumber Counter(10001, 1), " & _
                             "   DrvLicNumber Text(30), " & _
                             "   FirstName Text(25), " & _
                             "   LastName Text(25), " & _
                             "   Address Text(60), " & _
                             "   City Text(50), " & _
                             "   State Text(40)," & _
                             "   ZIPCode Text(20)," & _
                             "   Notes LongText, " & _
                             "   CONSTRAINT PK_Customers PRIMARY KEY(CustomerNumber)" & _
                             ");"
        
        dbConnection.Execute "CREATE TABLE RentalOrders" & _
                             "(" & _
                             "   ReceiptNumber Counter(100001, 1), " & _
                             "   EmployeeNumber Text(20), " & _
                             "   DrvLicNumber Text(30), " & _
                             "   TagNumber Text(20), " & _
                             "   CarCondition Text(50), TankLevel Text(30), " & _
                             "   MileageStart Integer, MileageEnd Integer, " & _
                             "   TotalMileage Integer, " & _
                             "   StartDate Date, EndDate Date, TotalDays Integer, " & _
                             "   RateApplied Double, " & _
                             "   TaxRate Double Default 0.0750, " & _
                             "   OrderStatus Text(40) Default 'Unknown', " & _
                             "   Notes LongText, " & _
                             "   CONSTRAINT FK_RentalOrdersClerks Foreign Key(EmployeeNumber)  " & _
                             "      REFERENCES Employees(EmployeeNumber), " & _
                             "   CONSTRAINT FK_RentalOrderedCars Foreign Key(TagNumber)  " & _
                             "      REFERENCES Cars(TagNumber), " & _
                             "   CONSTRAINT PK_RentalOrders PRIMARY KEY(ReceiptNumber)" & _
                             ");"
        
        dbConnection.Close 
        Set dbConnection = Nothing
        Application.RefreshDatabaseWindow
    End Sub
    
    
  7. Close Microsoft Visual Basic and return to Microsoft Access
  8. Switch the form to Form View
  9. Click the Create Tables button
  10. Close the form
  11. When asked whether you want to save the table, click No
  12. In the Navigation Pane, right-click Customers and click Design View
  13. Right-click Address under Field Name and click Insert Rows
  14. In the new empty box, type FullName
  15. Set its Data Type to Calculated
  16. In the Expression Builder, type LastName & ', ' & FirstName
  17. Click OK
     
    Bethesda Car Rental - Customers
  18. Save and close the Customers table
  19. On the Ribbon, click CREATE and, in the Forms section, click Form Design
  20. Using the Properties window, change the following characteristics:
    Record Source: RentalRates
    Default View: Continuous Forms
    Navigation Buttons: No
  21. Save the form as RentalRates
  22. Design the form as follows:
     
    Bethesda Car Rental - Rental Rates
  23. Save and close the form

Deleting a Table in ADO

Another type of operation you can perform using a database connection is to remove a table from the database. To delete a table, create a DROP TABLE ... SQL statement and execute it by calling the Execute() method of the ADODB.Connection class. Here is an example:

Private Sub cmdDeleteTable_Click()
    Dim conDepartments As ADODB.Connection
    Dim strSQL As String
    
    Set conDepartments = New ADODB.Connection
    conDepartments.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                        "Data Source='C:\My Documents\Exercise.accdb'"
    
    strSQL = "DROP TABLE Departments;"
    
    conDepartments.Execute strSQL
    
    MsgBox "The Departments table of the Exercise.accdb database has been deleted"
    
    Set conDepartments = Nothing
End Sub

Practical LearningPractical Learning: Ending the Lesson

  • Close Microsoft Access
 
 
   
 

Previous Copyright © 2005-2013, FunctionX, Inc. Next