Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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
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.
This repeats infinitely, processing the user text entry and buttons pressed.
While ``(``"True"``)
`` If ``(`` lastTB ``<`` > ``""`` ) ``Then
``GetText``(``)
`` lastTB `` = `` "" ``'Event handled
``EndIf
`` If ``(`` lastButton ``<`` > ``""`` ) ``Then
``ButtonPressed``(``)
`` lastButton `` = `` "" ``'Event handled
``EndIf
``Program``.``Delay``(``20`` ) ``'Don't mash CPU but update faster than you can type
EndWhile
This is where subroutines that do the work are located. They include:
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!
This is just the two event subroutines that set appropriate flags for use in the main loop.
Sub ``OnTextTyped
`` lastTB `` = ``Controls``.``LastTypedTextBox
EndSub
Sub ``OnButtonClicked
`` lastButton `` = ``Controls``.``LastClickedButton
EndSub
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.
Anonymous
October 15, 2015
Building databases with Small Basic! That's awesome!
Anonymous
October 16, 2015
Yay, great article...!!
Anonymous
December 08, 2015
I added this program to the Program Gallery:
blogs.msdn.com/.../small-basic-program-gallery-listed-by-category.aspx
Anonymous
October 20, 2016
Hi litdev,Where can I download this sample program from?Regards Rene
Anonymous
November 18, 2016
The comment has been removed
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in