可能重复:
SQL Query & VBA error
我一直收到这个错误:
BOF或EOF设置为True,或者当前记录已被删除。请求的操作需要当前记录。
MyQuery = "Select Destinataire, SUM(" & Entete & ") AS NombreTotal FROM [Feuil1$] " _
& "WHERE [DateMad] Between #" _
& Format(date_deb, "yyyy/mm/dd") _
& "# And #" & Format(date_fin, "yyyy/mm/dd") & "#" & Query3 & " Group By Destinataire"
objRecordSet.Open MyQuery, objConnection, adOpenStatic, adLockOptimistic
Feuil3.Cells(1, Col) = Entete
Feuil3.Cells(strLine, 2) = Entete2
Feuil3.Cells(2, Col).Value = objRecordSet.Fields("NombreTotal").Value
我在最后一行得到错误
Feuil3.Cells(2, Col).Value = objRecordSet.Fields("NombreTotal").Value
为什么它问我BOF或EOF因为我问SUM?还有如何让这个查询工作PLZ? 我无法用这个调试来呼吸
debug.print MyQuery提供以下内容:
Select Destinataire, SUM(NbCompteurElecNR) AS NombreTotal FROM [Feuil1$] WHERE [DateMad] Between #2012/11/21# And #2012/11/26# And [Destinataire] = 'REL12' Group By Destinataire
答案 0 :(得分:0)
无法评论您的数据,但您收到了EOF错误,因为您的查询(MyQuery)不会导致任何数据行。在使用查询打开记录集之后,必须首先检查它是否包含任何行,然后才能继续(即它不在文件'EOF'的末尾)。例如,见下文。
MyQuery = "....."
Set objRecordSet = New ADODB.Recordset
objRecordSet.Open MyQuery, objConnection, adOpenStatic, adLockOptimistic
If Not objRecordSet.EOF Then
'The record set contain one or more rows, do stuff
Else
'The record set does not contain any rows, take action and notify user
End If
objRecordSet.Close
Set objRecordSet = Nothing
如果您正在进行更新/插入操作,则应在发生数据库错误或其他失败的情况下正确回滚数据库,否则可能会使数据库处于不愉快状态。例如,见下文。
On Error Goto Rollback
objConnection.BeginTrans
MyInsertQuery = "..."
Set objRecordSet = New ADODB.Recordset
objRecordSet.Open MyInsertQuery , objConnection, adOpenStatic, adLockOptimistic
objRecordSet(1) = someValue
objRecordSet(2) = someOtherValue
objRecordSet.Update
objRecordSet.Close
Set objRecordSet = Nothing
objConnection.CommitTrans
Exit Sub
Rollback:
objConnection.RollbackTrans
objRecordSet.Close
Set objRecordSet = Nothing