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. In the File Name text box, type (replace Database1 with) BethesdaCarRental1
  3. Click Create
  4. On the Ribbon, click File and click Options
  5. In the left click, click Current Database
  6. Click Overlapping Windows
  7. Click OK
  8. Click OK
  9. Close Microsoft Access and re-open it with the BethesdaCarRental1 database
  10. On the Ribbon, click Create
  11. In the Tables section, click Table Design
  12. Create the fields of the table as follows:
     
    Field Name Data Type Field Size
    Category Text 30
    Notes Memo  
  13. Right-click Category and click Primary Key
  14. To switch the table to Datasheet View, in the Views section of the Ribbon, click the View button Datasheet View
  15. When asked whether you want to save the table, click Yes
  16. Type Categories and click OK
  17. 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
  18. Create the values of the categories as follows:
     
    Category
    Economy
    Compact
    Standard
    Full Size
    Mini Van
    SUV
    Truck
    Grand Van
  19. Close the Categories form or table
  20. On the Ribbon, click Create and, in the Tables section, click Table Design
  21. Create the fields of the table as follows:
     
    Field Name Data Type Caption Field Size
    TagNumber Text Tag Number 20
    Category Text   30
    Make Text   40
    Model Text   40
    Doors Number   Integer
    Passengers Number   Integer
    Condition Text   40
    Available Text   10
    Pictures Attachment    
    Notes Memo    
  22. Right-click TagNumber and click Primary Key
  23. Right-click the title bar or tab of the table and click Save
  24. Type Cars and click OK
  25. Set the Data Type of the Category column to Lookup Wizard...
  26. In the first page of the wizard, make sure the first radio button is selected and click Next
  27. In the second page of the wizard, click Table: Categories
  28. Click Next
  29. In the third page of the wizard, double-click Category
  30. Click Next
  31. Click Next
  32. Click Finish
  33. When asked to save the table, click Yes
  34. Set the Data Type of the Available column to Lookup Wizard...
  35. In the first page of the wizard, click the second radio button
  36. Click Next
  37. Under Col1, type Yes and press the down arrow key
  38. Type No
  39. Click Next
  40. Click Finish
  41. Close the table
  42. On the Ribbon, click Create and, in the Forms section, click Form Design
  43. Using the Properties window, set the Record Source to Cars
  44. Save the form as Cars
  45. Design the form as follows:
     
    Bethesda Car Rental - Cars
  46. Save the form and switch it to Form View
  47. 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
  48. Close the Cars form
  49. On the Ribbon, click Create and, in the Tables section, click Table Design
  50. Create the fields of the table as follows:
     
    Field Name Data Type Caption Field Size
    EmployeeNumber Text Employee Number 20
    FirstName Text First Name 25
    LastName Text Last Name 25
    FullName Calculated    
  51. After setting the data type of FullName to Calculated, in the Expression Builder, type LastName & ', ' & FirstName
  52. Click OK
  53. Complete the definition of the table as follows:
     
    Field Name Data Type Caption Field Size
    EmployeeNumber      
    FirstName      
    LastName      
    FullName Text Full Name  
    Title Text   50
    Notes Memo    
  54. Right-click EmployeeNumber and click Primary Key
  55. Close the ttable
  56. When asked whether you want to save it, click Yes
  57. Type Employees and click OK
  58. On the Ribbon, click Create and, in the Forms section, click Form Design
  59. Using the Properties window, set the Record Source to Employees
  60. Save the form as Employees
  61. Design the form as follows:
     
    Employees
  62. Save the form and switch it to Form View
  63. 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
  64. Close the Employees form
  65. On the Ribbon, click Create and, in the Forms section, click Form Design
  66. Save the form as NewCustomer
  67. Save and close the form
  68. Using the Properties window, change the following characteristics:
    Auto Center: Yes
    Navigation Buttons: No
    Dividing Lines: Yes
  69. 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
  70. Right-click the Reset button and click Build Event...
  71. Double-click Code Builder
  72. Implement the event as follows:
    Private Sub cmdReset_Click()
        txtDrvLicNumber = ""
        txtFirstName = ""
        txtLastName = ""
        txtAddress = ""
        txtCity = ""
        txtState = ""
        txtZIPCode = ""
        txtNotes = ""
    End Sub
  73. Return to Microsoft Access
  74. Close the NewCustomer form
  75. When asked whether you want to save, click Yes
  76. On the Ribbon, click Create and, in the Forms section, click Form Design
  77. Save the form as UpdateRentalOrder
  78. Using the Properties window, change the following characteristics:
    Auto Center: Yes
    Navigation Buttons: No
    Dividing Lines: Yes
  79. 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  
  80. Right-click the Reset button and click Build Event...
  81. Double-click Code Builder
  82. 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
  83. Close Microsoft Visual Basic and return to Microsoft Access
     
    Bethesda Car Rental - Update Rental Order
  84. Close the form
  85. When asked whether you want to save, click Yes
  86. In the Navigation Pane, right-click UpdateRentalOrder and click Copy
  87. Right-click any section of the Navigation Pane and click PasteForm Design
  88. Type NewRentalOrder as the name of the new form
  89. Click OK
  90. In the Navigation Pane, right-click NewRentalOrder and click Design View
  91. 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
  92. Right-click the Reset button and click Build Event
  93. 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
  94. Return to Microsoft Access
  95. Close the form
  96. 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 X.X 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 and 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 Memo, " & _
                            "   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 Memo, " & _
                            "   CONSTRAINT FK_Employees Foreign Key(EmployeeNumber)  " & _
                            "      REFERENCES Employees(EmployeeNumber), " & _
                            "   CONSTRAINT FK_Cars 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-2016, FunctionX, Inc. Next