Results 1 to 9 of 9

Thread: [RESOLVED] How to display all dates of a selected month in separate labels.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    25

    Resolved [RESOLVED] How to display all dates of a selected month in separate labels.

    Hello,

    I am currently working on a project in VB6 and I need a little help.

    I have a combo box which I will use to select the month.
    Once I select the month, I want the labels(control array) below to automatically populate with the dates of the month.

    I am trying to use nested For statements for the same but am unable to figure out the right way.

    I have attached a screenshot of my form which might be useful.

    In the form, when I select the EmpName and the Month, I want the Label1(s) below to auto-populate with the dates of the selected month.

    Any help on this would be greatly appreciated.
    Attached Images Attached Images  

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: How to display all dates of a selected month in separate labels.

    I think you'll have to clarify a bit. You have 10 'Labels' which look like ComboBoxes to me, what exactly do you want to display in the 'labels'?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    25

    Re: How to display all dates of a selected month in separate labels.

    Doogle,

    There are labels and combo boxes below them.

    When I select the month in the combo box at the top, I want the label's captions to fill in with the dates of the selected month.

    And then, that will allow me to select a value for the particular date.

    The labels are headings for those combo boxes in the frame and the value of those headings should be the dates of the month.

    The month will be selected in the combo box which says "Select Month".

    Thank you in advance.

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: How to display all dates of a selected month in separate labels.

    It must be too early in the morning for me, but all months have more than 27 days yet you have only 10 Labels.

    Please could you give an example of what you'd expect in the 10 labels if I chose September as the Month?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    25

    Re: How to display all dates of a selected month in separate labels.

    Right now I am trying to get 10 dates in the label boxes.

    I will add 21 labels and combo boxes more once I get it right for 10.

    The form is still "Under Construction".

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: How to display all dates of a selected month in separate labels.

    You need to derive the number of days in the selected Month (and Year will be important to cater for February in Leap Years) and then set up a For /Next loop to populate the Captions.
    Code:
    Private Sub cmbMonth_Click()
    Dim intI As Integer
    Dim intDays As Integer
    intDays = DaysInMonth(cmbMonth.Text)
    If intDays <> -1 Then
        For intI = 1 To intDays
            label1(intI - 1).Caption = CStr(intI)
        Next intI
    Else
        MsgBox "Invalid Month or year"
    End If
    End Sub
    
    Private Function DaysInMonth(strMonth As String, Optional strYear As String = vbNullString) As Integer
    If strYear = vbNullString Then strYear = Format(Now, "YYYY")
    Select Case True
        Case IsDate("31/" & strMonth & "/" & strYear)
            DaysInMonth = 31
         Case IsDate("30/" & strMonth & "/" & strYear)
            DaysInMonth = 30
        Case IsDate("29/" & strMonth & "/" & strYear)
            DaysInMonth = 29
        Case IsDate("28/" & strMonth & "/" & strYear)
            DaysInMonth = 28
        Case Else
            DaysInMonth = -1
    End Select
    End Function
    The above assumes that the Label for January is Label1(0), by default the Year is assumed to be the current year.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    25

    Re: How to display all dates of a selected month in separate labels.

    Thanks Doogle,

    That works like a charm...

  8. #8
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,346

    Re: How to display all dates of a selected month in separate labels.

    If you have finished with the thread please mark it "Resolved".
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  9. #9
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: [RESOLVED] How to display all dates of a selected month in separate labels.

    Another Example:

    I used cmbMonths as combo's name. DateSerial creates a date given day + month + year, DateAdd adds or substracts days/months/years to any given Date.

    Questions:
    1) What about the year? Shouldn't the user be able to also select a year? (In this example i used current year).
    2) What will you do with labels that won't be used? You could keep all labels invisible when you clear and make visible only those you'll use, like i did here with line Label1.Item(i - 1).Visible = True, but you could also just clear text on them if you use transparent labels, decide what's best for you.

    Code:
    Private Sub FillDays()
    Dim i As Long, LngMonth As Long, LngLastDayInMonth As Long, DateFirstDayNextMonth As Date
    
        LngMonth = cmbMonths.ListIndex + 1
        
        'We create a new Date Variable that will be on the first day next month
        DateFirstDayNextMonth = DateAdd("M", 1, DateSerial(Year(Date), LngMonth, 1))
        
        'We substract one day from previous Date to get last day in this Month:
        LngLastDayInMonth = Day(DateAdd("D", -1, DateFirstDayNextMonth))
        
        'Fill labels with dates
        For i = 1 To LngLastDayInMonth
            Label1.Item(i - 1).Visible = True
            Label1.Item(i - 1).Caption = FormatDateTime(DateSerial(Year(Date), LngMonth, i), vbShortDate)
        Next
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width