ADODB.Recordset error '800a0cc1'

The 800a0cc1 error returned by the ADO Recordset Object is usually accompanied by the following text:

ADODB.Recordset error '800a0cc1'
Item cannot be found in the collection corresponding to the requested name or ordinal.

The error occurs in one of two situations:

  • You try to access a field in the fields collection that is not in the Recordset
  • You try to access a field using an ambiguous name.

Scenario 1 - Missing field
Consider the following code:

<%
strSQL = _
   "SELECT field1, field2 " & _
   "FROM table1 " & _
   "WHERE field1 = 'foobar'"

Set objRS = Server.CreateObject("ADODB.Recordset")
With objRS
   .Source = strSQL
   .ActiveConnection = objConn
   .CursorType = adOpenForwardOnly
   .LockType = adLockReadOnly
End With

objRS.Open ,,,,adCmdText

Response.Write(objRS("field3"))
%>

An error will be generated by the last line of code because an attempt it being made to access a field that is not present in the recordset. Looking at the first line of code we can see that the SQL string that we are creating selects only field1 and field2 from the database - and our resulting recordset will contain only those two fields, not field3.

Scenario 2 - Ambiguous Field Name
Consider the following code:

<%
strSQL = _
   "SELECT table1.ID, table2.ID " & _
   "FROM table1, table2 " & _
   "WHERE table1.ID = 1"

Set objRS = Server.CreateObject("ADODB.Recordset")
With objRS
   .Source = strSQL
   .ActiveConnection = objConn
   .CursorType = adOpenForwardOnly
   .LockType = adLockReadOnly
End With

objRS.Open ,,,,adCmdText

Response.Write(objRS("ID"))
%>

In this case an error will be generated because the ADO Recordset Object does not know which ID field you are attempting to write to the screen. To solve this problem either:

  • rename your fields so that they don't have the same name
  • use the SQL "AS" keyword
  • use ordinals

Probably the best of the above alternatives involves renaming your fields. For example, if table1 and table2 above where instead called "Customers" and "Orders" then instead of calling the fields simply "ID" an alternative naming scheme would have called the two fields "CustomerID" and "OrderID" respectively.

If renaming the fields is out of the question, then another alternative involves using the "AS" keyword, as shown in the following code (the code to open the recordset is omitted):

<%
strSQL = _
   "SELECT Customers.ID AS CustomerID, " & _
   "Orders.ID AS OrderID " & _
   "FROM Customers, Orders " & _
   "WHERE Customers.ID = 1"
'...
Response.Write( _
   objRS("CustomerID") & "<br>" & vbCrLf &_
   objRS("OrderID") & "<br>" & vbCrLf)
%>

The last alternative involves using ordinals instead. By using an ordinal reference we avoid any confusion over which ID field we are attempting to reference:

<%
strSQL = _
   "SELECT table1.ID, table2.ID " & _
   "FROM table1, table2 " & _
   "WHERE table1.ID = 1"
'...
Response.Write( _
   objRS.Fields(0).Value & "<br>" & vbCrLf &_
   objRS.Fields(1).Value & "<br>" & vbCrLf)
%>

转载于:https://www.cnblogs.com/flykiss/archive/2004/03/20/3700.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值