I’m using LibreOffice Version: 5.2.6.2
I’ve made two cascading list boxes (Unit_Motive_ID and Unit_MUL_Role_ID) with the parent list box (Unit_Type_ID) in the mainform using the following macro:
Cascading called from “Unit_Type_ID” Item Status Changed
Sub Cascading_from_Unit_Type_ListBox (oEvent As Object)
oEvent.Source.Model.commit()
oForm = oEvent.Source.Model.Parent
sValue = oEvent.Source.Model.ValueItemList(oEvent.Selected)
sSQL = "SELECT ""Table_Unit_Motive_Category"".""Unit_Motive_Category"", ""Table_Unit_Motive"".""Unit_Motive_ID"" FROM ""Table_Unit_Motive_Category"", ""Table_Unit_Motive"" WHERE ""Table_Unit_Motive_Category"".""Unit_Motive_Category_ID"" = ""Table_Unit_Motive"".""Unit_Motive_Category_ID"" AND ""Table_Unit_Motive"".""Unit_Type_ID"" ='" & sValue & "'"
oListBox=oForm.GetByName("Unit_Motive_ID")
oListBox.ListSourceType=com.sun.star.form.ListSourceType.SQL
oListBox.ListSource=Array(sSQL)
oListBox.Refresh
oListBox.Enabled = 1
sSQL = "SELECT ""Table_Unit_MUL_Role_Category"".""Unit_MUL_Role_Category"", ""Table_Unit_MUL_Role"".""Unit_MUL_Role_ID"" FROM ""Table_Unit_MUL_Role_Category"", ""Table_Unit_MUL_Role"" WHERE ""Table_Unit_MUL_Role_Category"".""Unit_MUL_Role_Category_ID"" = ""Table_Unit_MUL_Role"".""Unit_MUL_Role_Category_ID"" AND ""Table_Unit_MUL_Role"".""Unit_Type_ID"" ='" & sValue & "'"
oListBox=oForm.GetByName("Unit_MUL_Role_ID")
oListBox.ListSourceType=com.sun.star.form.ListSourceType.SQL
oListBox.ListSource=Array(sSQL)
oListBox.Refresh
oListBox.Enabled = 1
End Sub
Refresh when record change called from “MainForm” After Record Change
sub AfterRecordChange_MainForm (oEvent as Object)
oForm = oEvent.source
oListBox = oForm.GetByName("Unit_Type_ID")
sValue = oListBox.ValueItemList(oListBox.SelectedItems(0))
if sValue <> "" then
sSQL = "SELECT ""Table_Unit_Motive_Category"".""Unit_Motive_Category"", ""Table_Unit_Motive"".""Unit_Motive_ID"" FROM ""Table_Unit_Motive_Category"", ""Table_Unit_Motive"" WHERE ""Table_Unit_Motive_Category"".""Unit_Motive_Category_ID"" = ""Table_Unit_Motive"".""Unit_Motive_Category_ID"" AND ""Table_Unit_Motive"".""Unit_Type_ID"" ='" & sValue & "'"
oListBox=oForm.GetByName("Unit_Motive_ID")
oListBox.ListSourceType=com.sun.star.form.ListSourceType.SQL
oListBox.ListSource=Array(sSQL)
oListBox.Refresh
oListBox.Enabled = 1
sSQL = "SELECT ""Table_Unit_MUL_Role_Category"".""Unit_MUL_Role_Category"", ""Table_Unit_MUL_Role"".""Unit_MUL_Role_ID"" FROM ""Table_Unit_MUL_Role_Category"", ""Table_Unit_MUL_Role"" WHERE ""Table_Unit_MUL_Role_Category"".""Unit_MUL_Role_Category_ID"" = ""Table_Unit_MUL_Role"".""Unit_MUL_Role_Category_ID"" AND ""Table_Unit_MUL_Role"".""Unit_Type_ID"" ='" & sValue & "'"
oListBox=oForm.GetByName("Unit_MUL_Role_ID")
oListBox.ListSourceType=com.sun.star.form.ListSourceType.SQL
oListBox.ListSource=Array(sSQL)
oListBox.Refresh
oListBox.Enabled = 1
end if
end sub
Next I wanted to add another cascading listbox (Planet_ID) with the parent list box (Manufacturer_ID) in a subform table control of the same mainform.
Cascading called from “Manufacturer_ID” Item Status Changed
Sub Cascading_from_Manufacturer_ListBox (oEvent As Object)
oEvent.Source.Model.commit()
oForm = oEvent.Source.Model.Parent
sValue = oEvent.Source.Model.ValueItemList(oEvent.Selected)
if sValue <> "" then
sSQL = "SELECT ""Table_Planet"".""Planet_Name"", ""Table_Planet"".""Planet_ID"" FROM ""Table_Planet"", ""Table_Manufacturer_Facility"" WHERE ""Table_Planet"".""Planet_ID"" = ""Table_Manufacturer_Facility"".""Planet_ID"" AND ""Table_Manufacturer_Facility"".""Manufacturer_ID"" = '" & sValue & "'"
oListBox=oForm.GetByName("Planet_ID")
oListBox.ListSourceType=com.sun.star.form.ListSourceType.SQL
oListBox.ListSource=Array(sSQL)
oListBox.Refresh
oListBox.Enabled = 1 'enable Listbox
end if
End Sub
Added new codes to refresh “Planet_ID” when record change called from “MainForm” After Record Change
sub AfterRecordChange_MainForm (oEvent as Object)
oForm = oEvent.source
oListBox = oForm.GetByName("Unit_Type_ID")
sValue = oListBox.ValueItemList(oListBox.SelectedItems(0))
if sValue <> "" then
sSQL = "SELECT ""Table_Unit_Motive_Category"".""Unit_Motive_Category"", ""Table_Unit_Motive"".""Unit_Motive_ID"" FROM ""Table_Unit_Motive_Category"", ""Table_Unit_Motive"" WHERE ""Table_Unit_Motive_Category"".""Unit_Motive_Category_ID"" = ""Table_Unit_Motive"".""Unit_Motive_Category_ID"" AND ""Table_Unit_Motive"".""Unit_Type_ID"" ='" & sValue & "'"
oListBox=oForm.GetByName("Unit_Motive_ID")
oListBox.ListSourceType=com.sun.star.form.ListSourceType.SQL
oListBox.ListSource=Array(sSQL)
oListBox.Refresh
oListBox.Enabled = 1 'enable Listbox
sSQL = "SELECT ""Table_Unit_MUL_Role_Category"".""Unit_MUL_Role_Category"", ""Table_Unit_MUL_Role"".""Unit_MUL_Role_ID"" FROM ""Table_Unit_MUL_Role_Category"", ""Table_Unit_MUL_Role"" WHERE ""Table_Unit_MUL_Role_Category"".""Unit_MUL_Role_Category_ID"" = ""Table_Unit_MUL_Role"".""Unit_MUL_Role_Category_ID"" AND ""Table_Unit_MUL_Role"".""Unit_Type_ID"" ='" & sValue & "'"
oListBox=oForm.GetByName("Unit_MUL_Role_ID")
oListBox.ListSourceType=com.sun.star.form.ListSourceType.SQL
oListBox.ListSource=Array(sSQL)
oListBox.Refresh
oListBox.Enabled = 1 'enable Listbox
end if
oListBox = oForm.GetByName("SubForm_Table_Unit_Manufacturer").GetByName("Table Control Table_Unit_Manufacturer").GetByName("Manufacturer_ID")
sValue = oListBox.ValueItemList(oListBox.SelectedItems(0))
if sValue <> "" then
sSQL = "SELECT ""Table_Planet"".""Planet_Name"", ""Table_Planet"".""Planet_ID"" FROM ""Table_Planet"", ""Table_Manufacturer_Facility"" WHERE ""Table_Planet"".""Planet_ID"" = ""Table_Manufacturer_Facility"".""Planet_ID"" AND ""Table_Manufacturer_Facility"".""Manufacturer_ID"" = '" & sValue & "'"
oListBox=oForm.GetByName("SubForm_Table_Unit_Manufacturer").GetByName("Table Control Table_Unit_Manufacturer").GetByName("Planet_ID")
oListBox.ListSourceType=com.sun.star.form.ListSourceType.SQL
oListBox.ListSource=Array(sSQL)
oListBox.Refresh
oListBox.Enabled = 1 'enable Listbox
end if
end sub
But I can’t get the refresh to work properly.
When I open the form, the first record’s “Planet_ID” will show the correct “Planet_Name”. But when I go to the next record that have another “Planet_ID” stored, the “Planet_ID” will be blank. And when I return back to the previous record, “Planet_ID” will also be blank.
Thinking that my SQL is wrong, I’ve tested it in the query and it was correct. Also back in the form, although the “Planet_ID” is blank, but when I press the little arrow button, the correct set of “Planet_ID” is shown correctly.
I’ve made a lot of modification and testing, but I can’t get it to work.
Can someone please help? Please Please Please Please Please…
Update 1:
I updated LibreOffice right before posting my original question. I noticed that the listbox content is behaving differently now. The content of the listbox is showing the value that should be displaying for my last mainform record. I suspect this is the real reason why nothing is showing up in “Planet_ID” since the ‘saved’ value doesn’t exist in the listbox content.
Update 2 (04/05/2017):
Update 3 (04/07/2017):
This is the structure for the form in question.
MainForm (Table Table_Unit)
ListBox Name/Field "Unit_Chassis_ID"
(SQL list content from Table_Unit_Chassis)
ListBox Name/Field "Unit_Type_ID"
(SQL list content from Table_Unit_Type)
ListBox Name/Field "Unit_Motive_ID"
(SQL list content from Table_Unit_Motive)
ListBox Name/Field "Unit_MUL_Role_ID"
(SQL list content from Table_Unit_MUL_Role)
SubForm_Table_Unit_Deployment_Civilian
(Table_Unit_Deployment_Civilian, Link Field "Unit_ID")
(Update/Delete Cascade)
Grid Name "Table Control Table_Unit_Deployment_Civilian"
(Table_Unit_Deployment_Civilian)
ListBox Name/Field "Unit_Deployment_Civilian_Category_ID"
(SQL list content from Table_Unit_Deployment_Civilian_Category)
SubForm_Table_Unit_Deployment_Military
(Table_Unit_Deployment_Military, Link Field "Unit_ID")
(Update/Delete Cascade)
Grid Name "Table Control Table_Unit_Deployment_Military"
(Table_Unit_Deployment_Military)
ListBox Name/Field "Unit_Deployment_Military_Category_ID"
(SQL list content from Table_Unit_Deployment_Military_Category)
SubForm_Table_Unit_Equipment
(Table_Unit_Equipment, Link Field "Unit_ID")
(Update/Delete Cascade)
Grid Name "Table Control Table_Unit_Equipment"
(Table_Unit_Equipment)
ListBox Name/Field "Equipment_ID"
(SQL list content from Table_Equipment)
ListBox Name "Unit_Equipment_Overall_Rating" Field "Unit_ID"
(SELECT MAX from Table_Unit_Equipment)
SubForm_Table_Unit_Manufacturer
(Table_Unit_Manufacturer, Link Field "Unit_ID")
(No relationship)
Grid Name "Table Control Table_Unit_Manufacturer"
ListBox Name/Field "Manufacturer_ID"
(SQL list content from Table_Manufacturer)
ListBox Name/Field "Planet_ID"
(SQL list content from Table_Planet)
ListBox Name/Field "Reference_Book_ID"
(SQL list content from Table_Reference_Book)
Table_Unit
Unit_ID (Primary Key, Integer)
Unit_Chassis_ID (Foreign Key, Integer)
Unit_Model (VarChar)
Unit_Weight (Decimal)
Unit_Type_ID (Foreign Key, Integer)
Unit_Motive_ID (Foreign Key, Integer)
Unit_MUL_Role_ID (Foreign Key, Integer)
Unit_Prototype_Year (SmallInt)
Unit_Production_Year (SmallInt)
Unit_Extinction_Year (SmallInt)
Table_Unit_Chassis
Unit_Chassis_ID (Primary Key, Integer)
Unit_Chassis (VarChar)
Table_Unit_Type
Unit_Type_ID (Primary Key, Integer)
Unit_Type (VarChar)
Table_Unit_MUL_Role
Unit_MUL_Role_ID (Primary Key, Integer)
Unit_Type_ID (Foreign Key, Integer)
Unit_MUL_Role_Category_ID (Foreign Key, Integer)
Table_Unit_Deployment_Civilian_Category
Unit_Deployment_Civilian_Category_ID (Primary Key, Integer)
Unit_Deployment_Civilian_Category (VarChar)
Table_Unit_Deployment_Military_Category
Unit_Deployment_Military_Category_ID (Primary Key, Integer)
Unit_Deployment_Military_Category (VarChar)
Table_Unit_Equipment
Unit_Equipment_ID (Primary Key, Integer)
Unit_ID (Foreign Key, Integer)
Equipment_ID (Foreign Key, Integer)
Table_Equipment
Equipment_ID (Primary Key, Integer)
Equipment_Name (VarChar)
Equipment_Role (VarChar)
Equipment_Technology_Base (VarChar)
Equipment_Technology_Rating (VarChar)
Equipment_Availability_Rating_1 (VarChar)
Equipment_Availability_Rating_2 (VarChar)
Equipment_Availability_Rating_3 (VarChar)
Equipment_Availability_Rating_4 (VarChar)
Equipment_Prototype_Year (Integer)
Equipment_Production_Year (Integer)
Equipment_Common_Year (Integer)
Equipment_Extinction_Year (Integer)
Equipment_Return_Year (Integer)
Equipment_Book_Reference (VarChar)
Equipment_Reference_Book_Page (VarChar)
Table_Unit_Manufacturer
Unit_Manufacturer_ID (Primary Key, Integer)
Unit_ID (Foreign Key, Integer)
Manufacturer_ID (Foreign Key, Integer)
Planet_ID (Foreign Key, Integer)
Reference_Book_ID (Foreign Key, Integer)
Unit_Manufacturer_Reference_Book_Page (VarChar)
Table_Manufacturer
Manufacturer_ID (Primary Key, Integer)
Manufacturer_Name (VarChar)
Manufacturer_General_Inner_Sphere (Boolean)
Manufacturer_General_Periphery (Boolean)
Manufacturer_General_Mercenary (Boolean)
Manufacturer_General_Home_World_Clan (Boolean)
Manufacturer_General_Inner_Sphere_Clan (Boolean)
Manufacturer_General_All (Boolean)
Manufacturer_General_Faction (Boolean)
Table_Planet
Planet_ID (Primary Key, Integer)
Planet_Name (VarChar)
Planet_Remark (LongVarChar)
Table_Reference_Book
Reference_Book_ID (Primary Key, Integer)
Reference_Book_Code (VarChar)
Reference_Book_Name (VarChar)
Update 4 (04/11/2017):
odb file can be downloaded here: