Search code examples
vb6

Finding the Last Day of the Month in a Year (VB 6.0)


I'm creating a program that will predict the last day of the month. I've managed to give the months their value, but the year needs to be written as well in order to determine whether it's a leap year or not. Objects: 2 Textboxes, 1 Command Button, 1 Label

Needs to be Select case unfortunately.

My code:

Private Sub Command1_Click()
Dim month, year As String

month = txtmonth.Text
year = txtyear.Text

Select Case month
Case "Feb", "February"
        lblday.Caption = CStr(Day(DateSerial(year, 3, 1) - 1))
Case "Jan", "January", "Mar", "March", "May", "Jul", "July", "Aug", "August", "Oct", "October", Dec, December
        lblday.Caption = "It has" & " " & CStr(Day(DateSerial(year, 3 + 2, 1) - 1)) & " " & "days"
'year/30/month
Case "April", "Jun", "June", "Sept", "September", "Nov", "November"
        lblday.Caption = "It has" & " " & CStr(Day(DateSerial(year, 3 + 1, 1) - 1)) & " " & "days"
'year/31/month
End Select

End Sub

Solution

  • What is the user supposed to enter into the textbox, string or a number?

    If user input is a string, then you should put months in quotes like this Case "Feb", "February" and month needs to be declared as a String and assigned txtmonth.Text directly without Val

    If user input is a number, then you need to remove all the short and long month names from the Case like Feb and March and use a number like this Case 2 ' this is for February

    If you wish to optimize your code (assuming you absolutely have to have a Case in there), instead of asking user for a leap year you can calculate that yourself:

    Case Feb, February
         lblday.Caption = Cstr(day(DateSerial(Year(Now), 3, 1) - 1))
    

    if you didn't have to have a Case in there, all your code could of been optimized to this:

    Private Sub Command1_Click()
        lblday.Caption = CStr(Day(DateSerial(Year(Now), Month(DateValue(txtmonth.Text & " 0")) + 1, 1) - 1))
    End Sub
    

    With that code you can enter month number, month full name or month short name and it will work. It will assume the year to be the current year (you didn't specify whether user enters year or not).

    EDIT:

    I took your code and fixed the following:

    • rearranged months because some months were in the case for 30-day month and they were 31-day months and the other way around
    • changed case into lower case. This way you can enter month name in any case you wish. For example, before, only Oct would work. Now anything works: oct, OCT, oCt, OcT ...
    • changed Sept to sep to be consistant
    • changed the way you calculate days in a month. For 30 day months, you take the number of days from April with this Day(DateSerial(year, 3 + 2, 1) - 1) and for 31 day months from March with this Day(DateSerial(year, 3 + 1, 1) - 1) . What happens if in the future, we change the number of days in March? the whole logic collapses. I realize this is very unlikely, but still a possibility. There is absolutely no reason why we can't calculate each months on its own like this CStr(Day(DateSerial(year, month(DateValue(txtmonth.Text & " 0")) + 1, 1) - 1))
    • because of the thing I changed above, I had to use function called Month but you already had a badly named variable called month so it was a conflict I had to fix and thus removed variable month altogether. If you have to have it, rename it to myMonth or something.

    Some tips to have a well defined code: - rename year to myYear use camel style: change all labels and text boxes to follow this naming convention. Example: renamelbldaytolblDayandtxtmonthtotxtMonth don't use variables if you don't need to. month variable was unnecessary. You could even remove year variable too, since you do not do any calculations on it at all.

        Private Sub Command1_Click()
            Dim year As String
            year = txtyear.Text
    
            Select Case LCase(txtmonth.Text)
                Case "feb", "february"
                    lblday.Caption = CStr(Day(DateSerial(year, 3, 1) - 1))
                Case "april", "apr", "jun", "june", "sep", "september", "nov", "november"
                    lblday.Caption = "It has" & " " & CStr(Day(DateSerial(year, month(DateValue(txtmonth.Text & " 0")) + 1, 1) - 1)) & " " & "days"
                    'year/30/month
                Case "jan", "january", "mar", "march", "may", "jul", "july", "aug", "august", "oct", "october", "dec", "december"
                    lblday.Caption = "It has" & " " & CStr(Day(DateSerial(year, month(DateValue(txtmonth.Text & " 0")) + 1, 1) - 1)) & " " & "days"
                    'year/31/month
            End Select
        End Sub