|
Example Application: Bethesda Car Rental
|
|
|
Bethesda Car Rental is a fictitious company that rents cars
(vehicles) to customers. The company has many employees who take care of cars,
customers, related transactions, and other maintenance tasks.
|
To rent a car, a customer must come to the store, fill out
any application to provide the necessary information, select a car to rent, and
use it for the desired period.
Practical
Learning: Starting the Application
|
|
- Start Microsoft Access
- In the File Name text box, type (replace Database1 with) BethesdaCarRental
- Click Create
- On the Ribbon, click File and click Options
- In the left click, click Current Database
- Click Overlapping Windows
- Click OK
- Click OK
- Close Microsoft Access and re-open it with the BethesdaCarRental2
database
- On the Ribbon, click Database Tools and click Visual Basic
- On the main menu of Microsoft Visual Basic, click Tools ->
References...
- 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:
- Click OK
- Return to Microsoft Access
The cars are provided in various categories from small
(compact) to trucks, from mini vans and SUVs (sport utility vehicles) to large
vans of many seats. Instead of storing a constant list of categories, we will
create a table for them.
Practical
Learning: Creating the Categories of Cars
|
|
- On the Ribbon, click Create
- In the Tables section, click Table Design
- Create the fields of the table as follows:
Field Name |
Data Type |
Field Size |
Category |
Text |
30 |
Notes |
Memo |
|
- Right-click Category and click Primary Key
- To switch the table to Datasheet View, in the Views section of the
Ribbon, click the View button
- When asked whether you want to save the table, click Yes
- Type Categories and click OK
- Close the table
- On the Ribbon, click Create and, in the Forms section, click Form
Design
- Using the Properties window, set the Record Source to Categories
- Save the form as Categories
- Complete its design as follows:
- Save and close the Categories form
The Cars Rented to Customers
|
|
The company has many cars that customers can rent. Customers
have different needs, desires, and requirements. Therefore, the company has
various cars of different categories as we mentioned in the previous section.
Before renting a car, a customer may ask various questions about the available
cars. The employee helping the customer must then check the company's inventory
of cars to see which one suits a customer.
The record of each car must provide all the necessary
information to let a customer choose the necessary one. To assist with this, we
will create a list of cars.
Practical
Learning: Creating the Cars
|
|
- On the Ribbon, click Create and, in the Tables section, click Table Design
- 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 |
|
|
- Right-click TagNumber and click Primary Key
- Right-click the title bar or tab of the table and click Save
- Type Cars and click OK
- Set the Data Type of the Category column to Lookup Wizard...
- In the first page of the wizard, make sure the first radio button
is selected and click Next
- In the second page of the wizard, click Table: Categories
- Click Next
- In the third page of the wizard, double-click Category
- Click Next
- Click Next
- Click Finish
- When asked to save the table, click Yes
- Set the Data Type of the Available column to Lookup Wizard...
- In the first page of the wizard, click the second radio button
- Click Next
- Under Col1, type Yes and press the down arrow key
- Type No
- Click Next
- Click Finish
- Close the table
- On the Ribbon, click Create and, in the Forms section, click Form
Design
- Using the Properties window, set the Record Source to Cars
- Save the form as Cars
- Design the form as follows:
- Save and close the Cars form
As mentioned already, each car is considered as belonging to
a certain category. The price of renting a car depends on its category.
Furthermore, customers rent cars for different periods. There are people who
need a car for one or more days during the week. There are people who need a car
for the weekend. And there are people who need a car for a long period, such as
a month or an undetermined period based on the customer's circumstances.
We will create a list of prices based on categories and
periods. An employee can then refer to such a table to let a customer know how
much it would cost to rent a certain car.
Practical
Learning: Creating a Table in ADO
|
|
- On the Ribbon, click Create and, in the Forms section, click Form Design
- From the Controls section of the Ribbon, click the Button
and click the form. If the wizard starts, click Cancel
- Using the Properties window, change the following characteristics
of the button:
Name: cmdCreateRentalRates Caption:
Create Rental Rates
- Right-click the button and click Build Event...
- Double-click Code Builder
- Implement the event as follows:
Private Sub cmdCreateRentalRates_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 "INSERT INTO RentalRates(Category, Daily, Weekly, Monthly, Weekend)" & _
"VALUES('Economy', 34.95, 28.75, 24.95, 24.95);"
dbConnection.Execute "INSERT INTO RentalRates(Category, Daily, Weekly, Monthly, Weekend)" & _
"VALUES('Compact', 38.95, 32.75, 28.95, 28.95);"
dbConnection.Execute "INSERT INTO RentalRates(Category, Daily, Weekly, Monthly, Weekend)" & _
"VALUES('Standard', 45.95, 39.75, 35.95, 34.95);"
dbConnection.Execute "INSERT INTO RentalRates(Category, Daily, Weekly, Monthly, Weekend)" & _
"VALUES('Full Size', 50.00, 45.00, 42.55, 38.95);"
dbConnection.Execute "INSERT INTO RentalRates(Category, Daily, Weekly, Monthly, Weekend)" & _
"VALUES('Mini Van', 55.00, 50.00, 44.95, 42.95);"
dbConnection.Execute "INSERT INTO RentalRates(Category, Daily, Weekly, Monthly, Weekend)" & _
"VALUES('SUV', 56.95, 52.95, 44.95, 42.95);"
dbConnection.Execute "INSERT INTO RentalRates(Category, Daily, Weekly, Monthly, Weekend)" & _
"VALUES('Truck', 62.95, 52.75, 46.95, 44.95);"
dbConnection.Execute "INSERT INTO RentalRates(Category, Daily, Weekly, Monthly, Weekend)" & _
"VALUES('Grand Van', 69.95, 64.75, 52.75, 49.95);"
dbConnection.Close
Set dbConnection = Nothing
Application.RefreshDatabaseWindow
End Sub
- Return to Microsoft Access
- Swith the form to Form View
- Click the Create Rental Rates button
- Close the form
- When asked whether you want to save, click No
- On the Ribbon, click Create and, in the Forms section, click Form Design
- Using the Properties window, change the following characteristics:
Record Source: RentalRates Default View: Continuous Forms Navigation Buttons: No
- Save the form as RentalRates
- Design the form as follows:
- Save and close the form
The Employees of the Company
|
|
Employees are staff members who use and manage the database.
We will use a simple list with minimal information such as the name and title.
Each employee must be identified with a unique employee number. Also, we will
use a feature new in Microsoft Access 2010 to create a field that represents the
full name of the employee. This means that this database cannot be used in
versions of Microsoft Access prior to 2010.
Practical
Learning: Creating Employees
|
|
- On the Ribbon, click Create and, in the Tables section, click Table Design
- 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 |
|
|
- After setting the data type of FullName to Calculated, in the Expression
Builder, type LastName & ', ' & FirstName
- Click OK
- 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 |
|
|
- Right-click EmployeeNumber and click Primary Key
- Close the ttable
- When asked whether you want to save it, click Yes
- Type Employees and click OK
- On the Ribbon, click Create and, in the Forms section, click Form
Design
- Using the Properties window, set the Record Source to Employees
- Save the form as Employees
- Design the form as follows:
- Save and close the Employees form
Customers are individuals who rent a car for a fee. To rent
a car, besides having a driver's license, a customer must provide certain
minimum information, then select a car. We will use only just some information
such as a name and an address. Usually there is more information than that but
we will not need it.
Practical
Learning: Creating Customers
|
|
- On the Ribbon, click Create and, in the Forms section, click Form
Design
- Save the form as NewCustomer
- Save and close the form
- Using the Properties window, change the following characteristics:
Auto Center: Yes Navigation Buttons: No Dividing Lines: Yes
- Design the form as follows:
|
Control |
Name |
Caption |
Text Box |
|
txtDrvLicNumber |
Driver's Lic. #: |
Text Box |
|
txtFirstName |
First Name |
Text Box |
|
txtLastName |
Last Name: |
Text Box |
|
txtAddress |
Address: |
Text Box |
|
txtCity |
City: |
Text Box |
|
txtState |
State: |
Text Box |
|
txtZIPCode |
ZIP Code: |
Text Box |
|
txtNotes |
Notes: |
Button |
|
cmdSubmit |
Submit |
Button |
|
cmdReset |
Reset |
Button |
|
cmdClose |
Close |
|
- Right-click the Reset button and click Build Event...
- Double-click Code Builder
- Implement the event as follows:
Private Sub cmdReset_Click()
txtDrvLicNumber = ""
txtFirstName = ""
txtLastName = ""
txtAddress = ""
txtCity = ""
txtState = ""
txtZIPCode = ""
txtNotes = ""
End Sub
- Return to Microsoft Access
- Close the NewCustomer form
- When asked whether you want to save, click Yes
- On the Ribbon, click Create and, in the Forms section, click Form Design
- From the Controls section of the Ribbon, click the Button
and click the form. If the wizard starts, click Cancel
- Using the Properties window, change the following characteristics
of the button:
Name: cmdCreateCustomers Caption:
Create Customers
- Right-click the button and click Build Event...
- Double-click Code Builder
- Implement the event as follows:
Private Sub cmdCreateCustomers_Click()
Dim dbConnection As New ADODB.Connection
Set dbConnection = CurrentProject.Connection
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.Close
Set dbConnection = Nothing
Application.RefreshDatabaseWindow
End Sub
- Return to Microsoft Access
- Switch the form to Form View
- Click the Create Customers button
- Close the form
- When asked whether you want to save the table, click No
- In the Navigation Pane, right-click Customers and click Design View
- Right-click Address under Field Name and click Insert Rows
- In the new empty box, type FullName
- Set its Data Type to Calculated
- In the Expression
Builder, type LastName & ', ' & FirstName
- Click OK
- Save and close the Customers table
- In the Navigation Pane, right-click NewCustomer and click Design
View
- On the form, right-click the Submit button and click Build
Event...
- Double-click Code Builder
- Implement the event as follows:
Private Sub cmdSubmit_Click()
On Error GoTo cmdSubmit_ClickError
Dim rsCustomers As ADODB.Recordset
Set rsCustomers = New ADODB.Recordset
rsCustomers.Open "Customers", _
CurrentProject.Connection, _
adOpenStatic, _
adLockOptimistic
If IsNull(txtDrvLicNumber) Then
MsgBox "You must enter the customer's driver's license number.", _
vbOKOnly Or vbInformation, "Bethesda Car Rental"
Exit Sub
End If
With rsCustomers
If .Supports(adAddNew) Then
.AddNew
.Fields("DrvLicNumber").Value = txtDrvLicNumber
.Fields("FirstName").Value = txtFirstName
.Fields("LastName").Value = txtLastName
.Fields("Address").Value = txtAddress
.Fields("City").Value = txtCity
.Fields("State").Value = txtState
.Fields("ZIPCode").Value = txtZIPCode
.Fields("Notes").Value = txtNotes
.Update
End If
End With
MsgBox "The customer's record has been created.", _
vbOKOnly Or vbInformation, "Bethesda Car Rental"
cmdReset_Click
rsCustomers.Close
Set rsCustomers = Nothing
Exit Sub
cmdSubmit_ClickError:
MsgBox "An error occurred when trying to create the customer. " & _
"Please report the error as follows." & vbCrLf & _
"Error #: " & Err.Number & vbCrLf & _
"Description: " & Err.Description, _
vbOKOnly Or vbInformation, "Bethesda Car Rental"
Resume Next
End Sub
- In the Object combo box, select cmdClose
- Implement the event as follows:
Private Sub cmdClose_Click()
On Error GoTo cmdClose_ClickError
DoCmd.Close
Exit Sub
cmdClose_ClickError:
MsgBox "An error occured as follows." & vbCrLf & _
"Error #: " & Err.Number & vbCrLf & _
"Message: " & Err.Description, _
vbOKOnly Or vbInformation, "Bethesda Car Rental"
Resume Next
End Sub
- Close Microsoft Visual Basic and return to Microsoft Access
- Close the NewCustomer form
- When asked whether you want to save, click Yes
|
|