使用开始日期和结束日期列出月份和每月的天数

时间:2016-11-03 22:51:53

标签: vba ms-access

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

MyEnum val = random_value<MyEnum::val1, MyEnum::val2, MyEnum::val3>(rnd);

我对VBA稍微有点新意,我得到的是dateiff()计算Month Days September - 30 October - 31 November - 30 ,它给了我一个月之间的总天数,但没有分解

2 个答案:

答案 0 :(得分:2)

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

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

enter image description here

答案 1 :(得分:0)

你可以使用一个回调函数 - 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

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

mmmm - dd

你完成了:

enter image description here

相关问题