Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag 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

Avatar of SteveL13
SteveL13
Flag of United States of America image

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

Avatar of Patrick Matthews
When that error comes up, click 'debug'. Now, which line does the debugger jump to?
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Rey,  sorry.  Which line is that?
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.