我有一个MS Access表,其中包含已执行的作业列表(作业ID,StartDate,EndDate)我需要查找指定日期范围内的天数(例如1月1日至6月30日之间)用户选择使用至少打开1个作业的文本框,即StartDate
和EndDate
之间。我有一些VBA和SQL的经验(分组不是很好)。
有人可以帮我提一下如何计算这个数据吗?
JobID| StartDate| EndDate
1142| 03-Jan-14| 04-Feb-14|
1143| 13-Mar-14| 18-May-14|
1144| 03-Jan-14| 29-Jan-14|
1145| 20-Jan-14| 13-Apr-14|
1146| 03-Jan-14| 07-Jan-14|
答案 0 :(得分:0)
计数使用的是约会功能。
datediff(dd,startdate, enddate)
" DD"告诉它查找天数,开始日期将是1/03/2014,结束日期将是2014年4月2日作为第一行的示例
答案 1 :(得分:0)
您可以按照评论中的建议创建日历表,并在表单中添加一个名为cmdCountJobDays的按钮。 以下代码将根据作业日期检查所选日期范围内的所有可能日期。有点笨重,但它会让你到那里: - )
Private Sub cmdCountJobDays_Click()
Dim StartDate as Date
Dim EndDate as Date
StartDate = me.txtYourStartDateTextBox
EndDate = me.txtYourEndDateTextBox
SQLGetJobCountPeriod = SQLGetJobCountPeriod & "Select CalendarDate from tblCalendarDates where CalendarDate >=" & cdbl(StartDate)
SQLGetJobCountPeriod = SQLGetJobCountPeriod & " and CalendarDate <= " & cdbl(EndDate)
Set dateschecked = CurrentDb.OpenRecordset(SQLGetJobCountPeriod)
If dateschecked.EOF = False Then
dateschecked.MoveFirst
CountOpenJobDays = 0
CountAllDays = 0
Do While dateschecked.EOF = False
CurrentCheckDate = CDbl(dateschecked.Fields("CalendarDate"))
SQLJobDates = "Select StartDate, EndDateDate from jobdetails "
Set Jobdates = CurrentDb.OpenRecordset(SQLJobDates)
If Jobdates.EOF = False Then
Jobdates.MoveFirst
Do While Jobdates.EOF = False
Jobstartdate = CDbl(Jobdates.Fields("StartDate"))
Jobenddate = CDbl(Jobdates.Fields("EndDate"))
If (CurrentCheckDate > Jobstartdate - 1) And (CurrentCheckDate < Jobenddate + 1) Then
CountJobOpen = CountJobOpen + 1
Exit Do
End If
Jobdates.MoveNext
Loop
End If
CountAllDays = CountAllDays + 1
dateschecked.MoveNext
Loop
End If
msgbox CountJobOpen
End Sub