1

First off the user selects a product by using the yellow section. This generates the information for the drop down box to the right in the results section. The user selects a specific product and Dlookups generate the values in the text boxes.

On the form below, I have a list box (not currently pictured). The list box lists information that was generated from a query using the attributes section. The problem is the listbox often shows information from the query that does not match any results in the text boxes. I need to remove that result from the list.

enter image description here

Using this code below which I was given, it loops through each text box control and checks if the values in each text box match the result of the query. If not then to remove from the list box. However it does not seem to be working. Any suggestions on where this code is going wrong or suggestions on how to achieve the same objective.

    Private Sub Command87_Click()
    'Refine Search Results based on the control attributes
        Dim iCtr As Long, valFound As Boolean, frmCtl As Control
        Dim selStr As String

        With Me.ResList
            iCtr = .ListCount
            While iCtr <> 0
                For Each frmCtl In Me.Controls
                    If frmCtl.ControlType = acTextBox Then
                        If .Column(0, iCtr) = frmCtl.Value Then
                            valFound = True
                            Exit For
                        End If
                    End If
                Next
                If Not valFound Then selStr = selStr & .Column(0, iCtr) & ","
                iCtr = iCtr - 1
            Wend
        End With

        If Len(selStr) > 0 Then
            selStr = Left(selStr, Len(selStr) - 1)

            Me.ResList.RowSource = ("Test_Qry")

        End If
End Sub

enter image description here

6
  • Were you able to make progress on this? Commented Apr 21, 2014 at 21:21
  • Not currently it has been placed on the back burner as our attention has been focused on other projects that are required asap. I will look at this week and let you know. Commented Apr 23, 2014 at 7:08
  • I'm still having issues with this one. I still can't get it to reduce the list where they are not equal to the textboxes on the form. Any ideas? Sorry its taken so long to get back to you i got bombarded with other projects with a higher priority. Commented Jun 2, 2014 at 20:56
  • 1
    That may be fixed with my new correction to my answer. Commented Jun 3, 2014 at 1:38
  • that worked better than previous. It counted that there 3 in the list, however 2 did not match and one should have been left behind. Instead it removed all three from the list. Commented Jun 3, 2014 at 8:24

1 Answer 1

1
selStr = Left(selStr, Len(selStr) - 1)
Me.ResList.RowSource = ("Test_Qry")

You seem to have what you need with selStr, but you don't do anything with it. You need to do something like:

Me.ResList.RowSourceType = "Value List"

Me.ResList.RowSourceType = "Table/Query"
Me.ResList.RowSource = selStr

Note correction above, 6/2/2014

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for such a quick response, I hadn't noticed that line wasn't being used. When i used your solution it did not produce any errors however does not list any results. When i ran it with a break point it showed the value of selStr as ",".
For starters, make up a value for selStr just before you declare the .RowSource so you can test the final UI part. Then use Debug.Print to discover why selStr isn't getting loaded as it should. There could be many causes - typo, logic flaw, problem with If() comparison...
I found that there was an issue with the .column if you have any input. Please see the additional picture above.
Your list box has specifications that must match the code. You can learn what the code "sees": in break mode, use the Immediate Window to check values by entering ?.column(0,1), ?.column(1,0) and so on. The Locals Window might be even better.
I have only just noticed this... The person who wrote this code didn't take into account the information is generated in a query (Test_Qry) So the code is has nothing to compare the results on the form too as it is not loading any information for the comparison.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.