Abstract repeating error messages

I have tried to figure out a way to abstract my error messages so that I am not repeating the same code following every every sql statement.
Virtually all statements that return an error display a message box to the user with the what was taking place when the error occurred followed by the actual sql error message. Then the application “System Log” table is updated with the same message and the method where the error occurred. (The “E” is there to indicate if the Log Message is an error or recording access information - not really relevant to this question).

Then if the method is retrieving a particular record as this one is, the same process is done if the record isn’t found - usually the used has selected the record from a list before hand so if it’s not there now, it’s an error.

I can’t figure out a way to separate the error checking from the method because it is also so closely related to where and what’s going on, that passing all the required parameters would be about as hard as just duping the code.

Anyone have a better way?


[code]Private Function GetVendor(iVendor As Integer) as String
dim rs As RecordSet
dim sMsg As String
dim sVendorName As String

rs = dbSQL.SQLSelect("SELECT pkRecID, Company from Contacts WHERE pkRecID = " + str(iVendor))

//=== the code I would like to abstract
if dbSQL.Error then
sMsg = "Database Error retrieving Vendor Name from Contacts Table for pkRecID = " + str(iVendor) + " " + dbSQL.ErrorMessage
UpdateLog(CurrentMethodName, sMsg, “E”)
sVendorName = “Error retrieving Vendor Name”
end if
if rs.RecordCount = 0 then
sMsg = "Record not found for Vendor after selection - pkRecID = " + str(iVendor)
UpdateLog(CurrentMethodName, sMsg, “E”)
sVendorName = “Vendor not found”
end if

sVendorName = rs.Field(“Company”).StringValue

Return sVendorName
End Function

What I do is to create a SELECT and EXECUTE method that calls the DB function, but encapulastes the error checking as well
so instead of calling DB.SQLExecute I call myEXECUTE(sql,db) instead…

If this is a project that uses either ActiveRecord or Shorts, you should have access to the BKS SQL extensions. Inside is SQLExecuteRaiseOnError (and Select) which are extends methods for both database objects and/or prepared statements.

The methods are quite handy, because they perform the query and raise an exception if there was an error without you needing to check each and every time.

Okay thanks guys,

And Tim, it does use Shorts…