Avatar of SteveL13
SteveL13
Flag for United States of America asked on

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

Microsoft Access

Avatar of undefined
Last Comment
Helen Feddema

8/22/2022 - Mon
SteveL13

ASKER
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

Patrick Matthews

When that error comes up, click 'debug'. Now, which line does the debugger jump to?
ASKER CERTIFIED SOLUTION
Rey Obrero (Capricorn1)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SteveL13

ASKER
Rey,  sorry.  Which line is that?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Helen Feddema

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.