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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With