如何在VBScript中将循环结果存储到数组中?

时间:2017-12-19 08:29:29

标签: arrays vbscript asp-classic

我有一个循环,我想把结果放到数组中。 这是我的循环。

    For i = 1 To bill
        a = rs("CT08_Tarikh") 'from db
        cutiumum = Array(a)   'and this is how I declare array
        rs.MoveNext
    Next
    rs.Close
    Set rs = Nothing
End If

之后,我会将变量传递给另一个函数:

tarikh = NetWorkdays(dateFrom, dateTo, cutiumum)

Public Function NetWorkdays(dtStartDate, dtEndDate, arrHolidays)

但是,当我尝试在函数arrHolidays内为NetWorkdays做一些循环时,它只返回1个数据(并非全部来自cutiumum)。

你认为我的错误是什么?

更新

我已经在使用

dim arrRecordset

arrRecordset = rs.GetRows()

但是我在函数

中出错了
Public Function NetWorkdays(dtStartDate, dtEndDate, arrHolidays)
    Dim lngDays
    Dim lngSaturdays
    Dim lngSundays
    Dim lngHolidays
    Dim lngAdjustment
    Dim dtTest
    Dim i, x

    lngDays = DateDiff("d", dtStartDate, dtEndDate)
    lngSundays = DateDiff("ww", dtStartDate, dtEndDate, vbSunday)
    lngSaturdays = DateDiff("w", IIf(Weekday(dtStartDate, vbSunday) = vbSaturday, dtStartDate, dtStartDate - Weekday(dtStartDate, vbSunday)), dtEndDate)

    For x = LBound(arrHolidays) To UBound(arrHolidays)
        For i = 0 To lngDays
            dtTest = DateAdd("d", i, dtStartDate)

'error in line here: Subscript out of range: 'arrHolidays'
            If arrHolidays(x) = dtTest And Weekday(dtTest) <> 1 And Weekday(dtTest) <> 7 Then
                lngHolidays = lngHolidays + 1
            End If
        Next
    Next

    If Weekday(dtStartDate, vbSunday) = vbSunday Or Weekday(dtStartDate, vbSunday) = vbSaturday Then
        lngAdjustment = 0
    Else
        lngAdjustment = 1
    End If

    NetWorkdays = lngDays - lngSundays - lngSaturdays - lngHolidays + lngAdjustment
End Function

1 个答案:

答案 0 :(得分:2)

试试这个:

dim arrRecordset
arrRecordset = rs.GetRows()

getRows()方法会将记录集一次转换为二维数组:

https://www.w3schools.com/asp/met_rs_getrows.asp