Finally
db.CommitTransaction 'put commit here?
End Try
If Error.Message = “” or Error.ErrorNumber = 0 or Not db.Error Then 'Error is out of scope and db.Error is not API 2.0
db.CommitTransaction 'or put commit here?
End If[/code]
[quote=483881:@Norman Palardy]try
update database
commit
catch
// handle error
end try[/quote]
I would generally add to Norman’s response
try
update database
commit
catch <whatever error>
// handle error
rollback
end try
And remember that, unlike API 1.x, API 2.0 will complain if you try to Commit or Rollback without having started a Transaction.
One other minor issue with API 1.x is that each hit on the database would generally be in its own If…Then block and it was easy to track the errors. In API 2.0, hits on the database can be packed into a single Try/Catch block. That means that the error being caught can be at any of them and it can be hard to distinguish exactly which of multiple calls of the same type actually had the error. So I generally use a local variable and adjust its value for each call to the database so I can report that value.
[code]Var ErrLoc As String
If theDB.Connect() then
Try
ErrLoc = “Unable to alter Writers table”
theDB.BeginTransaction
theDB.ExecuteSQL (“ALTER TABLE Writers ADD WebAddr varchar”)
ErrLoc = "Unable to alter Preferences table"
theDB.ExecuteSQL ("UPDATE Preferences SET DBVersion = 5")
theDB.CommitTransaction
Catch err As DatabaseException
MessageBox (CurrentMethodName, Str(err.ErrorNumber), err.Message, ErrLoc)
theDB.RollbackTransaction
End Try
End If[/code]
It just makes tracking where the error occurred easier.