A DataSource is a container control that provides data from a database (recursively) to all controls in this container. The database is defined via the Connection property.
You can use the Table and Filter properties to specify which database data should actually be available, for example for display:
DataSource1.Connection = DBCS.DBConnection '-- Filtering -------------------------------------------------------------------- DataSource1.Table = "contacts" DataSource1.Filter = "id > 4 AND wohnort <> 'Berlin' AND wohnort NOT LIKE 'G%'" '----------------------------------------------------------------------------------- DataSource1.Sort = "wohnort"
The DataSource class has the following properties:
Property | Data type | Description |
---|---|---|
Connection | Connection | Sets the connection (DB connection) to be used by the DataSource. If no DB connection is specified, the default connection is used. |
Count | Integer | The number of data records is read; in relation to the selected DB table in the specified database. |
Current | Variant | All primary keys of the current data set are stored in a variant array. |
Filter | String | Sets a filter for the container - similar to a WHERE clause in an SQL statement - as an optional property to provide all controls in the container with data from the database already filtered. The default value for the filter property is an empty string. The data is then not filtered. You can also read out the value of the property. |
IgnoreParent | Boolean | If this property is set to TRUE, the DataSource does not filter its content with the primary keys of its parent DataSources. It will behave as if it were a root DataSource. |
Index | Integer | Returns the index of the current data set. |
Modified | Boolean | Returns True if the current data set has been modified by a control in the 'DataSource' container. |
ReadOnly | Boolean | If the property has the value True, the DB data cannot be changed. You can also read the value of the property. |
Sort | String | Sets the DB table field to be sorted by. If the property is not set, the DB data is not sorted (default). You can also read the value of the property. |
Table | String | Sets the name of the DB table in the selected database that is to be used. You can also read the value of the property. |
Table 22.5.3.1.1 : Properties of the DataSource class
The DataSource class has these special methods:
Method | Return type | Description |
---|---|---|
Cancel | - | Cancels the current change to the child controls and resets all changes in the child controls. |
Create ( [ bUpdate As Boolean ] ) | - | Saves data that is in the DataControls and then deletes their content. If the optional parameter 'bUpdate' has the value False, the data is not transferred immediately - until the refresh method is used. |
MoveFirst ( ) | Boolean | Moves the current record pointer to the first record and returns False if the move was possible. If there is no data record in the selected DB table or if the current data record has been changed, the move is canceled and TRUE is returned. |
MoveLast ( ) | Boolean | Moves the current record pointer to the last record and returns False if the move was possible. If there is no data record in the selected DB table or if the current data record has been changed, the move is canceled and TRUE is returned. |
MoveNext ( ) | Boolean | Moves the current record pointer to the next record and returns False if the move was possible. If there is no data record in the selected DB table or if the current data record has been changed, the move is canceled and TRUE is returned. |
MovePrevious ( ) | Boolean | Moves the current record pointer to the previous record and returns False if the move was possible. If there is no data record in the selected DB table or if the current data record has been changed, the move is canceled and TRUE is returned. |
MoveTo ( Index As Integer ) | Boolean | Moves the current record pointer to the record with the specified index and returns False if the move was possible. If there is no data record in the selected DB table or if the current data record has been changed, the move is canceled and TRUE is returned |
Remove () | Boolean | Deletes the current data record from the DB table and returns False if the deletion was successful, otherwise True. |
Reset () | - | Resets the internal connection metadata cache used by DataSource. |
Refresh | - | Redraws all controls in the container. The redraw is delayed and only processed the next time the event loop is called. If you require an immediate update, call WAIT after using this method. |
ResetAll () | - | Resets the internal connection metadata cache completely. |
Save ( [ bMessage As Boolean ] ) | Boolean | Writes the values from the DataControls used to the DB table, provided there is a connection to it. If the saving was successful, the function returns True, otherwise False. |
Update () | - | Reloads the datasource data and updates all controls in the container accordingly. |
Table 22.5.3.2.1 : Methods of the DataSource class
The DataSource class only has three special events:
Example 1 - Event: BeforeSave ( Data As Result )
The selected data set can still be changed before saving. The two potential changes shown here are only intended to demonstrate the procedure:
Public Sub DataSource1_BeforeSave(Data As Result) '-- If the date is missing, the current date is saved. If Data["date"] = Null Then Data["date"] = Now() '-- If the image is missing, you will be prompted to save an image. If Data["picture"].Length = 0 Then Message.Info("Action:<br>Insert new image.") Else If Message.Question("Do you want to change the current image?", "Yes", "No") = 1 Then Print "Action: Change current image." Else Print "Action: No change current image." Stop Event Endif Endif Catch Message.Error(Error.Text & gb.NewLine & Error.Where) End
Example 2 - Event: BeforeDelete ( Keys As Variant[ ] )
The focus here is on the confirmation prompt as to whether the selected data record should actually be deleted:
Public Sub DataSource1_BeforeDelete(Keys As Variant[]) Dim vElement As Variant Dim sQuestion As String '-- For Each vElement In Keys '-- Print vElement '-- Next sQuestion = Subst("&1 `&2` &3", ("Should the record with the ID"), Keys[0], ("be deleted?")) If Message.Question(sQuestion, "Yes", "No") = 2 Then Stop Event Endif End
Example 3 - Deleting the current data record from the DB table:
Dim bFlag As Boolean If DataSource1.Remove() = False Then Print "THE DELETION OF THE DATA SET WAS SUCCESSFUL!"
Example 4 - Setting the DB table field to be sorted by:
DataSource1.Sort = "PLZ"
Example 5 - Setting a filter:
MDataBase.ConnectDB("mysql", "localhost", "3606", "root", "sql", "Kontakte", "kontakte") DataSource1.Connection = MDataBase.hConnection DataSource1.Table = "kontakte" '-- DataSource1.Filter = "Wohnort <> 'Leipzig' AND Wohnort NOT LIKE 'G%'" ' "Wohnort <> \"Leipzig\"" DataSource1.Filter = "" '-- Standard-Filter
Note: In MySQL, the patterns are not case-sensitive by default.
Additions
In commit https://gitlab.com/gambas/gambas/commit/6902893dd50e6480a63aa2268207241aea021f3 the new property “IgnoreParent” has been added to the DataSource control. If this property is set to TRUE, the DataSource will not filter its content with the primary keys of its parent DataSources. It will behave as if it were a root DataSource.