filling calendar in listview

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

filling calendar in listview

Post by sal21 »

Code: Select all

Option Explicit
Private Sub Form_Load()

    Dim I As Integer

    With Me.CANNO
        .Clear
        For I = Year(Now) To Year(Now) + 2
            .AddItem I
        Next I
    End With

    With Me.CMESE
        .Clear
        .AddItem "01-GENNAIO"
        .AddItem "02-FEBBRAIO"
        .AddItem "03-MARZO"
        .AddItem "04-APRILE"
        .AddItem "05-MAGGIO"
        .AddItem "06-GIUGNO"
        .AddItem "07-LUGLIO"
        .AddItem "08-AGOSTO"
        .AddItem "09-SETTEMBRE"
        .AddItem "10-OTTOBRE"
        .AddItem "11-NOVEMBRE"
        .AddItem "12-DICEMBRE"
    End With

End Sub

possible to have the same calendar in listview, based the immagine1

for example:

month 05-MAGGIO in combobox MESE
year 2020 in combobox ANNO
You do not have the required permissions to view the files attached to this post.

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

Re: filling calendar in listview

Post by HansV »

I'll get back to you later today, no time now.
Best wishes,
Hans

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

Re: filling calendar in listview

Post by sal21 »

HansV wrote:
29 May 2020, 07:56
I'll get back to you later today, no time now.
no prob! :cheers:

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

Re: filling calendar in listview

Post by HansV »

Like this:

Code: Select all

Private Sub CANNO_Change()
    Call FillCalendar
End Sub

Private Sub CMESE_Change()
    Call FillCalendar
End Sub
Private Sub FillCalendar()
    Dim a As Long
    Dim m As Long
    Dim d0 As Date
    Dim d1 As Date
    Dim d2 As Date
    Dim d As String
    Dim itm As ListItem
    Dim i As Long
    a = Val(Me.CANNO)
    m = Val(Me.CMESE)
    ' Check for empty year or month
    If a = 0 Then
        Me.CANNO.SetFocus
        MsgBox "Please select a year"
        Exit Sub
    End If
    If m = 0 Then
        Me.CMESE.SetFocus
        MsgBox "Please select a month"
        Exit Sub
    End If
    Me.ListView1.ListItems.Clear
    ' First day of the month
    d1 = DateSerial(a, m, 1)
    ' Last day of the month
    d2 = DateSerial(a, m + 1, 0)
    ' Monday on or before the first day
    d0 = d1 + 1 - Weekday(d1, vbMonday)
    Do
        If d0 < d1 Or d0 > d2 Then
            d = ""
        Else
            d = Day(d0)
        End If
        Set itm = Me.ListView1.ListItems.Add(Text:=d)
        For i = 1 To 6
            d0 = d0 + 1
            If d0 < d1 Or d0 > d2 Then
                d = ""
            Else
                d = Day(d0)
            End If
            itm.ListSubItems.Add Index:=i, Text:=d
        Next i
        d0 = d0 + 1
    Loop Until d0 > d2
End Sub
Best wishes,
Hans

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

Re: filling calendar in listview

Post by sal21 »

HansV wrote:
29 May 2020, 09:56
Like this:

Code: Select all

Private Sub CANNO_Change()
    Call FillCalendar
End Sub

Private Sub CMESE_Change()
    Call FillCalendar
End Sub
Private Sub FillCalendar()
    Dim a As Long
    Dim m As Long
    Dim d0 As Date
    Dim d1 As Date
    Dim d2 As Date
    Dim d As String
    Dim itm As ListItem
    Dim i As Long
    a = Val(Me.CANNO)
    m = Val(Me.CMESE)
    ' Check for empty year or month
    If a = 0 Then
        Me.CANNO.SetFocus
        MsgBox "Please select a year"
        Exit Sub
    End If
    If m = 0 Then
        Me.CMESE.SetFocus
        MsgBox "Please select a month"
        Exit Sub
    End If
    Me.ListView1.ListItems.Clear
    ' First day of the month
    d1 = DateSerial(a, m, 1)
    ' Last day of the month
    d2 = DateSerial(a, m + 1, 0)
    ' Monday on or before the first day
    d0 = d1 + 1 - Weekday(d1, vbMonday)
    Do
        If d0 < d1 Or d0 > d2 Then
            d = ""
        Else
            d = Day(d0)
        End If
        Set itm = Me.ListView1.ListItems.Add(Text:=d)
        For i = 1 To 6
            d0 = d0 + 1
            If d0 < d1 Or d0 > d2 Then
                d = ""
            Else
                d = Day(d0)
            End If
            itm.ListSubItems.Add Index:=i, Text:=d
        Next i
        d0 = d0 + 1
    Loop Until d0 > d2
End Sub
:clapping: :clapping: :clapping: :clapping: :clapping: :cheers:

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

Re: filling calendar in listview

Post by sal21 »

But how to get the value 15, when i click on listview?
similar a Naval Battle X,Y coordinate :scratch: :grin:

I know only to get the row with:

Code: Select all

Private Sub CALENDAR_Click()

    Dim L As Long
    L = Empty
    L = Me.CALENDAR.SelectedItem.Index

End Sub
but how to get the related column index?
You do not have the required permissions to view the files attached to this post.

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

Re: filling calendar in listview

Post by HansV »

Why don't you use the MonthView control? Wouldn't that be a lot easier?
Best wishes,
Hans

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

Re: filling calendar in listview

Post by sal21 »

HansV wrote:
29 May 2020, 11:32
Why don't you use the MonthView control? Wouldn't that be a lot easier?
MonthView???

Never used...

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

Re: filling calendar in listview

Post by HansV »

I'd try it...
Best wishes,
Hans