我得到了运行时错误3021:
EOF的BOF为True,或当前记录已被删除
当我调试这个子时建议我的记录集是空的。
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cs As String
Dim sql As String
cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\database.accdb;Persist Security Info=False;"
cn.Open cs
sql = "SELECT DISTINCT dbo_vwScaEuLevel.PropertyNameParcelTiebacks, dbo_vwScaEuLevel.PsdPrimaryAddress, " & _
"dbo_vwScaEuLevel.VsTotalEconomicUnit, dbo_vwScaEuLevel.AssessYear, dbo_vwScaEuLevel.PrimaryPin, " & _
"dbo_vwScaEuLevel.BsdAvgYearBuilt, dbo_vwScaEuLevel.PsdNeighborhood, dbo_vwScaEuLevel.ModelName, " & _
"FROM ((dbo_vwScaEuLevel INNER JOIN dbo_ScaEuLevelOccupancy ON (dbo_vwScaEuLevel.PrimaryPin = dbo_ScaEuLevelOccupancy.PrimaryPin) " & _
"AND (dbo_vwScaEuLevel.AssessYear = dbo_ScaEuLevelOccupancy.AssessYear)) INNER JOIN dbo_cama_ComBldgSec " & _
"ON dbo_vwScaEuLevel.PropertyID = dbo_cama_ComBldgSec.PropertyID) INNER JOIN dbo_vwPropertyOwner ON " & _
"dbo_vwScaEuLevel.PropertyID = dbo_vwPropertyOwner.PropertyID " & _
"WHERE (((dbo_vwScaEuLevel.AssessYear)= " & Range("C1").Value & ") AND ((dbo_vwScaEuLevel.BsdAvgYearBuilt) Is Not Null) " & _
"AND ((dbo_vwScaEuLevel.PsdNeighborhood) Between '800' And '900') AND ((dbo_vwScaEuLevel.TotalResidentialSquareFootage)=0) " & _
"AND ((dbo_vwScaEuLevel.ModelName) Like '*' & '" & Range("C2").Value & "' & '*'))"
根据另一个thread的建议,我包含了以下内容,并且"记录集为空"消息。
rs.Open sql, cn
Debug.Print sql
If rs.BOF And rs.EOF Then
MsgBox "recordset is empty"
Else
rs.MoveLast
MsgBox "Recordset contains " & rs.RecordCount & " rows"
End If
rs.close
cn.close
我将SQL从即时窗口复制到访问,它给了我我所需要的。有什么想法为什么我的记录集可能会空出来?