FEBRUARY problem

User avatar
sal21
PlatinumLounger
Posts: 4339
Joined: 26 Apr 2010, 17:36

FEBRUARY problem

Post by sal21 »

Why the code dont retrieve febrary month 28 or 29 days based ANNO?

Code: Select all

Private Sub FILL_LISTVIEW()

    Dim ITMX As ListItem
    Dim strDay As String
    Dim D As Long
    Dim M As Long
    Dim STRDATA As String

    With ListView1

        .LabelEdit = lvwManual
        .FullRowSelect = True
        .View = lvwReport
        .ListItems.Clear
        .Sorted = False

        For D = 1 To 31
            STRDATA = Format(D, "00") & "/" & "01/" & ANNO
            Set ITMX = .ListItems.Add(, , STRDATA)
            For M = 2 To 12
                strDay = vbNullString
                Select Case M
                Case 2
                    If D < 29 Then
                        STRDATA = Format(D, "00") & "/" & Format(M, "00") & "/" & ANNO
                        strDay = STRDATA
                    End If
                Case 4, 6, 9, 11
                    If D < 31 Then
                        STRDATA = Format(D, "00") & "/" & Format(M, "00") & "/" & ANNO
                        strDay = STRDATA
                    End If
                Case 3, 5, 7, 8, 10, 12
                    STRDATA = Format(D, "00") & "/" & Format(M, "00") & "/" & ANNO
                    strDay = STRDATA
                End Select
                ITMX.ListSubItems.Add , , strDay
            Next
        Next

        Set ITMX = Nothing

    End With

End Sub
other way are welcome!

User avatar
HansV
Administrator
Posts: 78379
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: FEBRUARY problem

Post by HansV »

Because you use

Code: Select all

                Case 2
                    If D < 29 Then
You don't check for leap years. Change those lines to

Code: Select all

                Case 2
                    If D < 29 - (ANNO Mod 4 = 0) Then
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4339
Joined: 26 Apr 2010, 17:36

Re: FEBRUARY problem

Post by sal21 »

HansV wrote:
01 Sep 2022, 09:59
Because you use

Code: Select all

                Case 2
                    If D < 29 Then
You don't check for leap years. Change those lines to

Code: Select all

                Case 2
                    If D < 29 - (ANNO Mod 4 = 0) Then
:cheers:

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: FEBRUARY problem

Post by SpeakEasy »

Mind you, you can do this a little more cleanly ...

Code: Select all

Public Sub FILL_LISTVIEW()

    Dim ITMX As ListItem
    Dim d As Long
    Dim m As Long
    Dim STRDATA As String
    
    With ListView1

        .LabelEdit = lvwManual
        .FullRowSelect = True
        .View = lvwReport
        .ListItems.Clear
        .Sorted = False

        For d = 1 To 31
            STRDATA = Format(DateSerial(anno, 1, d), "short date")
            Set ITMX = .ListItems.Add(, , STRDATA)
            For m = 2 To 12
                STRDATA = vbNullString
                If d <= Day(DateSerial(anno, m + 1, 0)) Then
                    STRDATA = Format(DateSerial(anno, m, d), "short date")
                End If
                ITMX.ListSubItems.Add , , STRDATA
            Next
        Next

        Set ITMX = Nothing

    End With

End Sub

User avatar
sal21
PlatinumLounger
Posts: 4339
Joined: 26 Apr 2010, 17:36

Re: FEBRUARY problem

Post by sal21 »

SpeakEasy wrote:
02 Sep 2022, 10:32
Mind you, you can do this a little more cleanly ...

Code: Select all

Public Sub FILL_LISTVIEW()

    Dim ITMX As ListItem
    Dim d As Long
    Dim m As Long
    Dim STRDATA As String
    
    With ListView1

        .LabelEdit = lvwManual
        .FullRowSelect = True
        .View = lvwReport
        .ListItems.Clear
        .Sorted = False

        For d = 1 To 31
            STRDATA = Format(DateSerial(anno, 1, d), "short date")
            Set ITMX = .ListItems.Add(, , STRDATA)
            For m = 2 To 12
                STRDATA = vbNullString
                If d <= Day(DateSerial(anno, m + 1, 0)) Then
                    STRDATA = Format(DateSerial(anno, m, d), "short date")
                End If
                ITMX.ListSubItems.Add , , STRDATA
            Next
        Next

        Set ITMX = Nothing

    End With

End Sub
nice!
bravo!

but have an idea to fill the 12 column in a msflexgrid?

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: FEBRUARY problem

Post by SpeakEasy »

Sure. Even easier.

Code: Select all

' Assumes we have a 12C x 31R flexgrid with no fixed row or column
Public Sub FillFlexgrid()
    Dim M As Long
    Dim D As Long
    
    MSFlexGrid1.Redraw = False ' speed up filling the grid by delaying redraw until all cells populated
    For M = 1 To 12
        'MSFlexGrid1.ColWidth(m - 1) = 1200 ' set col width if you need to
        For D = Day(DateSerial(ANNO, M, 1)) To Day(DateSerial(ANNO, M + 1, 0))
            MSFlexGrid1.Col = M - 1
            MSFlexGrid1.Row = D - 1
            MSFlexGrid1.Text = Format(DateSerial(ANNO, M, D), "short date")
        Next
    Next
    MSFlexGrid1.Redraw = True
End Sub

User avatar
sal21
PlatinumLounger
Posts: 4339
Joined: 26 Apr 2010, 17:36

Re: FEBRUARY problem

Post by sal21 »

SpeakEasy wrote:
02 Sep 2022, 12:11
Sure. Even easier.

Code: Select all

' Assumes we have a 12C x 31R flexgrid with no fixed row or column
Public Sub FillFlexgrid()
    Dim M As Long
    Dim D As Long
    
    MSFlexGrid1.Redraw = False ' speed up filling the grid by delaying redraw until all cells populated
    For M = 1 To 12
        'MSFlexGrid1.ColWidth(m - 1) = 1200 ' set col width if you need to
        For D = Day(DateSerial(ANNO, M, 1)) To Day(DateSerial(ANNO, M + 1, 0))
            MSFlexGrid1.Col = M - 1
            MSFlexGrid1.Row = D - 1
            MSFlexGrid1.Text = Format(DateSerial(ANNO, M, D), "short date")
        Next
    Next
    MSFlexGrid1.Redraw = True
End Sub
GREAT!

PROBS...

1) i need to insert the name of month in first row for each column
2) i need to center the date in each columns
3) clear all before to set a new ANNO from combobox click event

my last code:

Code: Select all

Option Explicit
Private Sub Form_Load()

Call FillFlexgrid

End Sub
Public Sub FillFlexgrid()

    Dim M As Long
    Dim D As Long
    Dim ANNO As Long
    
    ANNO = 2022
    
    MSFlexGrid.Redraw = False ' speed up filling the grid by delaying redraw until all cells populated
    For M = 1 To 12
        MSFlexGrid.ColWidth(M - 1) = 1200 ' set col width if you need to
        For D = Day(DateSerial(ANNO, M, 1)) To Day(DateSerial(ANNO, M + 1, 0))
            MSFlexGrid.Col = M - 1
            MSFlexGrid.Row = D - 1
            MSFlexGrid.Text = Format(DateSerial(ANNO, M, D), "short date")
        Next
    Next
    MSFlexGrid.Redraw = True
End Sub

You do not have the required permissions to view the files attached to this post.

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: FEBRUARY problem

Post by SpeakEasy »

>i need to

It'd be helpful if you set out your requirements at the outset.

Anyway, this is trivial. There are several approaches you could take. Here are two. Note in the first that there is almost no change to the FillFlexGrid routine

Code: Select all

Option Explicit
Public ANNO As Long 

Public Sub FillFlexgrid()
    Dim M As Long
    Dim D As Long
    
    MSFlexGrid1.Redraw = False ' speed up filling the grid by delaying redraw until all cells populated
    For M = 1 To 12
        MSFlexGrid1.ColWidth(M - 1) = 1100 ' set col width if you need to
            For D = Day(DateSerial(ANNO, M, 1)) To Day(DateSerial(ANNO, M + 1, 0))
                MSFlexGrid1.Col = M - 1
                MSFlexGrid1.Row = D '- 1
                MSFlexGrid1.Text = Format(DateSerial(ANNO, M, D), "short date")
            Next
        'End If
    Next
    MSFlexGrid1.Redraw = True
End Sub

Public Sub ResetGrid()
    ' Set up our flexgrid rows and columns
    MSFlexGrid1.FixedRows = 1
    MSFlexGrid1.FixedCols = 0
    MSFlexGrid1.Rows = 32
    MSFlexGrid1.Cols = 12
    MSFlexGrid1.FormatString = "^JAN|^FEB|^MAR|^APR|^MAY|^JUN|^JUL|^AUG|^SEP|^OCT|^NOV|^DEC" ' read the documentation to understand this
    FillFlexgrid
End Sub

Private Sub Combo1_Click()
    ANNO = Combo1 ' Pick up Anno from combobox
    ResetGrid ' redo the grid with newly selected Anno
End Sub

Private Sub Form_Initialize()
    'Load combo with a few years for sake of example  - obviously in your code you populate this however you like
    Combo1.AddItem "2020"
    Combo1.AddItem "2021"
    Combo1.AddItem "2022"
    Combo1.AddItem "2023"
End Sub
Here's the second. Note the additional code in FillFlexGrid which is doing all the work that was done by the FormatString method in the ResetGrid routine of the first example

Code: Select all

Option Explicit
Public ANNO As Long  '= 2021

' Assumes we have a 12C x 31R flexgrid with no fixed row or column
Public Sub FillFlexgrid()
    Dim M As Long
    Dim D As Long

    MSFlexGrid1.Redraw = False ' speed up filling the grid by delaying redraw until all cells populated
    For M = 1 To 12
        MSFlexGrid1.Row = 0
        MSFlexGrid1.Col = M - 1
        MSFlexGrid1.Text = Format(DateSerial(ANNO, M, 1), "mmm")
        MSFlexGrid1.ColWidth(M - 1) = 1100 ' set col width if you need to
        MSFlexGrid1.ColAlignment(M - 1) = flexAlignCenterCenter
        If M > 0 Then
            For D = Day(DateSerial(ANNO, M, 1)) To Day(DateSerial(ANNO, M + 1, 0))
                MSFlexGrid1.Col = M - 1
                MSFlexGrid1.Row = D '- 1
                MSFlexGrid1.Text = Format(DateSerial(ANNO, M, D), "short date")
            Next
        End If
    Next
    MSFlexGrid1.Redraw = True
End Sub

Public Sub ResetGrid()
    ' Set up our flexgrid rows and columns
    MSFlexGrid1.FixedRows = 1
    MSFlexGrid1.FixedCols = 0
    MSFlexGrid1.Rows = 32
    MSFlexGrid1.Cols = 12
    FillFlexgrid
End Sub

Private Sub Combo1_Click()
    ANNO = Combo1 ' Pick up Anno from combobox
    ResetGrid ' redo the grid with newly selected Anno
End Sub

Private Sub Form_Initialize()
    'Load combo with a few years for sake of example  - obviously in your code you populate this however you like
    Combo1.AddItem "2020"
    Combo1.AddItem "2021"
    Combo1.AddItem "2022"
    Combo1.AddItem "2023"
End Sub

User avatar
sal21
PlatinumLounger
Posts: 4339
Joined: 26 Apr 2010, 17:36

Re: FEBRUARY problem

Post by sal21 »

SpeakEasy wrote:
02 Sep 2022, 19:16
>i need to

It'd be helpful if you set out your requirements at the outset.

Anyway, this is trivial. There are several approaches you could take. Here are two. Note in the first that there is almost no change to the FillFlexGrid routine

Code: Select all

Option Explicit
Public ANNO As Long 

Public Sub FillFlexgrid()
    Dim M As Long
    Dim D As Long
    
    MSFlexGrid1.Redraw = False ' speed up filling the grid by delaying redraw until all cells populated
    For M = 1 To 12
        MSFlexGrid1.ColWidth(M - 1) = 1100 ' set col width if you need to
            For D = Day(DateSerial(ANNO, M, 1)) To Day(DateSerial(ANNO, M + 1, 0))
                MSFlexGrid1.Col = M - 1
                MSFlexGrid1.Row = D '- 1
                MSFlexGrid1.Text = Format(DateSerial(ANNO, M, D), "short date")
            Next
        'End If
    Next
    MSFlexGrid1.Redraw = True
End Sub

Public Sub ResetGrid()
    ' Set up our flexgrid rows and columns
    MSFlexGrid1.FixedRows = 1
    MSFlexGrid1.FixedCols = 0
    MSFlexGrid1.Rows = 32
    MSFlexGrid1.Cols = 12
    MSFlexGrid1.FormatString = "^JAN|^FEB|^MAR|^APR|^MAY|^JUN|^JUL|^AUG|^SEP|^OCT|^NOV|^DEC" ' read the documentation to understand this
    FillFlexgrid
End Sub

Private Sub Combo1_Click()
    ANNO = Combo1 ' Pick up Anno from combobox
    ResetGrid ' redo the grid with newly selected Anno
End Sub

Private Sub Form_Initialize()
    'Load combo with a few years for sake of example  - obviously in your code you populate this however you like
    Combo1.AddItem "2020"
    Combo1.AddItem "2021"
    Combo1.AddItem "2022"
    Combo1.AddItem "2023"
End Sub
Here's the second. Note the additional code in FillFlexGrid which is doing all the work that was done by the FormatString method in the ResetGrid routine of the first example

Code: Select all

Option Explicit
Public ANNO As Long  '= 2021

' Assumes we have a 12C x 31R flexgrid with no fixed row or column
Public Sub FillFlexgrid()
    Dim M As Long
    Dim D As Long

    MSFlexGrid1.Redraw = False ' speed up filling the grid by delaying redraw until all cells populated
    For M = 1 To 12
        MSFlexGrid1.Row = 0
        MSFlexGrid1.Col = M - 1
        MSFlexGrid1.Text = Format(DateSerial(ANNO, M, 1), "mmm")
        MSFlexGrid1.ColWidth(M - 1) = 1100 ' set col width if you need to
        MSFlexGrid1.ColAlignment(M - 1) = flexAlignCenterCenter
        If M > 0 Then
            For D = Day(DateSerial(ANNO, M, 1)) To Day(DateSerial(ANNO, M + 1, 0))
                MSFlexGrid1.Col = M - 1
                MSFlexGrid1.Row = D '- 1
                MSFlexGrid1.Text = Format(DateSerial(ANNO, M, D), "short date")
            Next
        End If
    Next
    MSFlexGrid1.Redraw = True
End Sub

Public Sub ResetGrid()
    ' Set up our flexgrid rows and columns
    MSFlexGrid1.FixedRows = 1
    MSFlexGrid1.FixedCols = 0
    MSFlexGrid1.Rows = 32
    MSFlexGrid1.Cols = 12
    FillFlexgrid
End Sub

Private Sub Combo1_Click()
    ANNO = Combo1 ' Pick up Anno from combobox
    ResetGrid ' redo the grid with newly selected Anno
End Sub

Private Sub Form_Initialize()
    'Load combo with a few years for sake of example  - obviously in your code you populate this however you like
    Combo1.AddItem "2020"
    Combo1.AddItem "2021"
    Combo1.AddItem "2022"
    Combo1.AddItem "2023"
End Sub
WOW.!!!

TKS for your time.