troubleshooting Question

Item not found in this collection

Avatar of SteveL13
SteveL13Flag for United States of America asked on
Microsoft Access
5 Comments1 Solution181 ViewsLast Modified:
There must be something missing in the following code.  I get an error: "Item not found in this collection"

Any thoughts?

Public Sub AppendHeader()

    Dim db As DAO.Database
    Dim lngRecordNumber As Long
    Dim rstDataHeader As DAO.Recordset
    Dim rstPropertyInterests As DAO.Recordset
    Dim rstSourceTable As DAO.Recordset
    Dim strNetRevenueInterest As String
    Dim strWorkingInterest As String
    Dim strInvoiceNumber As String
    Dim strSQL As String
    Dim tdf As DAO.TableDef
    Dim tblName As String

    strSQL = "SELECT DISTINCT tblTEMPDataImportStepOne.PROPNUM, tblProperty.WELL_NAME, tblProperty.OPERATOR "
    strSQL = strSQL & "FROM tblTEMPDataImportStepOne INNER JOIN tblProperty ON tblTEMPDataImportStepOne.PROPNUM = tblProperty.PROPNUM"

    Set db = CurrentDb
    
    tblName = "tblTEMPDataImportStepOne"
    Set tdf = db.TableDefs(tblName)
    tdf.Fields.Append tdf.CreateField("RECORD_NUMBER", dbLong)

    Set rstSourceTable = db.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)
    Set rstDataHeader = db.OpenRecordset("tblDataHeader", dbOpenDynaset, dbSeeChanges)
    
    rstSourceTable.MoveFirst
    Do While Not rstSourceTable.EOF
        strSQL = "SELECT TOP 1 tblPropertyInterests.WORKING_INTEREST, tblPropertyInterests.NET_REVENUE_INTEREST "
        strSQL = strSQL & "FROM tblPropertyInterests "
        strSQL = strSQL & "WHERE (((tblPropertyInterests.PROPNUM)='" & rstSourceTable.Fields("PROPNUM") & "')) ORDER BY tblPropertyInterests.DATE_ENTERED DESC"
        Set rstPropertyInterests = db.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)
        If Not rstPropertyInterests.EOF Then
            strNetRevenueInterest = rstPropertyInterests.Fields("NET_REVENUE_INTEREST")
            strWorkingInterest = rstPropertyInterests.Fields("WORKING_INTEREST")
        Else
            strNetRevenueInterest = ""
            strWorkingInterest = ""
        End If
        rstDataHeader.AddNew
        rstDataHeader.Fields("PROPNUM") = rstSourceTable.Fields("PROPNUM")
        rstDataHeader.Fields("WELL_NAME") = rstSourceTable.Fields("WELL_NAME")
        rstDataHeader.Fields("OPERATOR") = rstSourceTable.Fields("OPERATOR")
        rstDataHeader.Fields("WORKING_INTEREST") = strWorkingInterest
        rstDataHeader.Fields("NET_REVENUE_INTEREST") = strNetRevenueInterest
        rstDataHeader.Fields("DATA_ENTERED_BY") = DLookup("[CurrentUser]", "LOCALtblCurrentUser")
        rstDataHeader.Fields("DATE_ENTERED") = Date
        rstDataHeader.Fields("INVOICE_NUMBER") = strInvoiceNumber
        rstDataHeader.Update
        rstDataHeader.Bookmark = rstDataHeader.LastModified
        lngRecordNumber = rstDataHeader.Fields("RECORD_NUMBER")
        strSQL = "UPDATE tblTEMPDataImportStepOne SET tblTEMPDataImportStepOne.RECORD_NUMBER = " & lngRecordNumber & " WHERE tblTEMPDataImportStepOne.PROPNUM = '" & rstSourceTable.Fields("PROPNUM") & "'"
        CurrentDb.Execute strSQL, dbSeeChanges
        rstPropertyInterests.Close
        rstSourceTable.MoveNext
    Loop
    
    rstSourceTable.Close
    rstDataHeader.Close

    Set rstSourceTable = Nothing
    Set rstPropertyInterests = Nothing
    Set rstDataHeader = Nothing
    Set db = Nothing
    
End Sub
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros