自访问中的任意开始日期创建动态营业日期列表

时间:2015-09-04 17:36:11

标签: date access-vba ms-access-2010

使用MS Access 2010,我需要生成一个完整的日期列表,从任意开始日期开始,比如#1/1/2015#,到现在,Date()。我希望这个列表能够存在于自己的表中,尽管查询也可以。我也希望只抓住工作日/工作日。有人可以帮忙吗?

我按日期跟踪业务流程错误。为每个错误创建一个新的错误记录,每个记录都标有日期。但是,每个日期都没有错误。因此,随着时间的推移报告不能提供正确的可视化,因为没有错误的日期没有表示。

感谢帮助在Access中生成此列表,以及表示此信息的任何其他想法。

干杯, 伯吉斯

更新 - 我已经能够列出自开始日期以来的日期列表。这是我的代码:

Sub createDatesTable()

    'Declare variables'
    Dim startDate As Date
    Dim endDate As Date
    Dim countDate As Date
    Dim length As Long
    Dim i As Long
    Dim dates() As Date

    'Initialize'
    startDate = #6/23/2015#
    endDate = #9/1/2015#
    countDate = startDate
    length = endDate - startDate

    'Define date array length'
    ReDim dates(1 To length)

    'Generate date list in array'
    For i = 1 To (length)
        dates(i) = countDate
        countDate = countDate + 1
    Next i

    'Print array to Immediate Window'
    For i = 1 To length
        Debug.Print dates(i)
    Next i


    Dim db As DAO.Database
    Dim rs As DAO.Recordset


    Set db = CurrentDb
    Set rs = db.OpenRecordset("tblDateList", dbOpenDynaset)

    'Add array data to existing table'
    For i = 1 To length
        rs.AddNew
        rs!DateList = dates(i)
        rs.Update
    Next i

    rs.Close
    db.Close

End Sub

现在,我对VBA很新,所以这可能包含基本错误。感谢您的反馈。

-Burgess

1 个答案:

答案 0 :(得分:0)

你的桌子可以有这样的结构:

Column name    | Data type
---------------+----------
dt             | DateTime
isBusinessDay  | Yes/No

至于填充,这样的事情:

public sub fillDates(startDate as date, endDate as date)
    dim db as DAO.database, rs as DAO.Recordset
    dim strSQL as string
    dim d as date

    strSQL = "delete from tbl_dates"
    docmd.runsql strSQL

    set db = currentdb()
    set rs = db.openRecordset("tbl_dates", dbOpenDynaset, dbAppendOnly)

    for d = startDate to endDate
        with rs
            .addNew
            ![dt] = d
            ![isBusinessDay] = (format(d, "w", vbMonday) <= 5)
            .update
        end with
    next d
    rs.close
    db.close
end sub