Save data before closing application

I want to save some data to a SQLite database before the application closes. I’ve tried Close and CancelClose for both the App and main Window, but access to the database is not available. Is there an event I can use?

You are correct that you want to use the CancelClose event handler. Whether or not your DB is available is dependent on the design of your app. Where is the DB reference? What do you mean by “not available”?

If access to the database is not available it’s because you closed the database elsewhere in your app before you quit the app. Create a method for saving your data, then call the method from the close event.

App.CancelClose
WindowMainClose
Return False

App.WindowMainClose
// save internal use data

Dim rs As RecordSet
Dim i As Integer

rs = App.rakDataDB.SQLSelect(“SELECT * FROM InternalUse WHERE InternalUseID = ‘1’”)
// if error
if App.rakDataDB.Error then
Beep
MsgBox "Database Error: " + Str(App.rakDataDB.ErrorCode) + EndOfLine + EndOfLine + App.rakDataDB.ErrorMessage
return
end If

If rs = Nil then
MsgBox “Unable to access Exposure table.”
Beep
Return
End If

rs.Edit

rs.Field(“LastTabSelected”) = App.InternalUseLastTabSelected

// Update the record in the database
rs.Update
// if errors
if App.rakDataDB.Error then
Beep
MsgBox "Database Error: " + Str(App.rakDataDB.ErrorCode) + EndOfLine + EndOfLine + App.rakDataDB.ErrorMessage
return
end if

i’m getting a ‘This item does not exist’ in the bold field (rs.Field).

You get this when you try to run?

If so, it would imply that InternalUseLastTabSelected is not a property of App.

It is a property of App. I’ve been using it throughout the application. And I get the same error if I set rs.Field = “”

Sorry, I read that too quickly.

The syntax to set a field is:

rs.Field(“LastTabSelected”).Value = App.InternalUseLastTabSelected

https://documentation.xojo.com/index.php/DatabaseField

uhhh… thank you Paul.