On Apr 6, 2007, at 6:43 am, Ian Piper wrote:
> I have a fairly simple piece of code that is creating an exception,
> and I can't understand why. This code checks a database table for the
> existence of a record with a particular field populated. The first
> time the table has no rows, so I am expecting temp to be "" in the
> example below on the first run, (because rs.RecordCount > 0 should be
> false). So I can't understand why the code is stepping into that if
> statement nor indeed why it then raises an exception.
>
> The code is below: any help much appreciated.
>
> if db.Connect then
> rs = db.SQLSelect("select user.username, user.userid,
> key.userid, key.keyid, key.newkey from user,key where
> user.username='" + username +"' and user.userid=key.userid")
> if rs.RecordCount > 0 then
> while not rs.eof
> temp=rs.Field("key.newkey").Value // <-- this is where the
> NilObjectException is raised
> 'next record - there shouldn't actually be one
> rs.movenext
> wend
> else
> // there was no newKey record in the table
> // so return ""
> temp = ""
> end if
> db.close
> else
> // database connection failed
> MsgBox "Sorry, there was a problem opening the database to fetch
> an account. The program has to close. [Error code 95]"
> quit
> end if
>
Ian,
Where you have your "if" statement:
> if rs.RecordCount > 0 then
I usually do this:
> if rs <> nil then
Also, you might want to insert a command to check db.errormessage
before accessing the recordset, to see if the sql is returning an
error of some kind (like a misspelled field name). I usually do this:
rs = db.sqlselect(sqlcommand)
#if debugbuild then
msgbox db.errormessage
#endif
if the errormessage is blank, you don't get a box, but if there's an
error, at least you'll get some feedback.
GregO
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>