Handling months and years

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

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.