[ RB4] To test a cursor ?

Deane Venske dean at eduss.com
Fri Oct 24 15:02:30 CDT 2003


The following will firstly check if the cursor was Nil (Invalid because 
of an SQL mistake or some similar reason) and then it will check if it 
has records in it.

Dim sqlQuery As String
  sqlQuery = "SELECT * FROM userD"
  cursor = db.SQLSelect(sqlQuery)
  if cursor = Nil then
    msgbox "The cursor returned was invalid. The SQL being executed was 
:" + EndOfLine + sqlQuery + EndOfLine + "Valentina Reported the 
following error :" + EndOfLine + db.ErrString
  elseif cursor.FirstRecord() then
    msgbox "Records Found."
  else
    msgbox "No Records Found."
  end if
 
If you want you can do what I did and throw a method into a module to 
handle this a little easier for you so you don't have to rewrite code...
----------
Function vQuery(sqlQuery As String) As VCursor
  Dim queryCursor As VCursor
 
  queryCursor = db.SQLSelect(sqlQuery)
  if queryCursor = Nil then
    msgbox "The cursor returned was invalid. The SQL being executed was 
:" + EndOfLine + sqlQuery + EndOfLine + "Valentina Reported the 
following error :" + EndOfLine + db.ErrString
  else
    return queryCursor
  end if

End Function

Use it like this :
Dim cursor As Vcursor       <------- I actually have a property in a 
global module so I don't have to continuously declare cursor unless I 
need more than 1 cursor at one time.
cursor = vQuery("SELECT * FROM user")
if cursor.FirstRecord() then
    msgbox "Records Found."
else
    msgbox "No Records Found."
end if
cursor = Nil

It only saves a couple of key strokes but it just means you've got some 
exception handling and it gives you a little more feedback for when your 
queries fail.



More information about the Valentina mailing list