Can't set radio button

Hello all,

I am new to XOJO and have a problem that I can’t solve.

I want to store the initial settings of radio buttons in a small sqlite database. I know how to set the initial settings of radio buttons via the on/off switch of the “initial state” section but I want to control it via a database to keep it variable for a user so to say as “user settings”.

If the program starts and the window opens it checks if the settings_database.sqlite exists. If not: the database will be created and one record with integer values will be written. If the database exists: The one record will be read and according to the value of a database field (0 or 1) a radio button should set to “false” or “true”. The strange thing is: Reading the record works. Setting the radio button to “false” or “true” generally works. But both steps consecutive do not work together as shown in my code below. I think the problem is related to the event handler “open”. Has anyone an idea how to make that piece of code work?

[code]dim f as FolderItem = GetFolderItem (“settings_database.sqlite”)
dim sdb as new SQLiteDatabase
dim sdbsql as string
dim rssdb as RecordSet
sdb.DatabaseFile = f

if not f.Exists then
If sdb.CreateDatabaseFile Then
sdb.SQLExecute(“CREATE TABLE Settings(rbtWeight_0 INTEGER, rbtWeight_1 INTEGER, rbtElevation_0 INTEGER, rbtElevation_1 INTEGER, rbtPressure_0 INTEGER, rbtPressure_1 INTEGER, rbtTemperature_0 INTEGER, rbtTemperature_1 INTEGER)”)
Dim row As New DatabaseRecord
row.Column(“rbtWeight_0”) = “0”
row.Column(“rbtWeight_1”) = “1”
row.Column(“rbtElevation_0”) = “1”
row.Column(“rbtElevation_1”) = “0”
row.Column(“rbtPressure_0”) = “0”
row.Column(“rbtPressure_1”) = “1”
row.Column(“rbtTemperature_0”) = “0”
row.Column(“rbtTemperature_1”) = “1”
sdb.InsertRecord(“Settings”, row)
If sdb.Error Then
MsgBox("DB Error: " + sdb.ErrorMessage)
End If
end if
end if

sdbsql = “SELECT * FROM Settings”
dim datasdb as RecordSet

sdb.DatabaseFile = GetFolderItem(“settings_database.sqlite”)
if sdb.connect then
datasdb=sdb.sqlselect(sdbsql)
If datasdb <> Nil Then
if datasdb.field(“rbtWeight_0”).Value = 0 then
rbtWeight(0).value = false and rbtWeight(1).value = true
else
MsgBox("Connection error: " + sdb.ErrorMessage)
end if
end if
end if
[/code]
Thank you very much in advance and best regards from Germany,
Gerson Nerger

Edit (Paul): Added Code formatting tags per Post Formatting Tips.

rbtWeight(0).value = false and rbtWeight(1).value = true

Separate these two lines:

rbtWeight(0).value = false rbtWeight(1).value = true

Or you are doing a logical expression like (but not exactly)

a = 0 AND 1
which is
a=0

rather than just setting the radio flags to true or false

that and you really only have to set the TRUE button… that is what RADIO buttons are about… set one to TRUE the rest will go to false automatically

Thank you to both. It works and I have learned a lot :wink:

Best regards,
Gerson Nerger

Or instead of storing the state of each radio in the group, store an integer (or boolean if its just a 0/1 state) of the selected radio so you only have to:

rbtWeight( datasdb.field(“rbtWeight”).Value ).value = true

There are more ways than one to skin a cat :slight_smile:

Thats a bit grim, but you know what I mean :wink:

Have fun!

This is more ellegant and keeps the code short …

Thank you, Julian!

Best regards,
Gerson