Handling months and years

This tip discusses handling months and years in Visual Basic.

Handling months and years
Bayer White

Reader Bayer White offers an efficiency boost in dealing with the date we all deal with year in and year out.

From most of the code I've seen from other developers, I think they lean toward hard coding Month Names and 4 digit years into combo boxes. This can take unneeded lines of code, and it is not very manageable if requirements change in the future. That's why I decided to write a more effecient, manageable way of dealing with dates. Hope this helps!!!

**********Loading Full Month or Full Year into a combo box***********

Option Explicit

Private Enum DateType
    FullMonth = 1
    FourDigitYear = 2
End Enum

Private Sub LoadDateComboBoxes(cboBox As ComboBox, _
                               DateFormat As DateType)

Dim intMonthCtr As Integer

    Select Case DateFormat
    Case 1 'Just full month
        For intMonthCtr = 1 To 12
            cboBox.AddItem Format(DateAdd("m", intMonthCtr, "12/1"), "mmmm")
            cboBox.ItemData(cboBox.NewIndex) = Month(DateAdd("m", 
intMonthCtr, "12/1"))
    Case 2 'Just 4 digit year
        For intMonthCtr = -2 To 10
            cboBox.AddItem Year(DateAdd("yyyy", intMonthCtr, Now))
    End Select
End Sub

Thanks, Bayer. For your interest in SearchVB, we'll be sending you a free denim shirt.

Bayer White is a Senior VB Developer for Baywood Technologies, in Jacksonville Florida.

This was first published in February 2001
This Content Component encountered an error



Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: