存在记录时,访问VBA recordCount返回0

时间:2017-08-04 14:38:50

标签: ms-access access-vba

我正在尝试创建一个函数来将n个工作日添加到日期。这显然需要考虑到周末和假期。周末部分工作正常,但我使用一个表列出所有假期,我无法得到我的查询来拉出准确的recordCount。它坚持计数始终为零。这是我的代码:

Public Function dateAddNoWeekends(dt1 As Date, genDate As Integer)
Dim i As Integer
Dim date1 As Date
Dim date2 As Date
Dim holidays As Boolean
Dim isWeekday As Boolean
Dim dayOfWeek As Integer
Dim numDays As Integer
Dim db As Database
Dim rs As DAO.Recordset
Dim sql As String
Set db = CurrentDb
date1 = dt1
date2 = date1
i = 0
numDays = genDate

sql = "SELECT * FROM Holidays WHERE holidayDate = " & date2
Set rs = db.OpenRecordset(sql)

Debug.Print date2

Do While i < numDays
    If Not isWeekday Then date2 = date2 + 1
    dayOfWeek = Weekday(date2)

    If rs.RecordCount > 0 Then
        Debug.Print rs.RecordCount
        holidays = True
    Else
        holidays = False
        Debug.Print rs.RecordCount
    End If

    If dayOfWeek > 1 And dayOfWeek < 7 And holidays = False Then
        If i < numDays - 1 Then date2 = (date2 + 1)
            isWeekday = True
            i = i + 1
        Else: isWeekday = False
        End If
Loop

rs.Close
Debug.Print date2

1 个答案:

答案 0 :(得分:0)

为了解决这个问题,我在循环的顶部声明了一个变量(这样它每次都会正确更新)并分配一个Dcount函数来计算从date2到date2的数量。