Small Basic Featured Program - Database Sample

I would like to share a database sample program. 

There are extension methods to handle databases using SQL, but I wanted to do something in standard Small Basic with no extensions.  The idea was to have a file based database that can be edited and sorted interactively in the GraphicsWndow.  I also wanted to write it in a general way so that it could be extended or modified as easily as possible.

This is the Small Basic forum thread, and the source code import is GJV656.  You will need to check and uncomment the FIle commands on lines 94, 127 and 132.

The program has several sections:


The subroutine Initialise() creates the GraphicsWindow, textboxes and buttons including the ascending/descending sort buttons for each column.

The main parameters controlling the program and layout are set first, with the columns defined by an array called header.

  ``'Basic data for columns and grid sizing

  `` header `` = ``"1=First Name;2=Surname;3=Telephone;4=Mobile;5=Email;"

  `` nCol `` = ``Array``.``GetItemCount``(``header``)

  `` nRow `` = ``10

  `` tbWidth `` = ``150

  `` tbHeight `` = ``26

  `` space `` = ``6

Register Events

Events for TextTyped and ButtonsClicked are then set.

Controls``.`` TextTyped `` = ``OnTextTyped

Controls``.`` ButtonClicked `` = ``OnButtonClicked

The event subroutines just set flags identifying the last textbox and last button pressed.  These flags are then processed in the main program loop.

Main Program Loop

This repeats infinitely, processing the user text entry and buttons pressed.

While ``(``"True"``)

  `` If ``(`` lastTB ``<`` > ``""`` ) ``Then


    `` lastTB `` = `` "" ``'Event handled


  `` If ``(`` lastButton ``<`` > ``""`` ) ``Then


    `` lastButton `` = `` "" ``'Event handled


  ``Program``.``Delay``(``20`` ) ``'Don't mash CPU but update faster than you can type



This is where subroutines that do the work are located.  They include:

  • Initialise - define the GraphicsWindow and row/column textbox display and buttons.
  • Update - put the current database data in the textboxes for the current page of data.
  • GetText - update the database data when a change is made to a textbox.
  • ButtonPressed - handle all button presses from the main loop, this includes load, save, sort, change page etc.
  • Unify - this is a utility to remove blank rows and sequentially index the rows in the database data array to the textbox display order.
  • Sort - shell sort (ascending or descinding) by column - this is a lexical or numerical sort depending on the data type.
  • Compare - a utility to compare two entries, by number or characters depending on which they are.

The database data is stored in a single 2D array data[i][j] , where i and j are a row and column.  The data can easily be saved and read from the settings file File.GetSettingsFilePath() using File.ReadContents and File.WriteContents.

When the data is displayed in the textboxes, the data row index i is used to display data in textbox row i.  Therefore the data indices must be carefully managed.  For example if a row is deleted, then all indices after this row should be decremented (see subroutine Unify that does this).

To show a different page an offset is applied and to change the row order we have to reorder the row indices in the data, for example following a sort.

The most complex part is the sorting, I used shell sort because it is fast, but bubble sort would be easier to follow.  The ascending and descending options took the most work, with a bit of trial and plenty of error along the way!

Event Subroutines

This is just the two event subroutines that set appropriate flags for use in the main loop.

Sub ``OnTextTyped

  `` lastTB `` = ``Controls``.``LastTypedTextBox



Sub ``OnButtonClicked

  `` lastButton `` = ``Controls``.``LastClickedButton


Next Steps

If you wanted to extend it some search methods would be nice, which would probably mean changing the row indexing alignment between the data array and textbox display.