记录集中无结果

时间:2019-05-28 14:59:43

标签: sql vba ms-access

我有一个通过查询创建的临时表。我从该表中选择项目,并按特定顺序放置它们以显示在列表框中。一切正常,直到我尝试在我的select语句中添加where子句。

我想做的是,当我运行下面无法运行的查询时,只需将变量返回为“ 0”即可。

我试图做一个if语句,只是使我的变量为“ 0”,但仍然出现同样的“ No Current Record”错误。

下面的查询工作正常;

  strQuery = "SELECT * FROM " & currentTime & " Order By CVT, CVY, CVW, 
  AdjusterName"

但是当我添加<= 1的where子句时,会出现“无当前记录”错误。第二个查询是;

  strQuery = "SELECT * FROM " & currentTime & " WHERE CVT <=1 Order By 
  CVT, CVY, CVW, AdjusterName"

运行第二个查询时,出现“无当前记录”错误。这是我希望它仅显示零的地方。

只要有需要,这里就是整个功能;

    If ClaimTypeCB = "MO" Then
        strQuery = "SELECT * FROM " & currentTime & " WHERE CVT <=1 Order 
        By CVT, CVY, CVW, AdjusterName"
    Else
        strQuery = "SELECT * FROM " & currentTime & " Order By CVT, CVY, 
        CVW, AdjusterName"
    End If
        Set daoQueryResult = db.OpenRecordset(strQuery)

        daoQueryResult.MoveLast
        intRecordCount = daoQueryResult.RecordCount
        daoQueryResult.MoveFirst

    If intRecordCount = 0 Then
        intCVT = 0
    End If

(上面的If语句是我尝试插入If语句的位置,但是它没有用,并且仍然出现“无当前记录”错误。)

    If intRecordCount <> 0 Then
        With daoQueryResult
            Do While Not .EOF

                If IsNull(!CVT) Then
                    intCVT = 0
                Else
                    intCVT = !CVT
                End If

                If IsNull(!CVY) Then
                    intCVY = 0
                Else
                    intCVY = !CVY
                End If

                If IsNull(!CVW) Then
                    intCVW = 0
                Else
                    intCVW = (!CVW)
                End If

                AvailAdjList.AddItem !AdjusterName & "," & intCVT & "," & intCVY & "," & intCVW
                .MoveNext
            Loop
        End With
    End If

我真的只是希望CVT如果为0或1,则返回零。

2 个答案:

答案 0 :(得分:0)

此行之后:

Set daoQueryResult = db.OpenRecordset(strQuery)

您需要检查Recordset.EOF

With daoQueryResult
    If .EOF Then
        intRecordCount = 0
    Else
        .MoveLast
        intRecordCount = .RecordCount
        .MoveFirst
    End If
End With

尽管从您的代码来看,您似乎并不需要RecordCount

您可以简单地从Do While Not .EOF

开始

答案 1 :(得分:0)

尝试此查询:

strQuery = "SELECT CVT, CVY, CVW, AdjusterName FROM " & currentTime & " WHERE CVT <= 1 Order By CVT, CVY, CVW, AdjusterName" _
         & " UNION ALL " _
         & "SELECT DISTINCT 0, NULL, NULL, NULL FROM " & currentTime & " WHERE NOT EXISTS " _
         & "(SELECT 1 FROM " & currentTime & " WHERE CVT <= 1)"

它使用UNION ALL,因此,如果第一个查询不返回任何行,则第二个查询将返回具有以下值的行:0, NULL, NULL, NULL
我希望我没有错字。