run time error 3265 "item cannot be found in the collection"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • steve c
    New Member
    • Dec 2010
    • 1

    run time error 3265 "item cannot be found in the collection"

    Code:
    Dim rs As New ADODB.Recordset
    Dim oCnn As ADODB.Connection
    Set oCnn = New ADODB.Connection
    'Windows Integrated Security Login
    'oCnn.ConnectionString = "provider=sqloledb;data source=intel;initial catalog=Northwind;integrated security=sspi;"
    'oCnn.Open
    
    oCnn.Open "Driver={SQL Server};" & _
    "Server=(local);" & _
    "Database=Northwind;" & _
    "Uid=sa;" & _
    "Pwd=sa"
    
    Set rs = oCnn.Execute("SELECT * FROM Customers")
    
    'While Not rs.EOF
    For i = 1 To rs.Fields.Count
        
        MsgBox ("current field name = ") & rs.Fields(i).Name
        
    
    Next i
    
        MsgBox ("no of fields = ") & i
        
    'Wend
    hi above is the code. supposed to return all the fields names. i counted there are only 11 columns effectively. however using the above codes it returns 12 columns which 1 column extra and could be causing the error. why is 12 columns returned by the system?
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Collections are 0-based - that is, the first item in the Fields collection is actually Fields(0), and the last item is Fields(FieldCou nt - 1).

    Line 18, your FOR loop, should be modified as follows:

    Code:
    For i = 0 To rs.Fields.Count - 1
    -Stewart

    Comment

    • smartchap
      New Member
      • Dec 2007
      • 236

      #3
      Also after For..Next loop value of i will be 1 more than number of columns so for
      MsgBox ("no of fields = ") & i
      you will get no of fields = 12 (for 11 fields).

      Comment

      Working...