Item not found in this collection

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

Open in new window

SteveL13Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteveL13Author Commented:
Now getting too few parameters expected 1 with:

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, tblTEMPDataImportStepOne.INVOICE_NUMBER, 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") = rstSourceTable.Fields("INVOICE_NUMBER")
        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

Open in new window

0
Patrick MatthewsCommented:
When that error comes up, click 'debug'. Now, which line does the debugger jump to?
0
Rey Obrero (Capricorn1)Commented:
there are lots of fields that you did not include in your strSQL statement, ("PROPNUM"), WellName, Operator etc...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteveL13Author Commented:
Rey,  sorry.  Which line is that?
0
Helen FeddemaCommented:
Step through the code with F8, to find the line that triggers the error.  For "Item not found in this collection," generally that happens when a field has been removed or renamed, or you just didn't type its name correctly.  For the parameter request, that might be a query that references a form control on a form that is not open.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.