• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 916
  • Last Modified:

Month / Year Picker

Looking for something like DTPicker but only for Month / Year combo.

Thanks in advance,
-Alex
0
AlexF777
Asked:
AlexF777
  • 2
  • 2
  • 2
  • +2
2 Solutions
 
Jaime OlivaresCommented:
You need to specify a custom formatting string, take a look to this previous question:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20119599.html
0
 
mladenoviczCommented:
Private Sub Form_Load()
    DTPicker1.Format = dtpCustom
    DTPicker1.CustomFormat = "MM/yyyy"
    DTPicker1.UpDown = True
End Sub
0
 
fds_fatboyCommented:
This is a code solution, I hope it helps:

Create a new VB standard project with a single form (Form1). Place a ComboBox (Combo1) on it and set the style to 2 (Dropdown Listbox). Place the following code in the form:

--------------------
Option Explicit

Private Sub Combo1_Click()
    '*** Test code
    If Combo1.ListIndex > -1 Then
        MsgBox "You selected " & Format$(Combo1.ItemData(Combo1.ListIndex), "yyyy mm")
    End If
    '*** /Test code
End Sub

Private Sub Form_Load()
    '*** Test code
    FillMonthYearCombo Combo1, #3/1/2002#, Now
    '*** /Test code
End Sub

Private Sub GetMonthYear(ByVal MYdate As Date, ByRef MYmonth As Integer, ByRef MYYear As Integer)
    MYmonth = DatePart("m", MYdate)
    MYYear = DatePart("yyyy", MYdate)
End Sub

Private Sub AddMonthYearItem(cboMonthYear As ComboBox, ByVal nd As Date)
    With cboMonthYear
        .AddItem Format$(nd, "mmmm yyyy")
        .ItemData(.NewIndex) = CLng(nd)
    End With
End Sub

Private Function FillMonthYearCombo(cboMonthYear As ComboBox, ByVal StartDate As Date, ByVal EndDate As Date)
    Dim sm As Integer
    Dim sy As Integer
    Dim em As Integer
    Dim ey As Integer
    Dim yl As Integer
    Dim ml As Integer

    cboMonthYear.Clear

    If StartDate > EndDate Then
        Exit Function
    End If

    GetMonthYear StartDate, sm, sy
    GetMonthYear EndDate, em, ey

    If ey = sy Then
        For ml = sm To em
            AddMonthYearItem cboMonthYear, DateSerial(sy, ml, 1)
        Next
    Else
        For ml = sm To 12
            AddMonthYearItem cboMonthYear, DateSerial(sy, ml, 1)
        Next
        For yl = sy + 1 To ey - 1
            For ml = 1 To 12
                AddMonthYearItem cboMonthYear, DateSerial(yl, ml, 1)
            Next
        Next
        For ml = 1 To em
            AddMonthYearItem cboMonthYear, DateSerial(ey, ml, 1)
        Next
    End If
End Function

--------------------
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AlexF777Author Commented:

I like mladenovicz's solution, only would be good if my month could stay between 1 and 12

Thank You,
-Alex
0
 
Erick37Commented:
I think the correct format string is:

DTPicker1.CustomFormat = "MM/yyy" '<< 3 y's, not 4

Then the month will cycle correctly
0
 
AlexF777Author Commented:
somehow, month is varying between 00 and 59 and then goes back to 0 ...
0
 
Erick37Commented:
Make sure month is "MM" and not "mm"

"MM" is month
"mm" is minutes
0
 
mladenoviczCommented:
works fine for me (month is between 1 and 12).

are you sure you are using

 DTPicker1.CustomFormat = "MM/yyyy"

not

 DTPicker1.CustomFormat = "mm/yyyy"
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now