LibreOffice logo
BASEDOCUMENTER
The software tool for documenting your LibreOffice Base applications
 
Database file/home/jean-pierre/Documents/BaseDocumenter/www/databases/LODoc/Example_Search_and_Filter.odb
File actual save date2018-06-27 13:59:23
Scanning done on2018-08-21 17:39:10
Documentation generated on2018-08-21 17:39:20
Table of contents
Example_Search_and_Filter
Procedures by module
Library Module name Procedure name Language Used by Number of code lines Procedure code
Standard Module1 Filter Basic filter_with_macros_1|listbox1 (Control)
filter_with_macros_1|listbox2 (Control)
22
SUB Filter
REM This procedure will replace the "Save"-button in the form.
REM Used in form "filter_with_macros_1"
DIM oDoc AS OBJECT
DIM oDrawpage AS OBJECT
DIM oForm1 AS OBJECT
DIM oForm2 AS OBJECT
DIM oFieldList1 AS OBJECT
DIM oFieldList2 AS OBJECT
oDoc=thisComponent
oDrawpage=oDoc.drawpage
oForm1=oDrawpage.forms.getByName("filter")
oForm2=oDrawpage.forms.getByName("display")
oFieldList1=oForm1.getByName("listbox1")
oFieldList2=oForm1.getByName("listbox2")
oFieldList1.commit()
oFieldList2.commit()
oForm1.updateRow()
oFieldList1.refresh()
oFieldList2.refresh()
oForm2.reload()
END SUB
Standard Module1 Filter_Additional_Info Basic filter_with_macros_2|listbox1 (Control)
filter_with_macros_2|listbox2 (Control)
25
SUB Filter_Additional_Info(oEvent AS OBJECT)
REM The variables for the array will be written down in the tag of the listboxes
REM First part of the tag is the name of the own listbox, the other parts are of all other listboxes related to this listbox.
REM Used in form "filter_with_macros_2"
DIM oDoc AS OBJECT
DIM oDrawpage AS OBJECT
DIM oForm1 AS OBJECT
DIM oForm2 AS OBJECT
DIM stTag AS String
stTag = oEvent.Source.Model.Tag
arList() = Split(stTag, ",") 'Array would be established and filled with names of the listboxes
oDoc=thisComponent
oDrawpage=oDoc.drawpage
oForm1=oDrawpage.forms.getByName("filter")
oForm2=oDrawpage.forms.getByName("display")
FOR i = LBound(arList()) TO UBound(arList())
IF i = 0 THEN
oForm1.getByName(arList(i)).commit()
oForm1.updateRow()
ELSE
oForm1.getByName(arList(i)).refresh()
END IF
NEXT
oForm2.reload()
END SUB
Standard Module1 Filter_direct Basic searchtable_macrofilter_form_directly_filtered|listbox1 (Control) 15
SUB Filter_direct(oEvent AS OBJECT)
REM Filter of the form is dircetly set
REM Filter could be switched on and off in the navigationbar
DIM oForm AS OBJECT
DIM oField AS OBJECT
oField = oEvent.Source.Model
oForm = oField.Parent
stListValue = oField.getCurrentValue()
IF stListValue = "" THEN
oForm.Filter = ""
ELSE
oForm.Filter = "Autor = '" + stListValue + "'"
END IF
oForm.reload()
END SUB
Standard Module1 Hierarchical_Control Basic hierarchical_macrofilter|listbox4 (Control)
hierarchical_macrofilter|listbox3 (Control)
hierarchical_macrofilter|listbox2 (Control)
hierarchical_macrofilter|listbox1 (Control)
searchtable_hierarchical_macrofilter_form_directly_filtered|listbox4 (Control)
searchtable_hierarchical_macrofilter_form_directly_filtered|listbox3 (Control)
searchtable_hierarchical_macrofilter_form_directly_filtered|listbox2 (Control)
searchtable_hierarchical_macrofilter_form_directly_filtered|listbox1 (Control)
89
SUB Hierarchical_Control(oEvent AS OBJECT)
REM Listboxes will work hierachically when content of datasource is the same.
DIM oDoc AS OBJECT
DIM oDrawpage AS OBJECT
DIM oForm AS OBJECT
DIM oFieldHidden AS OBJECT
DIM oField AS OBJECT
DIM oField1 AS OBJECT
DIM oField2 AS OBJECT
DIM stSql AS STRING
DIM arContent()
DIM stTag AS STRING
oField = oEvent.Source.Model
stTag = oField.Tag
oForm = oField.Parent
REM Tag of the listboxes
REM Entries:
REM 0. Fieldname of the column in the table, which should be filtered,
REM 1. Fieldname of hidden control, which should save the filter,
REM 2. next listbox, which should be filtered
REM Tag would be written from the listbox, which is the source for this event.
aFilter() = Split(stTag, ",")
stFilter = ""
IF Trim(aFilter(1)) = "" THEN
IF oField.getCurrentValue <>"" THEN
stFilter = """"+Trim(aFilter(0))+"""='"+oField.getCurrentValue()+"'"
REM Filter will be written in the form. Could be enabled or disabled by the filterbuttons of the navigationbar.
REM If there is an existing filter, the filter will be added to this existing filter.
IF oForm.Filter <> "" AND InStr(oForm.Filter, """"+Trim(aFilter(0))+"""='") = 0 THEN
stFilter = oForm.Filter + " AND " + stFilter
REM If there is a filter for the same column, filter would be written new beginning with this column.
ELSEIF oForm.Filter <> "" THEN
stFilter = Left(oForm.Filter, InStr(oForm.Filter, """"+Trim(aFilter(0))+"""='")-1) + stFilter
END IF
END IF
oForm.Filter = stFilter
oForm.reload()
ELSE
oFieldHidden = oForm.getByName(Trim(aFilter(1)))
REM Columnname of the table will be combined with the value of the filter selected by the listbox
IF oField.getCurrentValue <>"" THEN
stFilter = """"+Trim(aFilter(0))+"""='"+oField.getCurrentValue()+"'"
REM If there is an existing filter, the filter will be added to this existing filter.
IF oFieldHidden.HiddenValue <> "" AND InStr(oFieldHidden.HiddenValue, """"+Trim(aFilter(0))+"""='") = 0 THEN
stFilter = oFieldHidden.HiddenValue + " AND " + stFilter
REM If there is a filter for the same column, filter would be written new beginning with this column.
ELSEIF oFieldHidden.HiddenValue <> "" THEN
stFilter = Left(oFieldHidden.HiddenValue, InStr(oFieldHidden.HiddenValue, """"+Trim(aFilter(0))+"""='")-1) + stFilter
END IF
END IF
oFieldHidden.HiddenValue = stFilter
END IF
REM If there are 3 entries in the tag there must be filtered another listbox
IF UBound(aFilter()) > 1 THEN
oField1 = oForm.getByName(Trim(aFilter(2)))
aFilter1() = Split(oField1.Tag,",")
IF oField.getCurrentValue <>"" THEN
REM Following listbox will be filtered by the same filter as the form.
REM The fields will be written and the content will be submitted directly to the listbox.
stSql = "SELECT DISTINCT """+Trim(aFilter1(0))+""" FROM """+oForm.Command+""" WHERE "+stFilter+" ORDER BY """+Trim(aFilter1(0))+""""
oDatasource = ThisComponent.Parent.CurrentController
If NOT (oDatasource.isConnected()) THEN
oDatasource.connect()
END IF
oConnection = oDatasource.ActiveConnection()
oSQL_Command = oConnection.createStatement()
oResult = oSQL_Command.executeQuery(stSql)
inCounter = 0 'Counter for array the values of the additional table will be saved
WHILE oResult.next
ReDim Preserve arContent(inCounter) 'Dimensioning of array with saving the former content.
arContent(inCounter) = oResult.getString(1) 'Reading first field
inCounter = inCounter+1 'Add 1 to the counter for redimensioning the array
WEND
ELSE
arContent(0) = ""
END IF
oField1.StringItemList = arContent() 'Content will be written to the listbox
oField1.refresh() 'Listbox must be refreshed
REM All following hierachical listboxes must be cleared
WHILE UBound(aFilter1()) > 1
DIM aEmpty()
oField2 = oForm.getByName(Trim(aFilter1(2)))
DIM aFilter1()
aFilter1() = Split(oField2.Tag,",")
oField2.StringItemList = aEmpty() 'Listbox will be cleared
oField2.refresh() 'Listbox must be refreshed
WEND
END IF
END SUB
Standard Module1 Searching Basic searchstart (Procedure) 43
SUB Searching(stTable AS STRING)
REM Saving value from additional forms
REM Table "searchtmp" has only a primarykey (AutoValue) and a column "Nr.",
REM where the founded primarykeys of stTable ("searchtable") would be saved.
REM First look for the form, read the value from the field
oDoc=thisComponent
oDrawpage=oDoc.drawpage
oForm=oDrawpage.forms.getByName("searchform")
oField=oForm.getByName("searchtext")
stContent=oField.getCurrentValue()
stContent=LCase(stContent)
REM Connection to database should be established
oDatasource = ThisComponent.Parent.DataSource
oConnection = oDatasource.GetConnection("","")
oSQL_Command = oConnection.createStatement()
IF stContent <> "" THEN
REM All columns of the table will be read out automatically
stSql = "SELECT ""COLUMN_NAME"" FROM ""INFORMATION_SCHEMA"".""SYSTEM_COLUMNS"" WHERE ""TABLE_NAME"" = '" + stTable + "' ORDER BY ""ORDINAL_POSITION"""
oResult = oSQL_Command.executeQuery(stSql)
inI=0 'Counter for array the values of the additional table will be saved
WHILE oResult.next
ReDim Preserve arContent(inI) 'Dimensioning of array with saving the former content.
arContent(inI)=oResult.getString(1) 'Reading first field
inI=inI+1 'Add 1 to the counter for redimensioning the array
WEND
stSql = "DROP TABLE ""searchtmp"" IF EXISTS"
oSQL_Command.executeUpdate (stSql)
stSql = "SELECT """+arContent(0)+"""INTO ""searchtmp"" FROM """+stTable+""" WHERE "
FOR inK = 0 TO (inI-1)
stSql = stSql+"LCase("""+arContent(inK)+""") LIKE '%"+stContent+"%'" ' stContent has been set to lower case - so the content of searchtabel should also be set to lowercase
IF inK < (inI-1) THEN
stSql = stSql+" OR "
END IF
NEXT
oSQL_Command.executeUpdate(stSql)
ELSE
stSql = "DELETE FROM ""searchtmp"""
oSQL_Command.executeUpdate (stSql)
END IF
REM For for display the result of the query must be reload
oForm2=oDrawpage.forms.getByName("display")
oForm2.reload()
End Sub
Standard Module1 searchstart Basic searchquery|btnSearch (Control) 5
SUB Searchstart
REM Name of the table, which sould be searched through, here: "searchtable".
REM If there isn't only one table the content of the tables must be joined in a view.
Searching("searchtable")
END SUB