Ask Your Question

Listbox contents filtered using another Listbox

asked 2018-03-14 17:26:51 +0200

MaaX gravatar image

Hi, i have looked through a few similar queries on this forum but have struggled to apply the solutions to my own problem.

My current REPLACES listbox lists all partnumbers in the database. I would like this REPLACES listbox to filter and only show the partnumbers related to a particular customer which has already been selected in an earlier listbox.

I can manage to get this working inside query design by simply adding a customer column and typing in a customer. When i run the query this way the results are restricted to the customer I have entered in the criterion field. However i need the criterion field to be updated according to the CUSTOMER listbox on the form.

So I guess my question is what do I enter into the Customer criterion field?C:\fakepath\Project Tracking test.odb

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2018-03-14 20:37:28 +0200

Ratslinger gravatar image

updated 2018-03-14 20:37:58 +0200

Hello MaaX,

This is a matter of modifying the SQL in the list box & refreshing the control when CUSTOMER changes. You can do this with a macro:

Sub CustChange
    oForm1 = ThisComponent.Drawpage.Forms.getByName("MainForm") 
    oField = oForm1.getByName("txtCustomer")
    sSelectedValue = oField.SelectedValue
    sSql = "SELECT DISTINCT ""Item"" FROM ""PartNumber"" WHERE ""Customer"" = '" & sSelectedValue & "' ORDER BY ""Item"" ASC"
    oField = oForm1.getByName("txtReplaces")
    oField.listsource = array(sSql)
End Sub

I would attach this in two events: Item status changed in txtCustomer list box and in After record change on MainForm. This allows for any change in the customer.

The routine itself when executed simply retrieves the current value of the Customer list box, inserts it into the SQL a string. The string is then inserted into the List source of the list box & the control refreshed generating a new list based on the Customer.

This answer only addresses your question and not anything else on your form.

If this answers your question please tick the ✔ (upper left area of answer). It helps others to know there was an accepted answer.

edit flag offensive delete link more


Brilliant, really appreciated as that works a treat. However you mention that it can be done modifying the SQL in the list box and then refreshing. I can see that the SQL command is there using the SELECT DISTINCT but also this has the sSelectedValue reference which is beyond me at this stage. Is there a way to separate the SQL and macro so i can add SQL direct to List content of the replaces listbox?

MaaX gravatar imageMaaX ( 2018-03-15 09:08:55 +0200 )edit

Also looking to add same to the Similar listbox. I tried to copy and paste the above changing out the Replaces for Similar and saving it as CustChange1 but its just ignored it like it has not even been added.

MaaX gravatar imageMaaX ( 2018-03-15 12:56:12 +0200 )edit

looks like i have the second one working. I added CustChange1 to txtCustomer listbox after updating. Seems to be working.

MaaX gravatar imageMaaX ( 2018-03-15 13:07:23 +0200 )edit

Reply to first comment. Because of your form design, the SQL must be part of the macro. This is key since the actual SQL is joined with the selected customer (sSelectedValue). This entire scenario may be possible without any macros if the form was designed differently.

sSelectedValue is fairly simple. The control (txtCustomer) is obtained on form (MainForm) and the current value of the control is places in this variable. It is the appended to the SQL to allow specific selection.

Ratslinger gravatar imageRatslinger ( 2018-03-15 14:23:19 +0200 )edit

Your secondary comments appear to have resolved the situation. You could have simply added:

oField = oForm1.getByName("txtSimilar")
oField.listsource = array(sSql)

before the End Sub line and both controls would be updated within the one macro.

Ratslinger gravatar imageRatslinger ( 2018-03-15 14:35:18 +0200 )edit

Yes i have had a few comments about the database design as a whole. Mainly the fact that i should be using more primary/foreign keys. I am still working on the above database but starting another one in parallel. I am looking at a medical database tutorial for help creating my new database. My first issue as always is trying to work out which fields need there own table and which ones can be part of the main table. I think i will look for a newbie section in a forum for these simpler problems.

MaaX gravatar imageMaaX ( 2018-03-16 09:08:08 +0200 )edit

answered 2019-07-14 08:02:31 +0200

Perfect. It worked for me, I tested and I'm very happy with the results, I'm using and external MySQL server as my DB.

Thanks this was easy. I've seen other method that involve much more work.


edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-03-14 17:26:51 +0200

Seen: 480 times

Last updated: Jul 14 '19