When i using MS Access ADO recordset to retrieve the Field Name that prompt the error message (Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record. 3021. Who can help me to solve this problem
'****************************Code Start************************************
Sub Export_Excel_Report(ByVal cnndb As ADODB.Connection, _
ByVal rs As ADODB.Recordset)
Dim objXls As New Excel.Application
Dim rst As New ADODB.Recordset
Dim str As String
Dim strSQL As String
Dim strSQLTemp As String
Dim i As Integer, z As Integer
Dim intRcd As Long
str = rs.Fields(0).Value
strSQLTemp = "SELECT [F1], [F2], [F3], [F4], [F5], "
strSQLTemp = strSQLTemp & "[F6], [F7], [F8], [F9] FROM [tbl] "
With objXls
.Workbooks.Add (1)
.Visible = True
.Sheets.Add ' After:=Worksheets(1)
.Worksheets(1).Name = "Accepted"
.Worksheets(2).Name = "Rejected"
End With
For z = 1 To 2
strSQL = ""
If z = 1 Then
strSQL = strSQLTemp & "WHERE [F1] = '" & str & "' And [F9] Not Like 'Rejected*' ORDER BY 1, 3;"
Else
strSQL = strSQLTemp & "WHERE [F1] = '" & str & "' And [F9] Like 'Rejected*';"
End If
rst.Open strSQL, cnndb, adOpenStatic, adLockReadOnly
With rst
.MoveFirst
If .RecordCount > 0 Then
objXls.Worksheets(z).Activate
For i = 0 To .Fields.Count - 1
objXls.Range("A1").Offset(0, i) = .Fields(i).Name
'MsgBox .AbsolutePosition <-- break point returned ( -3 ) WHY
.MoveNext
Next i
'objXls.Range("A2").CopyFromRecordset rst
End If
.Close
End With
Next z
Set rst = Nothing
Set objXls = Nothing
End Sub
'****************************Code End************************************
Thank you very much, if you can help me
Perry Choy

MS Access VBA: How to solve the problem, when i using the ADO [Error 3021]?
JMBC
Hey Perry,
There are two things that come to mind.
1. are you sure your query is correct, could be that it's not returning any results.
2. your cursor type of static... this is only valid for client side cursors, set the recordsets cursor location property to adUseClient before you open the recordset and see if that helps.
Another thing you should have posted to the VBA forum, someone might move your post there.
dn8