Share via


Visual Basic: Bi-Directional Programming

Hijri Calendar Support

See Also

Microsoft Visual Basic 6.0 and Microsoft Access for Windows 95-Arabic Edition (Arabic Access) or later contain support for both the Hijri and Gregorian calendars. The foundation for this capability is a new Calendar property in VBA and extensions to existing date-related VBA functions.

The following paragraphs describe how to perform calendar-related functions using Visual Basic 6.0. Unlike Arabic Access, which has user interface and other extensions beyond VBA to automate creation and management of databases based on either calendar, Visual Basic requires application developers to programmatically control the calendar associated with the entry, calculation, and display of date information.

  • The Gregorian calendar is the default calendar for all date-related information and VBA functions in Arabic Visual Basic. To see or change the calendar setting to be used by VBA date-related functions, use the new VBA Calendar property. The syntax for Calendar property usage is:

    [VBA.]Calendar [=value]

Items in brackets are optional. 'value' is one of the following settings or values.

Setting Value Description
vbCalGreg 0 (Default) Gregorian calendar used for VBA date-related calculations and formatting.
vbCalHijri 1 Hijri calendar used for VBA date-related calculations and formatting.
  • When the calendar is Gregorian, the language of Gregorian month names is determined by the Date tab in the Regional Settings Control Panel of Microsoft Windows 95 (or later). When the calendar is Hijri, the language of Hijri month names is always Arabic. Hijri month names are always returned in full month name format.

  • The behavior of all date-related functions in VBA is determined by the Calendar property setting. Functions affected by the Calendar property setting include CDate, Date, DateAdd, DateDiff, DatePart, DateSerial, DateValue, Day, Format, Month, Weekday and Year.

  • The Visual Basic data type Date does not change when the calendar changes. Values of type Date are stored in a format that is not calendar-specific. This not only allows date information to be readily formatted for a particular calendar but also provides a convenient way to convert formatted date values from one calendar to another.

The following example creates a function to convert date strings between the supported calendar systems. The example code that follows uses the function to convert a Gregorian date string to the equivalent Hijri string in Long Date format.

Function ConvertDateString ( _
    ByRef StringIn As String, _
    ByRef OldCalendar As Integer, _
    ByVal NewCalendar As Integer, _
    ByRef NewFormat As String) As String
    
    Dim SavedCal As Integer
    Dim d As Date
    Dim s As String
    
    '// Save VBA Calendar setting to restore when finished
    SavedCal = Calendar
    
    '// Convert date to new calendar and format
    Calendar = OldCalendar      ' Change to StringIn calendar
    d = CDate (StringIn)      ' Convert from String to Date
    Calendar = NewCalendar      ' Change to calendar of new string
    s = CStr (d)         ' Convert to short format String
    ConvertDateString = Format _      ' Reformat
    (s, NewFormat)
    
    '// Restore VBA Calendar setting
    Calendar = SavedCal
End Function

Call ConvertDateString function from a procedure to perform conversion...

    Dim GregorianDate As String
    Dim HijriDate As String
    Dim HijriFormat As String

    GregorianDate = "12/31/93"   ' Gregorian string to convert
    HijriFormat = "Long Date"   ' Format for Hijri date

    '// Convert to Hijri date 7/8/1414 and return in Long Date format
    HijriDate = ConvertDateString (_
        GregorianDate, _
        vbCalGreg, _
        vbCalHijri, _
        HijriFormat)
  • To ensure Hijri dates strings are evaluated correctly in VBA statements, use the CDate function to evaluate the string (e.g., CDate("1/25/14")) instead of declaring the string as a constant (e.g., #1/25/14#). CDate will always return a value based on the active database calendar, whereas date constants are always evaluated using the Gregorian calendar.

  • For reliable behavior, dates should generally be entered and displayed in an unambiguous format. For example, dates entered in short date format may be misinterpreted if the year or the day of the month are 12 or less (e.g. 3/11/10). For reliable behavior, use long date format whenever possible. You can alter the way a date is displayed in long format (e.g. exclude the day of the month) by changing the format in the Windows 95 (or later system) Regional Settings Control Panel.

  • If an existing database or file with date information is opened and the wrong calendar setting has been selected, Visual Basic may report a variety of errors or other unexpected behaviors. For example, if Calendar = vbCalHijri, Visual Basic may report an error when a statement makes reference to Gregorian dates that are invalid as Hijri dates (e.g., 'February 17, 1996' because no Hijri month has the name February).