Code Listing 2: The Sliding Window Procedure for 16-bit Basic (Access 2.0 and VB 3)

Function SafeCentury16 (varDateIn As Variant, intPivot As Integer) As Variant
  ' Comments  : Returns the passed date with the century modified
  '             as determined by the specified pivot year, using
  '             the following rule:
  '               Last Two Digits       Century
  '               Of Input Year         Assumed
  '               ----------------      ---------
  '               <= intPivot           21st
  '               > intPivot            20th
  '
  '             Note that any century information supplied in the
  '             varDateIn parameter is thrown away. This is because
  '             by the time a date gets to this procedure, it has
  '             most likely undergone the Access assumption rule
  '             and is therefore suspect.
  '
  '             This version is for 16-bit hosts such as Access 2.0
  '             and Visual Basic 3.0
  '
  ' Parameters: varDateIn - variant containing a date. This can be
  '             a string or date, but must be one of the following
  '             formats:
  '                           mm/dd/yy
  '                           dd/mm/yy
  '                           mm/dd/yyyy
  '                           dd/mm/yyyy
  '             intPivot - year to pivot to the current century.
  ' Returns   : variant of type 7 (date) containing the transformed
  '             date or Null if the passed value cannot be evaluated
  '             as a date or an error occurs.
  ' Example   : SafeCentury16 ("12/13/12", 29)   returns 12/12/2012
  '             SafeCentury16 ("12/13/1912", 29) returns 12/12/2012
  '             SafeCentury16 ("12/13/29", 29)   returns 12/13/2029
  '             SafeCentury16 ("12/13/30", 29)   returns 12/13/1930
  '
  Dim intYear As Integer
  Dim fIsDate As Integer
  Dim fOk As Integer

  On Error GoTo PROC_ERR

  ' Assume failure
  fOk = False

  ' Determine if the passed value can
  Select Case VarType(varDateIn)
    
    Case V_DATE
      ' Its a date, simply work on the century
      fOk = True

    Case V_STRING
      ' Its a string so we use IsDate to determine if the value is
      ' valid as a date. Note that IsDate() uses the date settings
      ' in the Windows Control Panel to determine validity. Therefore
      ' certain date formats (such as "Monday, June 3rd, 1995") will
      ' not be considered a valid date by this procedure. See the
      ' procedure comments for valid date formats for use with this
      ' procedure.
      On Error Resume Next
      fIsDate = IsDate(varDateIn)
      fOk = (Err = 0)
      On Error GoTo PROC_ERR

    Case V_LONG
      ' Check to see if it is in the serial date range
      fOk = (varDateIn > -657434 And varDateIn < 2958465)

    Case Else
      ' Can't figure out what it is
      
  End Select

  If fOk Then
    ' Get the right-most two digits of the year. Note the use of
    ' Format function to ensure that the string we search in
    ' always has four digits.
    intYear = CInt(Right$(Format(varDateIn, "yyyy"), 2))
    
    ' Pivot the century. Note the use of explicit, hard-coded
    ' centuries. While this may appear to be non-Year 2000
    ' compliant (don't hardcode century data), it does
    ' specify the full four digits of the year. And since
    ' we need to rely on this procedure to make exactly
    ' the assumption we want, the hard-coded values are correct.
    If intYear > intPivot Then
      ' If the supplied two-digit year is greater than then
      ' pivot number, the returned value is in the 20th
      ' century.
      intYear = 1900 + intYear
    Else
      ' If the supplied two-digit year is less than or equal
      ' to the pivot number, the returned value is in the
      ' 21st century.
      intYear = 2000 + intYear
    End If
              
    ' Return the variant. Note that we cast it explicitly as a date.
    ' This code explicitly creates the date in the format mm/dd/yyyy.
    ' If your date format is different, change the order of
    ' concatenation.
    SafeCentury16 = CVDate(Month(varDateIn) & "/" & Day(varDateIn) & "/" & intYear)
  Else
    SafeCentury16 = Null
  End If
  
PROC_EXIT:
  Exit Function

PROC_ERR:
  SafeCentury16 = Null
  Resume PROC_EXIT

End Function

Back to main page