User Tools

Site Tools


Sidebar

Databases

k22:k22.5:k22.5.3:start

22.5.3 Class DataSource (gb.db.form)

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"

22.5.3.1 Properties

The DataSource class has the following properties:

PropertyData typeDescription
ConnectionConnectionSets the connection (DB connection) to be used by the DataSource. If no DB connection is specified, the default connection is used.
CountIntegerThe number of data records is read; in relation to the selected DB table in the specified database.
CurrentVariantAll primary keys of the current data set are stored in a variant array.
FilterStringSets 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.
IgnoreParentBooleanIf 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.
IndexIntegerReturns the index of the current data set.
ModifiedBooleanReturns True if the current data set has been modified by a control in the 'DataSource' container.
ReadOnlyBooleanIf the property has the value True, the DB data cannot be changed. You can also read the value of the property.
SortStringSets 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.
TableStringSets 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

22.5.3.2 Methods

The DataSource class has these special methods:

MethodReturn typeDescription
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 ( ) BooleanMoves 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 ( )BooleanMoves 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 ( )BooleanMoves 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 ( )BooleanMoves 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 )BooleanMoves 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 ()BooleanDeletes 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 ] )BooleanWrites 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

22.5.3.3 Events

The DataSource class only has three special events:

  • BeforeSave ( Data As Result ): This event is triggered before the values from the DataControls used are saved to the DB table, provided there is a connection to it. The data is saved in the result object (Result data type). Use the event to change data before it is written to the database table.
  • BeforeDelete ( Keys As Variant[] ): This event is triggered before a record is deleted. Keys is an array with the values of the primary key that identifies the data record to be deleted. You can cancel the deletion by stopping the event.
  • Change ( ): The event is triggered when the current data record has been changed and all controls in the container have been updated.

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

22.5.3.4 Examples and additions

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.

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k22/k22.5/k22.5.3/start.txt · Last modified: 07.05.2024 by emma

Page Tools