Removing items from a combo box

I have 2 combo boxes, combo1 and combo 2. They display the date. I need it so that if say February is selcted in combo 1 then the number 31 is removed from combo 2 because you obviously cant have the 31st of february.

Cheers
MattRawlingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DeAnCommented:
Private Sub cboMonths_Click()
  Dim DaysInMonth As Integer, i As Integer
  ' I used the cboMonths.ItemData list to hold number of days, 31,28,....
  ' corrosponding to cboMonths.list, January, Februray, etc...
  DaysInMonth = cboMonths.ItemData(cboMonths.ListIndex)
  cboDays.Clear
  For i = 1 To DaysInMonth
    cboDays.AddItem i
  Next i
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DueCommented:
Private Sub Combo1_LostFocus()
Dim i As Integer
Combo2.Clear
mend = GetDays(Combo1.ListIndex)
For i = 1 To mend
Combo2.AddItem i
Next
End Sub

Private Sub Form_Load()
Combo1.AddItem "Select Month"
Combo1.AddItem "Jan"
Combo1.AddItem "Feb"
Combo1.AddItem "Mar"
Combo1.AddItem "Apr"
' ...........
End Sub
Function GetDays(mth As Integer) As Integer

If (mth = 1) Or (mth = 3) Or (mth = 5) Or (mth = 7) Or (mth = 8) Or (mth = 10) Or (mth = 12) Then
GetDays = 31
Else
GetDays = 30
End If
If mth = 2 Then GetDays = 28
' Related with years it could be 29 ad it below
End Function
0
R2DTOEACommented:
This builds your first combo values using the vb date functions and then in the click event of the month combo box - combo1, this rebuilds the number of days in combo2 based on the month.

Private Sub Combo1_Click()
    Dim i As Integer
   
    Dim dteCurDate As Date
   
    Me.Combo2.Clear
   
    dteCurDate = Me.Combo1.ItemData(Me.Combo1.ListIndex) & "/1/" & Format(Now, "yyyy")
    Do Until Format(dteCurDate, "mmmm") <> Me.Combo1.Text
        Me.Combo2.AddItem Format(dteCurDate, "dd")
        dteCurDate = DateAdd("d", 1, dteCurDate)
        DoEvents
    Loop
   
    If Me.Combo2.ListCount > 0 Then Me.Combo2.ListIndex = 0
End Sub

Private Sub Form_Load()
    Dim i As Integer
    Dim dteCurDate As Date

    dteCurDate = "01/01/" & Format(Now, "yyyy")
    For i = 0 To 11
        Me.Combo1.AddItem Format(DateAdd("m", i, dteCurDate), "mmmm"), i
        Me.Combo1.ItemData(i) = i + 1
    Next i
   
    Me.Combo1.ListIndex = 0
   
End Sub
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

dmangCommented:
Here's a couple of simple subs that will load your two combo's starting with the current month & year (in cbomth) and populate the second one(cboday) with the max number of days for that month...fully respecting leap-years...



Private Sub cboMth_Click()

    Dim d As Integer
    Dim strdate As String
    strdate = DateAdd("m", 1, cboMth.Text & " 01")
   
    cboDay.Clear
    For d = 0 To Day(DateAdd("d", -1, strdate)) - 1
        cboDay.AddItem Trim(Str(d + 1))
    Next d

End Sub

Private Sub Form_Load()

    Dim m As Integer
   
    cboMth.Clear
    cboDay.Clear
   
    strDte = Format(Now, "mmm yyyy")
    For m = 0 To 11
        cboMth.AddItem Format(DateAdd("m", m, Now), "mmm yyyy")
    Next m

End Sub
0
R2DTOEACommented:
To alter mine to start from the current month and to include year - the same results as dmang, it is only five line changes.  Here is my above code example, but with those five changes:

Private Sub Combo1_Click()
   Dim i As Integer
   
   Dim dteCurDate As Date
   
   Me.Combo2.Clear
   
   dteCurDate = Me.Combo1.ItemData(Me.Combo1.ListIndex) & "/1/" & Right(Me.Combo1.Text, 4)
   Do Until Format(dteCurDate, "mmmm yyyy") <> Me.Combo1.Text
       Me.Combo2.AddItem Format(dteCurDate, "dd")
       dteCurDate = DateAdd("d", 1, dteCurDate)
       DoEvents
   Loop
   
   If Me.Combo2.ListCount > 0 Then Me.Combo2.ListIndex = 0
End Sub

Private Sub Form_Load()
   Dim i As Integer
   Dim dteCurDate As Date

   dteCurDate = Format(Now, "mm") & "/01/" & Format(Now, "yyyy")
   For i = 0 To 11
       Me.Combo1.AddItem Format(DateAdd("m", i, dteCurDate), "mmmm yyyy"), i
       Me.Combo1.ItemData(i) = Format(DateAdd("m", i, dteCurDate), "m")
   Next i
   
   Me.Combo1.ListIndex = 0
   
End Sub
0
CleanupPingCommented:
Hi MattRawling,
This old question (QID 20556219) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0
GPrentice00Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

-->Accept DeAn's comments as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

GPrentice00
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.