help with error trapping

d2tw4all
01-13-2004, 04:26 PM
Hello,
I'm currently querying an SQL database with the following commands:

oConn.Open sConnection
oRs.Open sSQL, oConn

getntusername = oRs("ntaccount")

oRs.Close
oConn.Close
Set oRs = Nothing
Set oConn = Nothing


There is a bunch before and after but this is the gist of it, basically it all works fine if the record exists, but if it finds a record missing it errors out. How can I error trap for this, basically I'm passing a full name at the database and it is returning an NT username, this query is how I'm confirming for valid NT username so if the username doesn't exist I just want to write it to a log. I can handle the log writing part, however I need to be able to trap it if it fails rather than an error. On error resume next results in the program hanging so that isn't an option... Any ideas?
Tom

couch612
01-14-2004, 12:09 AM
to perform error trapping, use the following subroutine construct:


sub MyRoutine
On Error goto Error_MyRoutine

'subroutine commands here

Exit_MyRoutine:

Exit Sub

'this section is executed if an error occurs:
Error_MyRoutine:

if err = [insert error number to check for here] then
do something
else
'you might want to format the following a little better
msgbox "Unexpected Error: " & err & " Line: " & erl & " Message: " & error$
end if

resume Exit_MyRoutine

End Sub

d2tw4all
01-14-2004, 09:16 AM
to perform error trapping, use the following subroutine construct:


sub MyRoutine
On Error goto Error_MyRoutine

'subroutine commands here

Exit_MyRoutine:

Exit Sub

'this section is executed if an error occurs:
Error_MyRoutine:

if err = [insert error number to check for here] then
do something
else
'you might want to format the following a little better
msgbox "Unexpected Error: " & err & " Line: " & erl & " Message: " & error$
end if

resume Exit_MyRoutine

End Sub



How about:

oConn.Open sConnection
oRs.Open sSQL, oConn

if not ors.eof then
getntusername = oRs("ntaccount")
else
getntusername = "Not Found"
endif

oRs.Close
oConn.Close


That look like it'd work?
Tom

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum