Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

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
0
MattRawling
Asked:
MattRawling
1 Solution
 
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
 
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
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!

 
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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