Ms access VBA中的循环跳过值

Ms access VBA中的循环跳过值,ms-access,vba,Ms Access,Vba,我在Access VBA中有以下代码 Public Sub CalculateVol() Dim vol As Double Dim rs As Recordset Dim rs2 As Recordset Dim iRow As Long, iField As Long Dim strSQL As String Dim CurveID As Long Dim MarkRunID As Long Dim MaxOfMarkAsofDate As Date Dim userdate As Str

我在Access VBA中有以下代码

Public Sub CalculateVol()

Dim vol As Double
Dim rs As Recordset
Dim rs2 As Recordset
Dim iRow As Long, iField As Long
Dim strSQL As String
Dim CurveID As Long
Dim MarkRunID As Long
Dim MaxOfMarkAsofDate As Date
Dim userdate As String

DoCmd.RunSQL "DELETE * FROM HolderTable"
'Clears out the old array from the holder table.

Dim I As Integer
Dim x As Date

userdate = InputBox("Please Enter the Date (mm/dd/yyyy)")

x = userdate

Dim BucketTermAmt As Long
BucketTermAmt = InputBox("Please Enter the Term Amount")

For I = 0 To 76

MaxOfMarkAsofDate = x - I



strSQL = "SELECT * FROM VolatilityOutput WHERE CurveID=" & Forms!Volatility.cboCurve.Value & " AND MaxOfMarkAsofDate=#" & MaxOfMarkAsofDate & "# ORDER BY MaxOfMarkasOfDate, MaturityDate"

Set rs = CurrentDb.OpenRecordset(strSQL, Type:=dbOpenDynaset, Options:=dbSeeChanges)
Set rs2 = CurrentDb.OpenRecordset("HolderTable")



If rs.RecordCount <> 0 Then

    rs.MoveFirst

    rs.MoveLast


    Dim BucketTermUnit As String
    Dim BucketDate As Date
    Dim MarkAsOfDate As Date
    Dim InterpRate As Double


    Dim b As String

    b = BucketTermAmt

    BucketTermUnit = Forms!Volatility.cboDate.Value
    BucketDate = DateAdd(BucketTermUnit, b, MaxOfMarkAsofDate)
    InterpRate = CurveInterpolateRecordset(rs, BucketDate)

    rs2.AddNew
    rs2("BucketDate") = BucketDate
    rs2("InterpRate") = InterpRate
    rs2.Update


End If

Next I


vol = EWMA(0.94)

Forms!Volatility!txtVol = vol

Debug.Print vol


End Sub
其基本思想是用户输入MaxofMarkAsofDate的日期。然后,代码在表volatityoutput中找到MarkAsofDate的实例,并将其用作计算InterpRate的参考点。它将此号码存储在HolderTable中。然后它循环相同的过程,除了使用用户输入的MarkAsofDate的前一天,然后使用该日期的前一天,以此类推总共76次


第一部分工作正常,但循环给我带来了麻烦。如果在表中找不到用户输入的日期,它将跳过它,但仍将其作为循环计数。因此,虽然我想要76个数据点,但如果跳过20个日期,我可能只得到56个数据点。所以我想阻止它跳过,或者继续循环,直到HolderTable中总共有76个数字。如何执行此操作?

听起来您需要一个while循环,因为编写的for循环的次数总是相同的。看起来您可能需要第二个计数器来增加日期

while count < 76
    'get rs here
    if rs.RecordCount <> 0 Then
        'do everything else
        count = count + 1
    end if
    dateCounter = dateCounter + 1
loop

听起来你想要一个while循环,因为for循环的次数总是一样的。看起来您可能需要第二个计数器来增加日期

while count < 76
    'get rs here
    if rs.RecordCount <> 0 Then
        'do everything else
        count = count + 1
    end if
    dateCounter = dateCounter + 1
loop

谢谢,这正是我想要的。非常感谢。这正是我想要的。非常感谢。