2012-04-19 181 views
6

我想查詢從VBS SQL數據庫,但在沒有找到記錄我得到一個錯誤EOF和BOF錯誤,當SQL數據庫

ADODB.Field:BOF或EOF爲True,或當前記錄已被刪除。請求的操作需要當前記錄。

我想我需要使用IF NOT語句來捕獲記錄沒有找到,但我不知道它需要去哪裏。

Do Until objFile.AtEndofStream 
    strAppName = objFile.ReadLine 

    ConnString="DRIVER={SQL Server};SERVER=aardvark002;UID=***;PWD=***;DATABASE=DEW_Users" 
    SQL = "USE Win7AppData SELECT " & Chr(34) & strCountry & Chr(34) & " FROM AppsByCountry WHERE Application = '" & strAppName & "'" 

    Set Connection = CreateObject("ADODB.Connection") 
    Set Recordset = CreateObject("ADODB.Recordset") 
    Connection.Open ConnString 
    Recordset.Open SQL,Connection 
    strApproval = Recordset(strCountry) 
    If StrApproval = "YES" Then 
     strApproval = "Approved" 
    Else 
     strApproval = "Denied" 
    End If 
    objExcel.Cells(intRow, 1).Value = strAppname 
    objExcel.Cells(intRow, 2).Value = strCountry 
    objExcel.Cells(intRow, 3).Value = strApproval 
    intRow = intRow + 1 
Loop 

回答

5

有點生疏了我的VBScript,但你應該能夠使用.EOF的記錄,以檢查它是否在最後:

Recordset.Open SQL,Connection 
If Recordset.EOF = false Then 
    ' have some rows, do what you want with them 
End If 

W3Schools reference

+0

+1當使用ADODB時,它自己的.EOF引用就足夠了,例如,在MS Access中使用DAO時,只需要組合.BOF&.EOF。 – 2012-04-20 07:56:10

0

你可以有如果你有多條記錄作爲結果,另一個錯誤也會發生,那麼如果你不想以無限循環結束,那麼你需要移動記錄指針,另外我還縮短了你的代碼,另一方面你需要關閉你的conn如果你不打算再次使用它。

ConnString="DRIVER={SQL Server};SERVER=aardvark002;UID=***;PWD=***;DATABASE=DEW_Users" 
Set Connection = CreateObject("ADODB.Connection") 
Connection.Open ConnString 
Do Until objFile.AtEndofStream 
    strAppName = objFile.ReadLine 
    SQL = "USE Win7AppData SELECT " & Chr(34) & strCountry & Chr(34) & " FROM AppsByCountry WHERE Application = '" & strAppName & "'" 
    Set Recordset = Connection.Execute(SQL) 
    Do While not Recordset.EOF 
    strApproval = Recordset(strCountry) 
    If StrApproval = "YES" Then 
     strApproval = "Approved" 
    Else 
     strApproval = "Denied" 
    End If 
    objExcel.Cells(intRow, 1).Value = strAppname 
    objExcel.Cells(intRow, 2).Value = strCountry 
    objExcel.Cells(intRow, 3).Value = strApproval 
    intRow = intRow + 1 
    Recordset.MoveNext 
    End If 
Loop 
Connection.Close 
Set Connection = nothing 
0

我檢查Recodset.EOFRecordset.BOF,以確保這兩個都是假的,但每次我收到提到的錯誤。這花了我幾個小時,但我終於明白,如​​果你撥打Recordset.Fields.countEOFBOF改爲True

我希望這可以是有用的。

相關問題