{"id":415,"date":"2025-05-31T14:01:23","date_gmt":"2025-05-31T14:01:23","guid":{"rendered":"https:\/\/xojo.itbib4you.be\/?page_id=415"},"modified":"2025-05-31T14:17:05","modified_gmt":"2025-05-31T14:17:05","slug":"database-transactions","status":"publish","type":"page","link":"https:\/\/xojo.itbib4you.be\/index.php\/database-transactions\/","title":{"rendered":"Database Transactions"},"content":{"rendered":"\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-width wp-block-button__width-25 has-custom-font-size has-small-font-size\"><a class=\"wp-block-button__link has-light-green-cyan-to-vivid-green-cyan-gradient-background has-background wp-element-button\" href=\"https:\/\/xojo.itbib4you.be\/\">Overview<\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>In the previous lessons, all data was stored in an object created in RAM. When the application was terminated, all transactions (objects) were deleted. This, of course, is not acceptable. Financial institutions should keep all historical financial records.<\/p>\n\n\n\n<p>In this lesson, our transaction data will be stored in a database on a disk. A database on a disk may retain the data for later use. In this lesson, we will learn how a Xojo application can access a database and how data can be stored and retrieved from a database.<\/p>\n\n\n\n<p>It is also our intention to make the application more realistic and also to optimize the code in the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">User interface<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"490\" src=\"https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-30-om-14.52.31-1024x490.png\" alt=\"GUI\" class=\"wp-image-494\" style=\"width:551px\" srcset=\"https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-30-om-14.52.31-1024x490.png 1024w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-30-om-14.52.31-300x143.png 300w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-30-om-14.52.31-768x367.png 768w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-30-om-14.52.31.png 1196w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n<\/div>\n\n\n<p>The Graphical User Interface (GUI) has changed a bit. The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DepositButton<\/mark> and <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">WithdrawButton<\/mark> have been removed and replaced with a single <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">EnterButton<\/mark>. The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Pressed<\/mark> event of the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">EnterButton<\/mark> will take the amount entered in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DesktopTextField<\/mark> named <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AmountField<\/mark>. A positive amount is considered a deposit, a negative amount is considered a withdrawal.<\/p>\n\n\n\n<p>The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">ShowButton<\/mark> was also removed. Each transaction should now automatically change the balance shown in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">BalanceLabel<\/mark> control.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Databases<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-small-font-size\">In computing, a database is an organized collection of data or a type of data store based on the use of a database management system (DBMS) (Wikipedia)<\/p>\n<\/blockquote>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Relational<\/mark> databases are most commonly used. In a relational database, data is stored in tables. A <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">table<\/mark> with the name <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Student<\/mark> contains all the characteristic data of all individual persons: his (her) first (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">FirstName<\/mark>) and last name (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">LastName<\/mark>) , sex (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Sex<\/mark>), nationality and so on. One student&#8217;s data is kept in a <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">row<\/mark> of the table. The individual data elements of one student are kept in <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">columns<\/mark>. Each row in a table must be uniquely identified. This unique column (for instance the column named <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">StudentID<\/mark>) is called the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">primary key<\/mark>.<\/p>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table class=\"has-fixed-layout\"><thead><tr><th>StudentID<\/th><th>FirstName<\/th><th>LastName<\/th><th class=\"has-text-align-center\" data-align=\"center\">Sex<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>John<\/td><td>Winter<\/td><td class=\"has-text-align-center\" data-align=\"center\">M<\/td><\/tr><tr><td>2<\/td><td>Luis<\/td><td>Coleman<\/td><td class=\"has-text-align-center\" data-align=\"center\">M<\/td><\/tr><tr><td>3<\/td><td>Ann<\/td><td>Weber<\/td><td class=\"has-text-align-center\" data-align=\"center\">F<\/td><\/tr><tr><td>4<\/td><td>Peter<\/td><td>Cox<\/td><td class=\"has-text-align-center\" data-align=\"center\">M<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Student <\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table class=\"has-fixed-layout\"><thead><tr><th>CourseID<\/th><th>CourseTitle<\/th><th>CourseOwner<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>Databases<\/td><td>Ken Davis<\/td><\/tr><tr><td>2<\/td><td>Networking<\/td><td>Julie Foster<\/td><\/tr><tr><td>3<\/td><td>Java<\/td><td>Margaret Walters<\/td><\/tr><tr><td>4<\/td><td>Scratch<\/td><td>Lina Louis<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Course<\/figcaption><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Relationships<\/mark> between two tables are realized by combining the primary keys of both tables. The primary key of a table named <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Course<\/mark> can be stored in a column named <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">CourseID<\/mark>. A students&#8217;s enrollment in a particular course can be tracked in a new table named <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Enrollment<\/mark> by combining both primary keys (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">StudentID<\/mark> and <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">CourseID<\/mark>) in one row (for example student with StudentID = 3 enrolled in course with CourseID = 2).<\/p>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table class=\"has-fixed-layout\"><thead><tr><th>EnrollmentID<\/th><th>StudentID<\/th><th>CourseID<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>3<\/td><td>2<\/td><\/tr><tr><td>2<\/td><td>1<\/td><td>3<\/td><\/tr><tr><td>3<\/td><td>1<\/td><td>1<\/td><\/tr><tr><td>4<\/td><td>2<\/td><td>2<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Enrollment<\/figcaption><\/figure>\n\n\n\n<p>Xojo has built-in support for <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">SQLite<\/mark>, <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">MySQL<\/mark> and <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">PostgreSQL<\/mark> databases. Other databases can be accessed via the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">ODBC<\/mark> (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Open Database Connectivity<\/mark>) driver. Communication from Xojo to the DBMS is via <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">SQL<\/mark> (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Structured Query Language<\/mark>) commands. In this course, you will learn the basic SQL commands to manage data in a database. There are also a lot of tutorials available on the internet to learn SQL thoroughly.<\/p>\n\n\n\n<p>In this course, we will use SQLite databases to illustrate Xojo database operations with SQL.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to create a SQLite database?<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Using an external app<\/h3>\n\n\n\n<p>There are many (free) applications available on the internet that can help you to create, search and manage SQLite database files. Examples are <a href=\"https:\/\/sqlitebrowser.org\">DB Browser for SQLite <\/a>(DB4S), <a href=\"https:\/\/sqlitestudio.pl\">SQLiteStudio<\/a> and so on. A customised version usable for your operating system can be downloaded from the above websites.<\/p>\n\n\n<div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"654\" src=\"https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-22-om-15.40.15-1024x654.png\" alt=\"DB Browser for SQLite\" class=\"wp-image-426\" style=\"width:551px\" srcset=\"https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-22-om-15.40.15-1024x654.png 1024w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-22-om-15.40.15-300x192.png 300w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-22-om-15.40.15-768x490.png 768w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-22-om-15.40.15-1536x981.png 1536w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-22-om-15.40.15-2048x1308.png 2048w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-22-om-15.40.15-1200x766.png 1200w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><figcaption class=\"wp-element-caption\">DB Browser for SQLite<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Creating a SQLite database using an app is limited to giving the database a name and deciding where to store the database file on disk. In my example, I created the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">TransactionsDB.sqlite<\/mark> file (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">New Database<\/mark> button on the toolbar) and saved it in a new folder (called <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Xojo<\/mark>) on the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Desktop<\/mark> of my computer system.<\/p>\n\n\n\n<p>A database file can store a number of related tables. In our example, we only need one table named <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Accounts<\/mark> to store our transaction data. You can add a new table to the database using the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Create Table<\/mark> button. The table has 4 columns: <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">AccountID<\/mark> (Integer &#8211; primary key), <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">AccountName<\/mark> (Text), <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">AccountDeposit<\/mark> (Real) and <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">AccountWithdraw<\/mark> (Real).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using Xojo statements<\/h3>\n\n\n\n<p>Xojo provides all the statements you need to create a SQLite database and its tables.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"248\" src=\"https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-25-om-11.17.10-1024x248.png\" alt=\"Define db property\" class=\"wp-image-433\" style=\"width:551px;height:auto\" srcset=\"https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-25-om-11.17.10-1024x248.png 1024w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-25-om-11.17.10-300x73.png 300w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-25-om-11.17.10-768x186.png 768w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-25-om-11.17.10-1536x372.png 1536w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-25-om-11.17.10-2048x496.png 2048w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-25-om-11.17.10-1200x291.png 1200w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n<\/div>\n\n\n<ol class=\"wp-block-list\">\n<li>Define a <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Public<\/mark> property <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">db<\/mark> in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">App<\/mark>. The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Type<\/mark> of the property <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">db<\/mark> is <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">SQLitedatabase<\/mark> (see screenshot above). This variable is declared but has no value;<\/li>\n\n\n\n<li>Add the following code in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Opening<\/mark> event of the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">App<\/mark>:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/Opening event of App<\/mark><br>  <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Var<\/mark> dbFile <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As<\/mark> FolderItem = SpecialFolder.Desktop.Child(\"Xojo\").Child(\"TransactionsDB.sqlite\")<br>  App.db = <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">new<\/mark> SQLiteDatabase<br>  <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">if<\/mark> dbFile &lt;&gt; <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Nil<\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">And<\/mark> dbFile.Exists <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Then<\/mark>  <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/Use the existing database file<\/mark><br>    App.db.DatabaseFile = dbFile<br>    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Try <\/mark><br>      App.db.Connect<br>    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Catch<\/mark> error <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As<\/mark> DatabaseException<br>      MessageBox(\"Database not connected \"+error.Message)<br>      Quit<br>    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">End Try<\/mark><br>  <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Else<\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/Create a new database file<\/mark><br>    App.db.DatabaseFile = SpecialFolder.Desktop.Child(\"Xojo\").Child(\"TransactionsDB.sqlite\")<br>    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Try<\/mark><br>      App.db.CreateDatabase<br>    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Catch<\/mark> error <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As<\/mark> IOException<br>      MessageBox(\"Database file not available \" + error.Message)<br>      Quit<br>    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">End Try<\/mark><br>    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/Create a new table<\/mark><br>    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Var<\/mark> sql <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As String<\/mark> = \"CREATE TABLE Accounts (AccountID INTEGER NOT NULL, AccountName VARCHAR,\" _<br>    + \" AccountDeposit CURRENCY, AccountWithdraw CURRENCY, PRIMARY KEY (AccountID));\"<br>    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Try<\/mark><br>      App.db.ExecuteSQL(sql)<br>    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Catch<\/mark> error <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As<\/mark> DatabaseException<br>      MessageBox(\"Table creation error \" + error.Message)<br>    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">End Try<\/mark><br>  <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">End If<\/mark><br><\/pre>\n\n\n\n<p>Some instructions require some explanation:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Use an Existing database file<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Var<\/mark> dbFile <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As<\/mark> FolderItem = SpecialFolder.Desktop.Child(\"Xojo\").Child(\"TransactionsDB.sqlite\")<\/pre>\n\n\n\n<p>Above statement declares a variable <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">dbFile<\/mark> as an object (folder, file) of type <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">FolderItem<\/mark> that can be found in the file structure on your computer system. The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">FolderItem<\/mark> is searched from a <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">SpecialFolder<\/mark> with the foldername <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Desktop<\/mark> on your Operating System. The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Child<\/mark> method looks for a file or folder with the name <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Xojo<\/mark> in the folder <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Desktop<\/mark> and in the folder named <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Xojo<\/mark> the system tries to find the file named <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">TransactionsDB.sqlite<\/mark>.<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">SpecialFolder<\/mark> uses a starting point depending on the operating system in use. For example, the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Desktop<\/mark> starting point refers under <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Windows<\/mark> to the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">\\Users\\UserName\\Desktop<\/mark> directory, under <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">macOS<\/mark> to the folder <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">\/Users\/UserName\/Desktop<\/mark> and under <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Linux<\/mark> to <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">\/home\/UserName\/Desktop<\/mark>.<\/p>\n\n\n\n<p>If the file is found, the variable <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">dbFile<\/mark> refers to the found file. If the file is not found, the variable <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">dbFile<\/mark> is given the value <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Nil<\/mark>. This check is carried out by the instruction:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">If<\/mark> dbFile &lt;&gt; <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Nil And<\/mark> dbFile.Exists <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Then<\/mark> <\/pre>\n\n\n\n<p>The following piece of code initializes the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">db<\/mark> property declared in <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">App<\/mark>. <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">db<\/mark> is now an object from the class <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">SQLiteDatabase<\/mark>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\">App.db = <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">New<\/mark> SQLiteDatabase<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\">App.db.DatabaseFile = dbFile<\/pre>\n\n\n\n<p>The instruction above initializes the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DatabaseFile<\/mark> property of the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">db<\/mark> object to the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">FolderItem<\/mark> found on the file system being used. Now everything is ready to use the database. This is done by the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Connect<\/mark> method (see statement below) of  the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">db<\/mark> object:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\">App.db.Connect<\/pre>\n\n\n\n<p>The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Try\u2026Catch error As DatabaseException\u2026End Try<\/mark> construct checks if an error occurs during the execution of <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">App.db.Connect<\/mark>. If an error occurs then a message is displayed (&#8220;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Database not connected<\/mark>&#8220;) along with an error message (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">error.Message<\/mark>). Afterwards, the program is terminated (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Quit<\/mark>).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-yellow-background-color has-background\">To use an existing SQLiteDatabase, point a SQLiteDatabase object (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">db<\/mark>) to the database file on your file system (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">App.db.DatabaseFile =<\/mark> ) and connect to the database (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">App.db.Connect<\/mark>).<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Create a New Database file<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\">App.db.DatabaseFile = SpecialFolder.Desktop.Child(\"Xojo\").Child(\"TransactionsDB.sqlite\")<\/pre>\n\n\n\n<p>If the database file is not yet available (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Else<\/mark> branch of the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">If<\/mark> statement), the file must be created. In the first instruction, the desired file (name and location in the file system) is assigned to the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DatabaseFile<\/mark> property of the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">App.db<\/mark> object. Remember that the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DatabaseFile<\/mark> property expects a <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">FolderItem<\/mark> object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\">App.db.CreateDatabase<\/pre>\n\n\n\n<p>The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">CreateDatabase<\/mark> method of the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">App.db<\/mark> object then attempts to create the file specified in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DatabaseFile<\/mark> property. If this fails, an error message is displayed via the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Try\u2026Catch error as IOException\u2026End Try<\/mark> construct and the application terminates (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Quit<\/mark>).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-yellow-background-color has-background\">To create a SQLiteDatabase via Xojo code, initialize the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DatabaseFile<\/mark> property of an SQLiteDatabase object with a <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">FolderItem<\/mark> object that specifies the name and location of the new database file and use the method <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">CreateDatabase<\/mark> to create the file on the file system.<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Create a New table<\/h4>\n\n\n\n<p>A new table can be added to the database using a SQL command. This is the general SQL syntax to create a new table in a connected database:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-yellow-background-color has-background\">CREATE TABLE table_name(column_name1 datatype, column_name2 datatype...);<\/pre>\n\n\n\n<p>SQLite uses the internal data types TEXT, INTEGER, REAL, BLOB and NULL to store data in the database. If you use Xojo data types (INTEGER, DOUBLE, CURRENCY, BOOLEAN, STRING\u2026) in a SQL statement, SQLite will convert the Xojo datatype to one of their internal SQLite data types.<\/p>\n\n\n\n<p>Each <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">column_name<\/mark> can be completed with an additional constraint such as <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">PRIMARY KEY<\/mark> (to indicate the unique key of the table), <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">NOT NULL<\/mark> (if the column should not remain empty) or <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">UNIQUE<\/mark> (if the column value must be unique in the table).<\/p>\n\n\n\n<p>The data in an <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">INTEGER<\/mark> column defined as a <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">PRIMARY KEY<\/mark> contains a unique value. This value is automatically incremented by SQLite. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-yellow-background-color has-background\">An INTEGER column defined as PRIMARY KEY is automatically incremented by SQLite.<\/pre>\n\n\n\n<p>The following SQL statement can be used to create the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Accounts<\/mark> table:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\">CREATE TABLE Accounts(AccountID INTEGER PRIMARY KEY, AccountName VARCHAR, AccountDeposit CURRENCY, AccountWithdraw CURRENCY);<\/pre>\n\n\n\n<p>This SQL statement is stored in Xojo in a local String variable named <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">sql<\/mark> :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\">Var sql As String = \"CREATE TABLE Accounts (AccountID INTEGER PRIMARY KEY, AccountName VARCHAR,\" _\n+\" AccountDeposit CURRENCY, AccountWithdraw CURRENCY);\"<\/pre>\n\n\n\n<p>The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">ExecuteSQL<\/mark> method with the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">sql<\/mark> variable as a parameter can be used to execute this SQL statement:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\">App.db.ExecuteSQL(sql)<\/pre>\n\n\n\n<p>And of course, we also check via the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Try\u2026Catch\u2026End Try<\/mark> construct for possible errors while executing this SQL statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Insert a new row to a database table<\/h2>\n\n\n\n<p>The following piece of code shows the code in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Pressed<\/mark> event of the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">EnterButton<\/mark>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/Pressed event of the EnterButton<\/mark><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Var<\/mark> amount <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As<\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Currency<\/mark><br>amount = <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Currency<\/mark>.FromString(AmountField.Text,Locale.Current)<br><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">If<\/mark> amount &gt; 0.0 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Then<\/mark><br>  ActiveAccount.DepositMoney(amount)<br>  TransactionList.AddRow(AccountPopup.SelectedRowText,str(amount),\"\")<br>  TransactionList.SelectedRowIndex = TransactionList.LastAddedRowIndex<br>  AddDBRecord(AccountPopup.SelectedRowText,amount,0)<br>  ShowBalance<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Else<\/mark><br>  amount = ActiveAccount.WithdrawMoney(-amount)<br>  TransactionList.AddRow(AccountPopup.SelectedRowText,\"\",str(amount))<br>  TransactionList.SelectedRowIndex = TransactionList.LastAddedRowIndex<br>  AddDBRecord(AccountPopup.SelectedRowText,0,amount)<br>  ShowBalance<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">End If<\/mark><br><br>AmountField.Text = \"\"<\/pre>\n\n\n\n<p>The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Text<\/mark> property of a <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DesktopTextField<\/mark> control (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">AmountField<\/mark>) is a <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">String<\/mark>. To convert this <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">String<\/mark> to a <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Currency<\/mark> data type, the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">FromString<\/mark> method must be used. The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Locale.Current<\/mark> parameter tells the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">FromString<\/mark> method that the String may contain a number in the local format set by the Operating System installed (for example, a number such as 1.234,75).<\/p>\n\n\n\n<p>A positive <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">amount<\/mark> is considered to be a deposit, a negative amount is considered to be a cash withdrawal.<\/p>\n\n\n\n<p>A new row is added to the database with the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AddDBRecord<\/mark> method. The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AddDBRecord<\/mark> method can be developed with Xojo statements or can use a SQL command.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using Xojo statements<\/h3>\n\n\n\n<p>The method <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AddDBRecord<\/mark> is a new method defined in <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountWindow<\/mark>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"312\" src=\"https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-31-om-11.24.35-1024x312.png\" alt=\"AddDBRecord\" class=\"wp-image-500\" style=\"width:551px\" srcset=\"https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-31-om-11.24.35-1024x312.png 1024w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-31-om-11.24.35-300x91.png 300w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-31-om-11.24.35-768x234.png 768w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-31-om-11.24.35-1536x468.png 1536w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-31-om-11.24.35-2048x624.png 2048w, https:\/\/xojo.itbib4you.be\/wp-content\/uploads\/2025\/05\/Schermafbeelding-2025-05-31-om-11.24.35-1200x365.png 1200w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AddDBRecord<\/mark> method has 3 parameters: the account name (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">aName<\/mark> type STRING), the amount deposited (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">aDeposit<\/mark> type CURRENCY) and the amount withdrawn (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">aWithdraw<\/mark> type CURRENCY). This is the code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/AddDBRecord(aName As String, aDeposit As Currency, aWithdraw As Currency)<\/mark><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Var<\/mark> aRecord <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As New<\/mark> DatabaseRow <br><br>aRecord.Column(\"AccountName\").StringValue = aName<br>aRecord.Column(\"AccountDeposit\").CurrencyValue = aDeposit<br>aRecord.Column(\"AccountWithdraw\").CurrencyValue = aWithdraw<br><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Try<\/mark><br>  App.db.AddRow(\"Accounts\", aRecord)<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Catch<\/mark> error <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As<\/mark> DatabaseException<br>  MessageBox(\"DB error \" + error.Message)<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">End Try<br><\/mark><\/pre>\n\n\n\n<p>The row to be inserted (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">aRecord<\/mark>) must be defined as a new <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DatabaseRow<\/mark>. Our DatabaseRow has 3 columns named <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountName<\/mark>, <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountDeposit<\/mark> and <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountWithdraw<\/mark> and is populated with the parameters <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">aName<\/mark>, <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">aDeposit<\/mark> and <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">aWithdraw<\/mark>.<\/p>\n\n\n\n<p>The SQLiteDatabase<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AddRow<\/mark> method is used to insert a method to a database table. <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AddRow<\/mark> has two parameters: the name of the database table (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Accounts<\/mark>) and the database row to be added (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">aRecord<\/mark>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using SQL commands<\/h3>\n\n\n\n<p>The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AddDBRecord<\/mark> method can also be implemented with SQL statements. The SQL INSERT statement can be used to add a row to a table. The syntax is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-yellow-background-color has-background\">INSERT INTO table_name (column_name1, column_name2...) VALUES (value1, value2...);<\/pre>\n\n\n\n<p>The list of values can also be replaced by question marks (\u201c<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">?<\/mark>\u201d). In this case, the question marks should be replaced with actual values available during program execution. Using a parameter list with &#8220;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">?<\/mark>&#8221; is preferred to prevent <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">SQL injection attacks<\/mark>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-yellow-background-color has-background\">INSERT INTO table_name (column_name1, column_name2...) VALUES (?, ?...)<\/pre>\n\n\n\n<p>The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AddDBRecord <\/mark>method can be developed with a SQL command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/AddDBRecord(aName As String, aDeposit As Currency, aWithdraw As Currency)<\/mark><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Var<\/mark> sql <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As String<\/mark> = \"INSERT INTO Accounts (AccountName, AccountDeposit, AccountWithdraw) VALUES (?,?,?);\"<br><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Var<\/mark> myValues() As Variant<br>myValues.Add(aName)<br>myValues.Add(aDeposit)<br>myValues.Add(aWithdraw)<br><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Try<\/mark><br>  App.db.ExecuteSQL(sql, myValues)<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Catch<\/mark> error <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As<\/mark> DatabaseException<br>  MessageBox(\"DB error \"+error.Message)<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">End Try<\/mark><\/pre>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">myValues()<\/mark> is defined as an <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">array<\/mark>. The size of the array is not defined. To add an element to the array, use the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Add<\/mark> method. All elements of the array <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">myValues<\/mark> are of type <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Variant<\/mark>. A <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Variant<\/mark> data type is a special data type that can contain any type of data. As you notice from the example above, the first element (index 0) contains a <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">String<\/mark> and the second (index 1) and third (index 2) elements contain a <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Currency<\/mark> value. These are the data elements needed to replace the question marks in the SQL Insert statement.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\">App.db.ExecuteSQL(sql, myValues)<\/pre>\n\n\n\n<p>Use the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">ExecuteSQL<\/mark> method to execute the SQL statement defined in the string variable <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">sql<\/mark>. The first parameter of the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">ExecuteSQL<\/mark> method is the SQL command defined in a String variable (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">sql<\/mark>), the second parameter is the array to replace the question marks (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">myValues<\/mark>).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-yellow-background-color has-background\"> A <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Variant<\/mark> data type can contain all kind of data including arrays.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Reading data from a database table<\/h2>\n\n\n\n<p>Now all transactions are stored in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Accounts<\/mark> table in the database. When choosing an account, it would now be convenient to show all historical transactions, stored in the database, in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DesktopListBox<\/mark> (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">TransactionList<\/mark>). This can be accomplished via the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">SelectionChanged<\/mark> event from the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">AccountPopup<\/mark> control.<\/p>\n\n\n\n<p>The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">SelectionChanged<\/mark> event should carry out a number of activities:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Retrieve all historical data for the selected account from the database table (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Accounts<\/mark>);<\/li>\n\n\n\n<li>Enter the retrieved data into the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DesktopListBox<\/mark> (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">TransactionList<\/mark>);<\/li>\n\n\n\n<li>Recalculate the account balance based on the retrieved historical data;<\/li>\n\n\n\n<li>Initialize the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountBalance<\/mark> property with this calculated balance.<\/li>\n<\/ol>\n\n\n\n<p>The following code has been entered in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">SelectionChanged<\/mark> event of <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">AccountPopup<\/mark>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/SelectionChanged(item As DesktopMenuItem)<\/mark><br>ChangeActiveAccount(<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Me<\/mark>.SelectedRowText)<br><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Var<\/mark> rs <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As<\/mark> RowSet = ReadDBData <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/Retrieve data from database table<\/mark><br><br>PopulateListBox(rs) <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/Show retrieved data in DesktopListBox<\/mark><br><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Var<\/mark> balance <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As Currency<\/mark> = CalculateBalance  <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/Recalculate balance of the selected account<\/mark><br>ActiveAccount.UpdateBalance(balance)<br>ShowBalance<br><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">ChangeActiveAccount method<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\">ChangeActiveAccount(Me.SelectedRowText)<\/pre>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">ActiveAccount<\/mark> (type class <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Account<\/mark>) is a new Private property of <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountWindow<\/mark>. This property contains a reference to the object selected by choosing an item in <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">AccountPopup<\/mark>. From then on we can always use <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">ActiveAccount<\/mark> to refer to the properties and methods of the class <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Account<\/mark>.<\/p>\n\n\n\n<p>Since the code is part of the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">SelectionChanged<\/mark> event of the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">AccountPopup<\/mark>control, the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Me<\/mark> keyword refers to the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">AccountPopup<\/mark>control.<\/p>\n\n\n\n<p>The method <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">ChangeActiveAccount<\/mark> in <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountWindow<\/mark> contains the following statements:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/ChangeActiveAccount(selectedAccount As String)<\/mark><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Select Case<\/mark> selectedAccount<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Case<\/mark> \"CheckingAccount\"<br>  ActiveAccount = CheckingAccount<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Case<\/mark> \"SavingsAccount\"<br>  ActiveAccount = SavingsAccount<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Case<\/mark> \"RetirementAccount\"<br>  ActiveAccount = RetirementAccount<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">End <\/mark><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Retrieve historical data from the database<\/h3>\n\n\n\n<p>To retrieve the historical data from the database for the account selected in <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountPopup<\/mark>, we use a SQL SELECT command. The general syntax for the SQL SELECT command is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-yellow-background-color has-background\">SELECT * FROM table_name; <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/all columns from table_name are fetched<\/mark><br><br>SELECT column_name1, column_name2... FROM table_name; <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/only the named columns from table_name are fetched<\/mark><br><br>SELECT column_name1, column_name2... FROM table_name WHERE condition <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/only the named columns from table_name are fetched that meet the condition<\/mark><\/pre>\n\n\n\n<p>We only need to retrieve the columns <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountName<\/mark>, <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountDeposit<\/mark> and <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountWithdraw <\/mark> from the table <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Accounts<\/mark> where the column <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountName<\/mark> equals the item selected in <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountPopup<\/mark>. This is our SQL command required:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\">SELECT AccountName, AccountDeposit, AccountWithdraw FROM Accounts WHERE AccountName = AccountPopup.SelectedRowText<\/pre>\n\n\n\n<p>Executing the above SQL statement on the database table <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Accounts<\/mark> is contained in a separate method named <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">ReadDBData<\/mark> within <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountWindow<\/mark>.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/ReadDBData As RowSet<\/mark><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Var<\/mark> rs <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As<\/mark> RowSet<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Var<\/mark> sql <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As<\/mark> String = \"SELECT AccountName, AccountDeposit, AccountWithdraw  FROM Accounts WHERE AccountName =  ?;\" <br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Try<\/mark><br>  rs = App.db.SelectSQL(sql, AccountPopup.SelectedRowText)<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Catch<\/mark> error <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As<\/mark> DatabaseException<br>  Messagebox(\"DB Select error \"+error.Message)<br>  Quit<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">End Try<\/mark><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Return<\/mark> rs<\/pre>\n\n\n\n<p>A <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">RowSet<\/mark> contains database rows that are the result of a database query. The Xojo method <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">SelectSQL<\/mark> executes the SQL command (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">sql<\/mark>) and replaces the question mark  (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">?<\/mark>) in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">sql<\/mark> String with the selected item in <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountPopup<\/mark>. The selected rows are stored in a <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">RowSet<\/mark> named <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">rs<\/mark>. This <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">RowSet<\/mark> is returned to the calling method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Enter the RowSet data in a DesktopListBox<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\">PopulateListBox(rs)<\/pre>\n\n\n\n<p>To populate the retrieved <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">RowSet<\/mark> in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DesktopListBox<\/mark> (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">TransactionList<\/mark>) the following piece of code can be used:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/PopulateListBox(rs As RowSet)<\/mark><br>TransactionList.RemoveAllRows<br>TransactionList.ColumnCount = rs.ColumnCount<br><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">If<\/mark> rs &lt;&gt; <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Nil<\/mark> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">And<\/mark> rs.RowCount &lt;&gt;  0 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Then<\/mark><br>  <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">For Each<\/mark> row As DatabaseRow <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">In<\/mark> rs<br>    TransactionList.AddRow<br>    <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">For<\/mark> column <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As Integer<\/mark> = 0 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">To<\/mark> rs.ColumnCount - 1<br>      TransactionList.CellTextAt(TransactionList.LastAddedRowIndex, column) = row.ColumnAt(column).StringValue<br>    Next column <br>  Next <br>End If<\/pre>\n\n\n\n<p>Before populating the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DesktopListBox<\/mark> all rows are removed (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">TransactionList.RemoveAllRows<\/mark>)  and the number of columns in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DesktopListBox<\/mark> (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">TransactionList.ColumnCount<\/mark>) is set equal to the number of columns in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">RowSet<\/mark> (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">rs.ColumnCount<\/mark>).<\/p>\n\n\n\n<p>If the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">RowSet<\/mark> (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">rs<\/mark>) contains multiple rows (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">If rs &lt;&gt; Nil And rs.RowCount &lt;&gt; 0 Then<\/mark>), all the columns (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">For column As Integer = 0 To rs. ColumnCount &#8211; 1<\/mark>) of each row (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">For Each row As DatabaseRow In rs<\/mark>) are transferred to the newly added row (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">LastAddedRowIndex<\/mark>) of <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">TransactionList<\/mark> (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">TransactionList.CellTextAt(TransactionList.LastAddedRowIndex, column) = row.ColumnAt(column).StringValue<\/mark>). The parameters of the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">CellTextAt<\/mark> method refer to a row and a column in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DesktopListBox<\/mark>. The parameter in the RowSet <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">ColumnAt<\/mark> method refer to the index of a column in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">RowSet<\/mark>.<\/p>\n\n\n\n<p>The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">StringValue<\/mark> property attempts to convert all \u201cspecial\u201d values discovered in a column of the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">RowSet<\/mark> (for example, <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Currency<\/mark> data types) to a <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">String<\/mark>. A <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">String<\/mark> is accepted in a column of <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">TransactionList<\/mark>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Recalculate the balance<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\">Var balance As Currency = CalculateBalance<\/pre>\n\n\n\n<p>Now we can use <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">TransactionList<\/mark> to calculate the actual balance for the selected <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Account<\/mark>. For each row in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">DesktopListBox<\/mark>, we make the difference between the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Deposit<\/mark> value (column 1 in the ListBox) and the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Withdraw<\/mark> value (column 2 in the ListBox) and add the result to the local variable <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">balance<\/mark>. This value is returned to the calling method. We have to convert the DesktopListBox <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">String<\/mark> values to a <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Currency<\/mark> data type (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Currency.FromString<\/mark>).<\/p>\n\n\n\n<p>The following code was used to calculate the balance:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/CalculateBalance As Currency<\/mark><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Var<\/mark> balance <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As Currency<\/mark> = 0.0<br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">If<\/mark> TransactionList.RowCount &gt; 0 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Then<\/mark><br>  <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">For<\/mark> row <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">As<\/mark> Integer = 0 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">To<\/mark> TransactionList.LastAddedRowIndex<br>    balance = balance + <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Currency<\/mark>.FromString(TransactionList.CellTextAt(row,1)) -  <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Currency<\/mark>.FromString(TransactionList.CellTextAt(row,2))<br>  <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Next<\/mark><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">End If<\/mark><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">Return<\/mark> balance<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Update AccountBalance property<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/UpdateBalance(balance As Currency)<\/mark><br>ActiveAccount.UpdateBalance(balance)<\/pre>\n\n\n\n<p>To reuse our existing program, we need to update the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountBalance <\/mark>property with the calculated value (<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-blue-color\">balance<\/mark>). Because the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">AccountBalance<\/mark> property is private, we added a new public method to the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Account<\/mark> class called <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">UpdateBalance<\/mark>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/UpdateBalance(balance As Currency)<\/mark><br>AccountBalance = balance<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">ShowBalance method<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted has-light-gray-background-color has-background has-small-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">\/\/ShowBalance<\/mark><br>BalanceLabel.Text = \"The balance of the \" + AccountPopup.SelectedRowText + \" is: \" + ActiveAccount.GetBalance.ToString(Locale.Current)<\/pre>\n\n\n\n<p>In the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">ShowBalance<\/mark> method, we can use the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">ActiveAccount<\/mark> property to reference the active account in use. The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">ToString(Locale.Current)<\/mark> converts <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">Currency<\/mark> data (returned by <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">GetBalance<\/mark>) into a <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">String<\/mark> in the way data is displayed on your Operating System.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Some useful database methods<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-regular has-small-font-size\"><table class=\"has-fixed-layout\"><thead><tr><th>Method<\/th><th>Type<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>CreateDatabase<\/td><td><\/td><td>Creates and opens a new database defined in the property DatabaseFile<\/td><\/tr><tr><td>Connect<\/td><td><\/td><td>Connects to an existing  database<\/td><\/tr><tr><td>AddRow<\/td><td>table_name As String, row As DatabaseRow<\/td><td>inserts row in the table_name<\/td><\/tr><tr><td>ExecuteSQL<\/td><td>SQL command As String,<br>[parameters() As Variant]<\/td><td>Executes the SQL command (no return)<\/td><\/tr><tr><td>SelectSQL<\/td><td>SQL command As String,<br>[parameters() As Variant]<\/td><td>Selects data based on the SQL command and returns a RowSet<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Important SQLiteDatabase methods<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In the previous lessons, all data was stored in an object created in RAM. When the application was terminated, all transactions (objects) were deleted. This, of course, is not acceptable. Financial institutions should keep all historical financial records. In this lesson, our transaction data will be stored in a database on a disk. A &hellip; <a href=\"https:\/\/xojo.itbib4you.be\/index.php\/database-transactions\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Database Transactions&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-415","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/xojo.itbib4you.be\/index.php\/wp-json\/wp\/v2\/pages\/415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xojo.itbib4you.be\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/xojo.itbib4you.be\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/xojo.itbib4you.be\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xojo.itbib4you.be\/index.php\/wp-json\/wp\/v2\/comments?post=415"}],"version-history":[{"count":90,"href":"https:\/\/xojo.itbib4you.be\/index.php\/wp-json\/wp\/v2\/pages\/415\/revisions"}],"predecessor-version":[{"id":513,"href":"https:\/\/xojo.itbib4you.be\/index.php\/wp-json\/wp\/v2\/pages\/415\/revisions\/513"}],"wp:attachment":[{"href":"https:\/\/xojo.itbib4you.be\/index.php\/wp-json\/wp\/v2\/media?parent=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}