2013-01-02 20 views
5

我想查询一个MySQL数据库与下面的代码:行集不支持滚动落后

'declare the variables 
Dim Connection 
Dim Recordset 
Dim SQL 

'declare the SQL statement that will query the database 
SQL = "SELECT * FROM CUSIP" 

'create an instance of the ADO connection and recordset objects 
Set Connection = CreateObject("ADODB.Connection") 
Set Recordset = CreateObject("ADODB.Recordset") 

'open the connection to the database 
Connection.Open "DSN=CCS_DSN;UID=root;PWD=password;Database=CCS" 

Recordset.CursorType=adOpenDynamic 

'Open the recordset object executing the SQL statement and return records 

Recordset.Open SQL,Connection 
Recordset.MoveFirst 

If Recordset.Find ("CUSIP_NAME='somevalue'") Then 
    MsgBox "Found" 
Else 
    MsgBox "Not Found" 
End If 


'close the connection and recordset objects to free up resources 
Recordset.Close 
Set Recordset=nothing 
Connection.Close 
Set Connection=nothing 

每当我执行上面我得到一个错误“行集不支持向后滚动”,有什么建议?

+0

根据http://msdn.microsoft.com/en-us/library/ee275542%28v=bts.10%29.aspx你不使用。正确查找。 –

回答

6

adOpenDynamic未在VBScript中声明,因此等于Empty,因此当您分配CursorType属性时将转换为0
0adOpenForwardOnly,并且仅向前不支持向后移动,方法需要的能力。

你应该与它的字面值替换adOpenDynamic

Recordset.CursorType = 2 'adOpenDynamic 

为了避免这一类的错误完全,放置Option Explicit作为脚本的第一行。

+0

这个伎俩!谢谢 !!顺便说一句,你会知道为什么'recordset.recordcount'返回-1,当数据库中有5行并cursortype改变为动态? –

+0

@TelsonAlva因为它是[记录](http://msdn.microsoft.com/ru-ru/library/windows/desktop/ms676701(v = vs.85).aspx):'游标类型Recordset对象影响是否可以确定记录的数量。 RecordCount属性将为只向前游标返回-1;静态或键集游标的实际计数;和-1或动态光标的实际计数,具体取决于数据源。' – GSerg

+0

哦,我明白了!所以我需要将光标类型更改为静态以获得计数。否则,由于它是动态的,因此记录可能会动态变化。谢谢 ! –

0

这是因为行集不允许向后移动;如错误消息所示。你的代码没有使用它们;因此应该与 Recordset.CursorType = adOpenForwardOnly(或等效值0)

更好离开共线替换行

Recordset.CursorType = adOpenDynamic ;默认是正向游标。

+0

你反过来了。 Find方法希望能够向后移动。 – GSerg

相关问题