VBA Code: Run Time Error 3265

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darkhat01
    New Member
    • Jul 2007
    • 16

    #1

    VBA Code: Run Time Error 3265

    I am getting a Run-Time error '3265':
    Item cannot be found in the collection corresponding to the requested name or ordinal.

    The Function call I am using is:

    Code:
    =getNextLastActivityDataPlanReviewScore([ID],"Review","TARGET_DATE","InitialScore")
    I think that is has something to do with sReturn = rst("sScore") in my function. But I am not 100% sure. Any Ideas would help.

    Here is the Function Code:

    Code:
    Public Function getNextLastActivityDataPlanReviewScore(nID, sType, sNextLast, sScore)
        Dim cnThisConnect As ADODB.Connection
        Set cnThisConnect = CurrentProject.Connection
        Dim rst As New ADODB.Recordset
        Dim sSQL As String
        Dim sReturn As String
        
        
    sSQL = "SELECT " & sNextLast & " AS fDate FROM DATA_PLAN_ACTIVITIES "
    sSQL = sSQL & " WHERE DATA_PLAN_ACTIVITIES.ACTIVITY_TYPE = '" & sType & "' "
    sSQL = sSQL & " AND DATA_PLAN_ACTIVITIES.APP_ID = " & nID
    sSQL = sSQL & " AND DATA_PLAN_ACTIVITIES." & sScore
    sSQL = sSQL & " AND " & sNextLast & " IS NOT NULL "
    sSQL = sSQL & " AND " & sScore & " IS NOT NULL "
    sSQL = sSQL & " ORDER BY " & sNextLast & " DESC"
    
            
    
        rst.Open sSQL, cnThisConnect, adOpenKeyset, adLockOptimistic
    
        If Not rst.BOF Then
        rst.MoveFirst
                sReturn = rst("sScore")
        Else
            sReturn = "None"
        End If
        rst.Close
        cnThisConnect.Close
        
        Set rst = Nothing
        Set cnThisConnect = Nothing
       MsgBox (sReturn)
       getNextLastActivityDataPlanReviewScore = sReturn
    End Function
    Thanks for any help,

    Darkhat01
  • FishVal
    Recognized Expert Specialist
    • Jun 2007
    • 2656

    #2
    Originally posted by darkhat01
    I am getting a Run-Time error '3265':
    Item cannot be found in the collection corresponding to the requested name or ordinal.

    The Function call I am using is:

    Code:
    =getNextLastActivityDataPlanReviewScore([ID],"Review","TARGET_DATE","InitialScore")
    I think that is has something to do with sReturn = rst("sScore") in my function. But I am not 100% sure. Any Ideas would help.

    Here is the Function Code:

    Code:
    Public Function getNextLastActivityDataPlanReviewScore(nID, sType, sNextLast, sScore)
        Dim cnThisConnect As ADODB.Connection
        Set cnThisConnect = CurrentProject.Connection
        Dim rst As New ADODB.Recordset
        Dim sSQL As String
        Dim sReturn As String
        
        
    sSQL = "SELECT " & sNextLast & " AS fDate FROM DATA_PLAN_ACTIVITIES "
    sSQL = sSQL & " WHERE DATA_PLAN_ACTIVITIES.ACTIVITY_TYPE = '" & sType & "' "
    sSQL = sSQL & " AND DATA_PLAN_ACTIVITIES.APP_ID = " & nID
    sSQL = sSQL & " AND DATA_PLAN_ACTIVITIES." & sScore
    sSQL = sSQL & " AND " & sNextLast & " IS NOT NULL "
    sSQL = sSQL & " AND " & sScore & " IS NOT NULL "
    sSQL = sSQL & " ORDER BY " & sNextLast & " DESC"
    
            
    
        rst.Open sSQL, cnThisConnect, adOpenKeyset, adLockOptimistic
    
        If Not rst.BOF Then
        rst.MoveFirst
                sReturn = rst("sScore")
        Else
            sReturn = "None"
        End If
        rst.Close
        cnThisConnect.Close
        
        Set rst = Nothing
        Set cnThisConnect = Nothing
       MsgBox (sReturn)
       getNextLastActivityDataPlanReviewScore = sReturn
    End Function
    Thanks for any help,

    Darkhat01
    You are right, this is exactly where the error occurs.
    You don't retrieve field [sScore] into your recordset. Surely "Item cannot be found in the collection".

    And, G..d save us, what this line supposed to do
    Code:
    cnThisConnect.Close
    close current project connection ???
    fortunately Access will not allow this operation as well as will not raise an error - simply nothing will happen

    Comment

    • darkhat01
      New Member
      • Jul 2007
      • 16

      #3
      What would be the best way to retrieve sScore into the record set?

      Something like:

      Code:
       sSQL = "SELECT " & sScore & " AS fDate FROM DATA_PLAN_ACTIVITIES "
      Then have this?

      Code:
      sReturn = rst(fScore)
      I tried this but I get the same error…..

      Comment

      • FishVal
        Recognized Expert Specialist
        • Jun 2007
        • 2656

        #4
        Originally posted by darkhat01
        What would be the best way to retrieve sScore into the record set?

        Something like:

        Code:
         sSQL = "SELECT " & sScore & " AS fDate FROM DATA_PLAN_ACTIVITIES "
        Then have this?

        Code:
        sReturn = rst(fScore)
        I tried this but I get the same error…..
        The best way would be
        Something like:

        Code:
         sSQL = "SELECT " & sScore & " AS fDate FROM DATA_PLAN_ACTIVITIES 
        .......
        sReturn = rst("fDate")
        Only be sure sScore string contains a valid field name compliant with field naming convention (without forbidden symbols, not a reserved word) otherwise enclose the name into square brackets.

        Comment

        • darkhat01
          New Member
          • Jul 2007
          • 16

          #5
          FishVal,

          Thank you so much, this worked.

          Maybe you can explain to me why it works now. My understanding is that fDate is a value, why would I need to but quotes around it. This make it a string correct?

          Thank you again,

          darkhat01

          Comment

          • FishVal
            Recognized Expert Specialist
            • Jun 2007
            • 2656

            #6
            Originally posted by darkhat01
            FishVal,

            Thank you so much, this worked.

            Maybe you can explain to me why it works now. My understanding is that fDate is a value, why would I need to but quotes around it. This make it a string correct?

            Thank you again,

            darkhat01
            Well. Actually it isn't a value. Its a field name.
            When you open recordset on SQL expression like
            Code:
            SELECT Field1 AS Field1_Alias FROM Table
            field having name "Field1" is being retrieved with the name "Field1_Ali as".

            What concerning RecordSet object.
            RecordSet object has Fields property returning Fields collection.
            Fields collection has Item property which receives member name or ordinal and returns member which is object of class Field.
            Field object has property Value which returns the value of the field - actually what do you need.

            So we have the following
            Code:
            Dim varVariable as Variant
            Dim RS as Recordset
            ........
            varVariable = RS.Fields.Item("FieldName").Value
            properties Fields, Item and Value are default for the correspondent parent class, so the last line in a pretty way turns to
            Code:
            varVariable = RS("FieldName")
            Moreover vb provides two object operators "." (dot) to refer to object property/method, "!" (bang) to refer to collection member (w/o explicitely calling Collection.Item property).

            we have collection Fields, using bang operator we can refer to collection member this way
            Code:
            varVariable = RS.Fields!FieldName.Value
            or simply
            varVariable = RS!FieldName
            VBA (like other IDE's) has ObjectBrowser. I reccomend you to use it whenever you have questions concerning object model.

            BTW you obviously have used expression like
            Code:
            var = Forms!FormName!ControlName
            if we rewrite this with all default properties called behind the scenes, then we will see expression like this
            Code:
            var = Forms.Item("FormName").Controls.Item("ControlName").Value

            Comment

            • darkhat01
              New Member
              • Jul 2007
              • 16

              #7
              Thank you for taking the time to explain this to me.

              Darkhat01

              Comment

              Working...