How do you get a ListBox changed value to create a SQL Query where the results will populate a second ListBox? After much trial and error I came up with this - and it doesn’t require a complicated Sub Form or Filter Table either.
Solution:
Set the Default Value of the Data Field bound to ListBox1 to a value for the SQL Query parameter (it will get updated when you change ListBox1 and is needed for a New Record as a null value will cause the SQL Query to fail.
Set ListBox2 to ValueList with some string like ‘Default’ in Form Control settings or Form Load will fail with null value error
Trigger the Macro in the Form ‘Record Change’ (to populate ListBox2 on Form Load) and also in the ListBox1 ‘Status Change’ to Update ListBox2 with different values based on a ListBox1 value.
Sub SetDAORecipName
oForm = ThisComponent.Drawpage.Forms.getByName("MainForm")
oField = oForm.GetByName("dao-name") '<- ListBox1 name
oField.commit() '<- Force Data Field Update bound to ListBox1 after Change
oField.refresh()
oField1 = oForm.getByName("fmtdaoid")
tVal = oField1.Text '<- Value from ListBox1 Bound Data Field to modify SQL Query
oField2 = oForm.getByName("Recip-Name") '<- ListBox2 name
oField2.ListSourceType = 3 '<- Change ListBox2 to SQL from Default ValueList
if oForm.IsNew Then '<- Different form of Query for new record (if necessary)
strSQL = "SELECT `recip-name`.`recip-name` `recip-name`, `recip-name`.`recipid` `recipid` FROM `epicenter`.`recip-name` `recip-name`, `epicenter`.`dao-recip` `dao-recip` WHERE `recip-name`.`recipid` = `dao-recip`.`recipid` AND `dao-recip`.`daoid` = " + tVal + " GROUP BY `recip-name`.`recipid` ORDER BY `recip-name` ASC"
else
strSQL = "SELECT `recip-name`.`recip-name` `recip-name`, `recip-name`.`recipid` `recipid` FROM `epicenter`.`dao-recip` `dao-recip`, `epicenter`.`dao-dist` `dao-dist`, `epicenter`.`recip-name` `recip-name` WHERE `dao-recip`.`daoid` = `dao-dist`.`daoid` AND `recip-name`.`recipid` = `dao-recip`.`recipid` AND `dao-dist`.`daoid` = " + tVal + "ORDER BY `recip-name` ASC"
End If
oField2.listsource = array(strSQL) '<- Update ListBox2 with new data
oField2.refresh()
End Sub