Item not found in this collection

SteveL13
SteveL13 used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

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

Top Expert 2010

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

Author

Commented:
Rey,  sorry.  Which line is that?
Top Expert 2009

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial