2013-01-02 49 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