【问题标题】:Listing Months and number of days in each month using start and end date使用开始日期和结束日期列出每月的月份和天数
【发布时间】:2023-03-10 19:24:01
【问题描述】:

您好,我在 MS Access VBA 中遇到了重大挑战,我有一个开始日期和结束日期,并想列出月份,例如:start_date ="2016-09" and_date="2016-11" 我希望结果是:

Month     Days
September - 30
October - 31 
November - 30

我对 VBA 有点陌生,我得到的只是进行 datediff() 计算 Me.Text365.Value = DateDiff("d", Me.start_date, Me.end_date)这给了我一个月之间的总天数,但没有分解

【问题讨论】:

  • 日历表是轻量级的,非常有用。它会解决你的问题。它可以像您喜欢的一样简单,但您可以在此处了解有用的字段/列mssqltips.com/sqlservertip/4054/…

标签: vba ms-access


【解决方案1】:

使用DateSerial 获取当月的最后一天:日期的年份、日期的月份 + 1 和 0 作为日期值:

DateSerial(Year(mydate),Month(mydate) + 1,0)

【讨论】:

    【解决方案2】:

    您可以为此使用回调函数 - Access 的隐藏瑰宝之一:

    ' Callback function to list ultimo dates of each month for a count of years.
    '
    ' Typical settings for combobox or listbox:
    '   ControlSource:  Bound or unbound
    '   RowSource:      Leave empty
    '   RowSourceType:  ListUltimoMonths
    '   BoundColumn:    1
    '   LimitToList:    Yes
    '   Format:         Valid format for date values
    '   ColumnCount:    1
    '
    ' 2014-09-24. Cactus Data ApS, CPH.
    '
    Public Function ListUltimoMonths( _
        ctl As Control, _
        Id As Long, _
        Row As Long, _
        Column As Long, _
        Code As Integer) _
        As Variant
    
        ' Count of months in a year.
        Const MonthCount    As Integer = 12
        ' Count of years to list.
        Const Years         As Integer = 3
    
        Static Start        As Date
        Static Format       As String
        Static Rows         As Integer
    
        Dim Value           As Variant
    
        Select Case Code
            Case acLBInitialize
                Start = Date            ' Date of first month to list.
                Rows = MonthCount * Years
                Format = ctl.Format
                Value = True            ' True to initialize.
            Case acLBOpen
                Value = Timer           ' Autogenerated unique ID.
            Case acLBGetRowCount        ' Get rows.
                Value = Rows            ' Set number of rows.
            Case acLBGetColumnCount     ' Get columns.
                Value = 1               ' Set number of columns.
            Case acLBGetColumnWidth     ' Get column width.
                Value = -1              ' Use default width.
            Case acLBGetValue           ' Get the data for each row.
                Value = DateSerial(Year(Start), Month(Start) + Row + 1, 0)
            Case acLBGetFormat          ' Format the data.
                Value = Format          ' Use format of control.
            Case acLBEnd
                ' Do something when form with listbox closes or
                ' listbox is requeried.
        End Select
    
        ' Return Value.
        ListUltimoMonths = Value
    
    End Function
    

    按照在线说明进行操作。然后将控件的Format设置为:

    mmmm - dd
    

    你就完成了:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 2023-03-17
      相关资源
      最近更新 更多