I getting a msgbox "BOF or EOF is true, or the current record has been deleted, request operation requires a current record"
Can anyone explain this?
Here is my code:
Private Sub cmdNewtrans_Click()
On Error GoTo CmdRepErr
Dim xlAdd As Integer
Dim xlApp As Excel.Application
Dim xlDoc As Excel.Worksheet
Dim FileStr As String
Dim Rst2 As ADODB.Recordset
Dim DocPname As String
Dim DocCname As String
Dim DocNname As String
Me.Requery
Me.Refresh
Set Cnx = New ADODB.Connection
Set Rst = New ADODB.Recordset
Set Rst2 = New ADODB.Recordset
Set fso = CreateObject("Scripting.FileSystemObject") 'creates an activeX object
Cnx.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Access.CurrentDb.Name & ";Persist Security Info=False"
Cnx.Open
If Rst.State = 1 Then Rst.Close
'opens first Record set from tbltransmittals table
Rst.Open "select * from tblTransmittals where [TID] = " & [TIDNo], Cnx, adOpenKeyset, adLockOptimistic
'Opens next set of records from tbltransdwgs
Rst2.Open "select * from tblTransdwgs where [TID] = " & [TIDNo], Cnx, adOpenKeyset, adLockOptimistic
Set xlApp = New Excel.Application 'set the application object to a new excel session
xlApp.Workbooks.Open fso.getparentfoldername(Access.CurrentDb.Name) & "\TRANS_template.xls" 'open the template xls file
xlApp.Visible = True 'bring it up on screen
Set xlDoc = xlApp.ActiveSheet 'set the document object to the template xls we just opened
xlDoc.PageSetup.CenterHorizontally = True
xlDoc.PageSetup.CenterVertically = True
xlDoc.Range("N11").Value = Rst.Fields("TID")
xlDoc.Range("AL8").Value = Rst.Fields("ProjNo")
xlDoc.Range("AL6").Value = Rst.Fields("ProjTitle")
'xlDoc.Range("AL7").Value = Rst.Fields("ProjTitle2")
xlDoc.Range("AL9").Value = Rst.Fields("ProjEng")
xlDoc.Range("N12").Value = Rst.Fields("TransBy")
xlDoc.Range("N13").Value = Rst.Fields("Date")
'xlDoc.Range("N14").Value = Rst.Fields("Tphone")
xlDoc.Range("AL11").Value = Rst.Fields("Courier")
xlDoc.Range("AL12").Value = Rst.Fields("Copyto")
xlDoc.Range("AL13").Value = Rst.Fields("Company")
xlDoc.Range("AL14").Value = Rst.Fields("CPhone")
xlDoc.Range("AS57").Value = Rst.Fields("signed")
xlDoc.Range("B52").Value = Rst.Fields("Comments")
DocPname = cboProjNo.Value
DocCname = cboCompany.Value
DocNname = txtTID
xlAdd = 16 'sets xladd varible - row 16
'reset this to the first recordset.
Do Until Rst.EOF 'for each record we just opened with the recordset
If Rst.AbsolutePosition > 20 Then Exit Do
If Rst.AbsolutePosition = 1 Then
Select Case Rst.Fields("Status")
Case "Issued for Approval": xlDoc.Range("C41").Value = "X"
Case "Issued for Construction": xlDoc.Range("C43").Value = "X"
Case "As Requested": xlDoc.Range("C45").Value = "X"
Case "For Review & Comment": xlDoc.Range("C47").Value = "X"
Case "Issued for Bid": xlDoc.Range("C49").Value = "X"
Case "Issed for Estimates Only": xlDoc.Range("X41").Value = "X"
Case "Approved as Noted": xlDoc.Range("X43").Value = "X"
Case "Returned for Corrections": xlDoc.Range("X45").Value = "X"
Case "For Your Use": xlDoc.Range("X47").Value = "X"
Case "Prints Returned from Us": xlDoc.Range("X49").Value = "X"
Case "Resubmit for Approval": xlDoc.Range("AW41").Value = "X"
Case "Return for Distribution": xlDoc.Range("AW43").Value = "X"
Case "Return Corrected Prints": xlDoc.Range("AW45").Value = "X"
Case Else: xlDoc.Range("AW47").Value = "X"
End Select
End If
'This is a part of the 2nd record set Cstr turns into string
xlDoc.Range("A" & CStr(xlAdd + Rst2.AbsolutePosition)).Value = Rst2.Fields("Copies") 'basically, the range property of the document object is used
xlDoc.Range("F" & CStr(xlAdd + Rst2.AbsolutePosition)).Value = Rst2.Fields("DwgNo") 'to get or set the values of each cell. Notice how the range
xlDoc.Range("U" & CStr(xlAdd + Rst2.AbsolutePosition)).Value = Trim(Rst2.Fields("Rev")) 'object calls on a certain cell and sets the value
xlDoc.Range("W" & CStr(xlAdd + Rst2.AbsolutePosition)).Value = Trim(Rst2.Fields("FileName")) 'to the corresponding field in the recordset.
xlDoc.Range("AI" & CStr(xlAdd + Rst2.AbsolutePosition)).Value = Rst2.Fields("Media") 'im adding the absolute position of the recordset to 16 because
xlDoc.Range("AN" & CStr(xlAdd + Rst2.AbsolutePosition)).Value = Rst2.Fields("DrawingTitle") 'the first record in the xls template starts at row 17...
' If Rst2.Fields("New") = -1 Then xlDoc.Range("BT" & CStr(xlAdd + Rst2.AbsolutePosition)).Value = "X" 'and the first absoluteposition in the recordset is 1.
' If Rst2.Fields("Revised") = -1 Then xlDoc.Range("BU" & CStr(xlAdd + Rst2.AbsolutePosition)).Value = "X" '1 + 16 = 17 and then the absoluteposition in the recordset will go
' If Rst2.Fields("Unrevised") = -1 Then xlDoc.Range("BV" & CStr(xlAdd + Rst2.AbsolutePosition)).Value = "X" 'up as you execute rst.movenext. So this section of code
' 'fills the ENTIRE xl spreadsheet no matter how many records are being reported.
Rst2.MoveNext 'move to the next record AND increment the absoluteposition value by 1
Loop
Rst.Close 'close recordset object
Rst2.Close
Cnx.Close 'close connection object
'FileStr = fso.getparentfoldername(Access.CurrentDb.Name) & "DocPname" & "_" & "DocCname" & Format(Date, "mmddyy") & "_" & "DocNname"
'xlDoc.SaveAs FileStr
Exit Sub
CmdRepErr:
MsgBox Err.Description
Resume
End Sub
I don't quite understand recordcount and positions yet.
Thanks very much for any help!
John Paul