Date Window

Visual Basic 5 improves on the internal date processing of versions 3 and 4 by employing what is sometimes referred to as a date window or date windowing technique. In a nutshell, when presented with a date consisting of only two digits for the year, Visual Basic will assume that any year greater than 29 is in the 1900s and any date below 30 is in the 2000s. For example, given the year 97, because it’s greater than 29, Visual Basic will assume the date to be (and convert it to) 1997. On the other hand, given the year 12, because it’s less than 30, Visual Basic will assume it represents 2012.

This is definitely a step in the right direction for many corporations and will go a long way toward minimizing the impact of the year 2000 on Visual Basic applications. Before porting all your existing applications to Visual Basic 5 and forgetting about the whole Y2K issue, however, you should bear in mind the implications that a fixed date window, such as 1930, can have.

Obviously, depending on the nature of your applications and the scope of any date processing that your applications might handle, I’d be very cautious before committing a Visual Basic application to be Year 2000 compliant without thoroughly investigating each line of code and testing the application to the hilt. Consider the example in Listing 9-1, which isn’t necessarily a business application but nonetheless demonstrates the dangers of presuming the context of an application and not supplying a user-defined, sliding-date–range window. This code is used to determine whether the user can afford his or her dream home at retirement age. The subject could just as easily be a pension plan, an insurance policy, an application, and so on. Enter the information required as you are prompted. Try this a number of times using different dates with two-digit years, both before and after 20 (1/1/18, 1/1/42). This sample is available on the companion CD in CHAP09\DateTest.bas.

Listing 9-1

Using Visual Basic 5’s date window in calculating retirement information

‘ **************************************************************
‘ *
' * Purpose - To retrieve details concerning the user’s planned
‘ *           savings, interest rate, retirement date, and the
‘ *           cost of the dream retirement home.  After this is 
‘ *           done, a function will be called to determine 
‘ *           whether the user can afford the dream retirement 
‘ *           home.
‘ *           
‘ *           Finally, a message will be displayed 
‘ *           indicating whether the home can be afforded.
‘ *
‘ *  Author - Mark Mayes (TMS)
‘ *    Date - 09/09/1996
‘ *
‘ *  Inputs - None
‘ * Outputs - None
‘ * Returns - None
‘ *
‘ *Notes - The point of this program is to demonstrate the
‘ *           use of VB5's date window. Any two-digit year
‘ *           greater than '29' is interpreted as being in the
‘ *           twentieth century; that is, 34 is treated as 1934.
‘ *           Any date less than 30 is interpreted as
‘ *           being in the twenty-first century; that is, 
‘ *           12 is treated as 2012.
‘ *
‘ *           When running this program, experiment with
‘ *           entering different retirement dates. Vary the
‘ *           number of year digits from two to four, and enter
‘ *           various dates either side of “30,” be it 1930 or
‘ *           2030.
‘ * 
‘ *           Please note that because this is a simple demonstration, 
‘ *           there is absolutely NO validation of user input in the
‘ *           program, so it is VERY easy to break!
‘ **************************************************************
Sub Main

Dim fMonthlySavings As Single    ' The amount to be saved
Dim fAPR               As Single    ' The annual % rate
Dim dteRetirementDate  As Date      ' The retirement date
Dim curCostOfHome      As Currency  ' Retirement home cost
Dim fSavings           As Single    ' Amount that will be 
                                    ' saved

' Retrieve from the user the amount that can be saved each
' month.
fMonthlySavings = InputBox("Monthly Savings")

' Retrieve from the user the annual percentage rate on the
' savings.
fAPR = InputBox("Enter the expected annual percentage rate.")

' Retrieve from the user the date of retirement.
dteRetirementDate = InputBox("What is your retirement date?")

' Retrieve from the user the cost of the dream retirement home.
curCostOfHome = InputBox("How much is the retirement home?")

' Call the function to establish whether the home can be 
' afforded.
Listing 9-1 If mbCanIAffordIt(fMonthlySavings, _
                  fAPR, _
                  dteRetirementDate, _
                  curCostOfHome, _
                  fSavings) Then
    ' The home CAN be afforded, so display a happy message.
    MsgBox "Whoopee! - As you will have saved " _
        & Format(fSavings, "Currency") _
        & ", you CAN afford your dream retirement home."
Else
    ' The home can NOT be afforded, so display a sad message!
    MsgBox "Boohoo! - As you will have saved only " _
        & Format(fSavings, "Currency") _
        & ", you CAN'T afford your dream retirement home."
End If
End Sub



‘ **************************************************************
‘ *
‘ * Purpose - Based on the input parameters, calculate 
‘ *           whether the user’s savings over the specified 
‘ *           period of time will cover the cost of the 
‘ *           desired item.
‘ *
‘ *  Author - Mark Mayes (TMS)
‘ *    Date - 09/09/1996
‘ *
‘ *  Inputs - ifMonthlySavings - The monthly savings
‘ *           ifAPR            - The APR on the savings
‘ *           idteEndDate      - The date when saving ceases
‘ *           icurCostOfItem   - The cost of the desired item
‘ *
‘ * Outputs - iofSavings       - The total amount that will
‘ *                                have been saved
‘ *
‘ * Returns - True if the desired item can be afforded.  False
‘ *           otherwise.
‘ *
‘ *   Notes - I'm not a financial whiz kid!!!  The interest
‘ *           calculations are probably all wrong.  However,
 Listing 9-1 ‘ *           the point of the function is to watch for the
‘ *           date calculations.
‘ *
‘ *           Please note that because this is a simple demonstration,  
‘ *           there is absolutely NO validation of user input in the
‘ *           program, so it is VERY easy to break!
‘ **************************************************************
Private Function mbCanIAffordIt(ByVal ifMonthlySavings _ 
                                      As Single, _
                                ByVal ifAPR As Single, _
                                ByVal idteEndDate As Date, _
                                ByVal icurCostOfItem _ 
                                      As Currency, _
                                ByRef iofSavings As Single) _
                                      As Boolean

    Dim nNumberOfPayments As Integer ' Calculated payments

    ' Establish whether a positive APR was entered.
    If ifAPR > 0 Then
       ' Yes, we have an APR, so convert it to a monthly
       ' percentage rate.
       ifAPR = (ifAPR / 100) / 12
    End If

    ' Calculate the number of monthly payments remaining until
    ' saving ceases.

    ' Notice that we are attempting to force the end date to a
    ' four-digit year.
    nNumberOfPayments = DateDiff("m", Now, _ 
                                 Format(idteEndDate, _
                                        "dd/mm/yyyy"))

    ' Calculate the total amount that will be saved.
    iofSavings = FV(ifAPR, nNumberOfPayments, _
                    -ifMonthlySavings)

    ' Set the return code of this function depending on whether
    ' or not the total savings meet or exceed the cost of the
    ' desired item.
    mbCanIAffordIt = iofSavings >= icurCostOfItem

End Function

Needless to say, the code in Listing 9-1 won’t win any prizes for its complexity, but it does demonstrate the very real danger attached to the date window used in Visual Basic 5. So don’t assume that all your troubles are over. You’ll still need to check all your applications before giving them the Year 2000–compliant seal of approval.