FilterData Event

Fired after a value is read and before a value is written to a recordset to allow custom formatting.

Syntax

Private Sub VSFlexGrid_FilterData( ByVal Row As Long,  ByVal Col As Long, Value As String,  ByVal SavingToDB As Boolean, WantThisCol As Boolean)

Remarks

This event is fired whenever data is read from or written to a bound recordset. It allows you to modify the data before it is committed to the grid (when reading) or to the recordset (when writing).

This event is mostly useful when the data is stored in the database using an encoded format that is not ideal for displaying and editing. For example, many databases store dates as a string of digits (usually 6 or 8), without any separators. You could use the FilterData event to insert the separators at the proper places before displaying the data, and to remove them again before writing them back into the database.

The parameters for the FilterData event are described below:

Row As Long, Col As Long

Address of the cell whose value is about to be read from or written to the bound recordset.

Value As String

Value just read from or about to be written to the bound recordset.

SavingToDB As Boolean

If True, the value was read from the grid and is about to be written to the bound recordset. If False, the value was read from the bound recordset and is about to be written to the grid.

WantThisCol As Boolean

This value is set to False by default. If you set it to True, the control will keep firing the FilterData event for this column. If you don't set it to True, the event will no longer be fired for this column until the grid is bound to a new recordset or the current recordset is refreshed. The WantThisCol parameter is important because the FilterData event is relatively slow. The WantThisCol parameter allows the application to establish which columns need filtering, thus improving performance.

For example, suppose you are dealing with a recordset that contains two date fields called Birth and Hired. These fields contain dates encoded as six-digit strings (for example, July 4th, 1962 is encoded as "070462") which you would like to display in the "Medium Date" format. The code below shows how you can accomplish this using the FilterData event:

Private Sub fg_FilterData(ByVal Row As Long, ByVal Col As Long, Data As String, ByVal SavingToDB As Boolean, WantThisCol As Boolean)

 

   ' we are interested only in "Birth" and "Hired" fields

   If fg.ColKey(Col) <> "Birth" And fg.ColKey(Col) <> "Hired" Then Exit Sub

   

   ' keep getting this column

   WantThisCol = True

       

    ' format data going out to the recordset (Date -> "mmddyy")

        Dim dt As Date

        If SavingToDB Then

            dt = Data

            Data = Format(dt, "mmddyy")

           

        ' format data coming in from the recordset ("mmddyy" -> Date)

        Else

            If Len(Data) = 6 Then

                dt = DateSerial(Right(Data, 2), Left(Data, 2), Mid(Data, 3, 2))

                Data = Format(dt, "Medium Date")

            End If

        End If

End Sub

Note

Be careful when writing FilterData code to avoid corrupting your database. The FilterData code should perform symmetrical translations on the data. In other words, if value X gets translated into value Y when it is read from the database, then value Y should be translated back into X when it is written out to the database.

See Also

VSFlexGrid Control