?
Solved

Removing items from a combo box

Posted on 2003-03-19
7
Medium Priority
?
244 Views
Last Modified: 2010-05-01
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
0
Comment
Question by:MattRawling
[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
7 Comments
 
LVL 3

Accepted Solution

by:
DeAn earned 80 total points
ID: 8167469
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
 
LVL 1

Expert Comment

by:Due
ID: 8167599
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
 

Expert Comment

by:R2DTOEA
ID: 8168025
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
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!

 
LVL 9

Expert Comment

by:dmang
ID: 8169272
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
 

Expert Comment

by:R2DTOEA
ID: 8169631
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
 

Expert Comment

by:CleanupPing
ID: 8531991
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
 
LVL 6

Expert Comment

by:GPrentice00
ID: 8957341
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

800 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