Car Rental Calculation


The Edsel Car Rental Company rents Edsels for $35 per day plus 10 cents a mile.  Write a program for the rental company so that the clerk can enter the date that the car was rented, the date that the car was returned, the mileage when the car was rented, and the mileage when the car was returned.  Calculate the total charges and display the results.


Note: Use the DateDiff function to determine the number of days that the car was rented.


Before you start this program, do the following tutorial to demonstrate the use of the DateDiff function.


STEP 1: Create a form that looks like the following.  Name the controls as indicated on the call-outs.



STEP 2:  Place the following code in the cmdDaysDiff_Click event:


Private Sub cmdDaysDiff_Click()


    Dim dtmDate1    As Date

    Dim dtmDate2    As Date

    Dim intDays     As Integer


    dtmDate1 = CDate(txtDate1)

    dtmDate2 = CDate(txtDate2)

    intDays = DateDiff("d", dtmDate1, dtmDate2)

    lblDaysDiff = intDays


End Sub


An explanation of the code above follows:


First, two Date variables, dtmDate1 and dtmDate2 are declared.  These will store the two dates we are interested in.  An Integer variable intDays is declared to store the number of days difference between the two dates.


The statements


dtmDate1 = CDate(txtDate1)

dtmDate2 = CDate(txtDate2)


use the CDate function to convert the dates entered in the textboxes to Date datatypes.  (The data type of data entered in textboxes is String, and those strings must be converted to Date data types in order for the DateDiff function to work).  The CDate function recognizes dates in many different formats, such as "11/29/99", "November 29, 1999",  "29-Nov-99", and can therefore convert these strings into internal Date formats.  If the CDate function does not recognize the string as a valid date, the program will crash.


The statement


intDays = DateDiff("d", dtmDate1, dtmDate2)


executes the DateDiff function and stores the result in the intDays variable.  The DateDiff function takes three main arguments: The first argument specifies the interval of time (days, weeks, months, quarters, years, etc.) used to calculate the difference between the two dates.  In this case we used "d" because we are interested in days.  The second and third arguments specify the two dates to be compared.  As a rule of thumb, Date1 should be earlier than Date2.


The statement


lblDaysDiff = intDays


causes the result to be displayed in the label.


STEP 3:   Run the program and test it with various dates:



Download the VB project code for this example here.


A sample run of the rent-a-car program should look something like the following:




Note: To display the picture of the cars on the form as shown above, perform the following steps at design-time.


1.         Place an Image control on the form and move it to the upper left-hand corner of the form.


2.         Go to the property sheet for the Image control, and locate the Picture property.


3.         Click the ellipsis button (...) on the property.  This will bring up the "Load Picture" dialog box.


4.         Navigate to the following folder:

Program Files\Microsoft Visual Studio\Common\Graphics\Icons\Industry.


5.         Select the CARS.ICO file and click Open. The picture should then appear on your form.


Note:    When Visual Studio 6 or VB 6 is installed, the Graphics folder is NOT installed by default (whoever is installing it must explicitly check the "Graphics" option during the appropriate step of the installation). If you do not have this folder on your system, you can obtain it from the appropriate VS/VB CD, or use some other icon file, or skip the icon altogether – it is for aesthetic purposes only and has no impact on the functionality of the program.


Download the solution for this project here.