Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EOF and BOF error when querying SQL database

Tags:

sql

vbscript

I'm trying to query a SQL database from vbs but when no record is found I get an error

ADODB.Field : Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.

I think I need to use an IF NOT statement to capture if the record isn't found but I can't figure out where it needs to go.

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
like image 653
chemist Avatar asked Apr 19 '12 09:04

chemist


2 Answers

Bit rusty on my VBScript, but you should be able to use .EOF on the Recordset to check if it's at the end:

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

W3Schools reference

like image 65
AdaTheDev Avatar answered Nov 19 '22 04:11

AdaTheDev


I was checking the Recodset.EOF and Recordset.BOF to make sure that both are False, but everytime I was receiving the mentioned error. That took me some hours but I finally realized that if you call the Recordset.Fields.count the EOF and BOF are changed to True.

I hope this can be useful.

like image 40
Bruno Silva Avatar answered Nov 19 '22 04:11

Bruno Silva