What event is triggered when I click on a base form? (solved)

I would like to use a macro to auto-filter a base form when I click the mouse in the form. eg click on ‘X’ in column ‘code’ and the form will auto-filter to select all records with ‘X’ in ‘Code’. This makes it possible for less skilled operators to work the form. My question is whether an event is triggered when I click on the form and if so what event? Without that, I don’t think it can be done.

Edit: I have realised that there is an event associated with the grid, while I was looking for a column event. Can the grid event track down the column and row?

Hello,

Attach the following:

Sub	GetGrid(oEvent)
	Dim oForm As Object
	Dim oColumn As Object
	Dim sValue as String
	Dim sFilter as String
	Dim sColumnName as String
	Dim iColumn as Integer
    If oEvent.Buttons = 1 and oEvent.Modifiers = 2 Then
        oForm = ThisComponent.Drawpage.Forms.getByName("YOUR_FORM_NAME")
        iColumn = oEvent.Source.CurrentColumnPosition
        oColumn = oForm.Columns.getByIndex(iColumn)
        sColumnName = oColumn.Name
        sFilter = "(" & sColumnName & " = '" & oColumn.String & "')"
        oForm.Filter = sFilter
        oForm.reload()
    End If
End Sub

to the Mouse button released event of the grid. To activate, hold down Ctrl key when left mouse clicking a cell in the grid. The selected value will be used for the filtering.

To clear the filtering, place a button on the form attached to this macro:

Sub ClearFilter
	Dim oForm As Object
	oForm = ThisComponent.Drawpage.Forms.getByName("YOUR_FORM_NAME")
   	oForm.Filter = ""
   	oForm.reload()
End Sub

Edit:

Please note that this wasn’t tested against all field types. Has worked for integer, varchar, decimal, and boolean fields. The Ctrl key will not work if the column is a list box ( combo box seems to work). If you want to use the list box possibly change from Ctrl to Alt key (modifier = 4).

I understand you wanting this for those users not able to cope with the normal filtering, and the added key is more than just a click but just clicking will be a problem anytime a cell is selected.

Brilliant as usual, Ratslinger. Thank you. And you’re quite right about preferring Ctrl-click.
I had to make a couple of mods. I had to change to oForm = oEvent.Source,Model.Parent and I had to add an apparently arbitrary 11 to iColumn. The reason for the latter is that not all the fields in the underlying query are displayed, and while CurrentPosition returns the position in the table, getByIndex looks up the query. I’m just saying this in case anyone else is looking for it.

[updated below]

@Pansmanser, this isn’t an answer, but a comment to help clarify what you want to do.

How does what you want to do relate to the already existing filter feature, that to me, appears to do the same thing?

For example, start with this table (you may need to zoom your browser in to see it more clearly):

In it under the Major Category click on ‘Home’ on record with ID of 3 to select it.

Next click on this icon image description which sets up a filter so that now the only records shown are those with Major Category = Home.

Undo it with image description


Ok, then by going to Menu | Customization I was able to drill down to this to find the name of the function is AutoFilter. Now the next step is to figure out how to call it with a macro.

But… I don’t know how to do that. Am searching…

Ok, can’t find how to do that.

So, instead need to be able to get the value of the current field, then put that into an appropriate filter.

Do you know how to do that already? It’s a matter of starting MRI when the field is clicked, then drilling down to find the text in the field, then using that to create the sql to put into the form’s (table’s) filter.

This somewhat depends on what type of control you want to be able to click on. Simple text boxes are fairly easy, pull down boxes (combo or list) are harder.

Thanks, EasyTrieve, but I want to do just that but a single click with a macro.

@EasyTrieve
Yes you could do exactly this with the autofilter. You can execute autofilter from a macro also, but not by calling a function.

Instead you dispatch an event into the application event handler named .uno.Autofilter.

This literally makes the application respond as if the user clicked on the autofilter button.

@drewjensen +1, cool, thanks.