Solved

Month / Year Picker

Posted on 2004-09-30
8
910 Views
Last Modified: 2010-05-19
Looking for something like DTPicker but only for Month / Year combo.

Thanks in advance,
-Alex
0
Comment
Question by:AlexF777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12189471
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
 
LVL 8

Accepted Solution

by:
mladenovicz earned 150 total points
ID: 12189487
Private Sub Form_Load()
    DTPicker1.Format = dtpCustom
    DTPicker1.CustomFormat = "MM/yyyy"
    DTPicker1.UpDown = True
End Sub
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12189755
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
Independent Software Vendors: 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!

 

Author Comment

by:AlexF777
ID: 12190720

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

Thank You,
-Alex
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12190861
I think the correct format string is:

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

Then the month will cycle correctly
0
 

Author Comment

by:AlexF777
ID: 12191011
somehow, month is varying between 00 and 59 and then goes back to 0 ...
0
 
LVL 32

Assisted Solution

by:Erick37
Erick37 earned 100 total points
ID: 12191049
Make sure month is "MM" and not "mm"

"MM" is month
"mm" is minutes
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12191054
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question