Results 1 to 11 of 11
  1. #1
    aknorth is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2018
    Posts
    7

    VBA Combo filtering

    I am having trouble filtering one combo box on another. I found this very useful tutorial


    http://www.databasedev.co.uk/filter_combo_boxes.html
    I even downloaded the sample database, however I am having a very hard time implementing it on something else

    Here is their code
    Private Sub cboStore_AfterUpdate()
    Dim sManagerSource As String

    sManagerSource = "SELECT [tblManager].[lngManagerID], [tblManager].[lngStoreID], [tblManager].[strManagerName] " & _
    "FROM tblManager " & _
    "WHERE [lngStoreID] = " & Me.cboStore.Value
    Me.cboManager.RowSource = sManagerSource
    Me.cboManager.Requery
    End Sub

    cboStore Row source SELECT [tblStore].[lngStoreID], [tblStore].[strStoreName] FROM tblStore;
    cboManager Row source SELECT [tblManager].[lngManagerID], [tblManager].[lngStoreID], [tblManager].[strManagerName] FROM tblManager;

    Here is my code

    Private Sub cboFirstName_AfterUpdate()
    Dim sLastSource As String


    sLastSource = "SELECT [contactsT].[ID], [contactsT].[first_name], [contactsT].[last_name] " & _
    "FROM contactsT " & _
    "WHERE [first_name] = " & Me.cboFirstName.Value
    Me.cboLastName.RowSource = sLastSource
    Me.cboLastName.Requery
    End Sub

    Basically I have a contacts table with ID, first_name, and last_name and combo boxes cboFirstName and cboLastName

    cboFirstName Row source SELECT [contactsT].[ID], [contactsT].[first_name] FROM contactsT;
    cboLastName Row source SELECT contactsT.ID, contactsT.first_name, contactsT.last_name FROM contactsT;

    I am completely flummoxed as to why this isn't working. Does anyone see something I am missing?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You need delimiters around text values, like:

    "WHERE [first_name] = '" & Me.cboFirstName.Value & "'"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    aknorth is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2018
    Posts
    7
    Thank you for the quick reply. Unfortunately that didn't solve the problem whenever I select the last name combo box it just shows blank. I have attached a super simple sample database just one table and the form with the two combo boxes I am trying to get this to implement on.
    Attached Files Attached Files

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Use this and you should see the problem (teaching how to fish):

    http://www.baldyweb.com/ImmediateWindow.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    aknorth is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2018
    Posts
    7
    Click image for larger version. 

Name:	vba.jpg 
Views:	6 
Size:	102.4 KB 
ID:	35193I completely understand that doing something is the best way to learn and giving the solution doesn't always help. I have been plugging away at this for 2 hours now so I am not trying to be handed the solution. So under the VBA editor I added the immediate window and I modified my code so it now reads

    Private Sub cboFirstName_AfterUpdate()
    Dim sLastSource As String

    sLastSource = "SELECT [contactsT].[ID], [contactsT].[first_name], [contactsT].[last_name] " & _
    "FROM contactsT " & _
    "WHERE [first_name] = '" & Me.cboFirstName.Value & "'"
    Debug.Print sLastSource
    Me.cboLastName.RowSource = sLastSource
    Me.cboLastName.Requery
    End Sub

    I do not know what to do next. I can't seem to run the code to get it to output that to the Immediate window I hit the green run button and the Macros window pops up.

    I created a macro to run it in it stated Compile error: Invalid use of Me keyword and the Me in the Me.cboFirstName.Value is highlighted

    SQL in the bottom states

    SELECT [contactsT].[ID], [contactsT].[first_name], [contactsT].[last_name] FROM contactsT WHERE [first_name] = '1'

    So the only thing I see is an issue is maybe contactsT need brackets and also whatever is to the right of the = sign is not right somehow

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You would just run the code by changing the option in the first combo. That said, you've suffered enough. The bound column of the first combo is the ID field, so you're comparing apples to oranges. I'd change the first combo to just return first names, since the ID field is irrelevant there. I'd also have it return a single instance of each name, instead of however many exist in the data. Something like:

    SELECT DISTINCT contactsT.first_name FROM contactsT;
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Oh, and FYI the debug would give you:

    SELECT [contactsT].[ID], [contactsT].[first_name], [contactsT].[last_name] FROM contactsT WHERE [first_name] = '1'
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    aknorth is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2018
    Posts
    7
    Man you have been super patient with me, I really don't want to ask any more ?'s but I would like to clarify some things to make sure I know how to fish.

    1) What do you mean by "run the code by changing the option in the first combo"?

    2) I see what you mean the first combo box has two fields and it is selecting the ID and trying to compare to first_name and they don't match up that makes sense

    I took the following actions

    1) Changed Row Source for cboFirstName to
    SELECT DISTINCT contactsT.first_name FROM contactsT;

    This created a problem however now nothing showings up in the first name combo box.

    Getting rid of the distinct and setting Row Source SELECT contactsT.first_name FROM contactsT;

    This also only shows blank entries, the only way I get the combo box to show anything on the combo box is to add back in the ID field

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Your code is in the after update event of the first combo, so with the Debug.Print in the code, you can simply open the form and make a selection in the first combo which will run the code and the output should go to the Immediate window.

    You have to change the column widths property of the first combo. It's currently hiding the first column. I'd also change the column count to 1.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    aknorth is offline Novice
    Windows 10 Access 2016
    Join Date
    Aug 2018
    Posts
    7
    Finally got it! Thanks a ton.

    I am not sure what was creating the issue but if you create a combo box using the "I want the combo box to get the values from another table or query" it will insist on adding the ID field if you delete the ID field it will show a blank drop down for the remaining field. Creating one from scratch allows you to bypass this. There must be an attribute you can change to correct this though I do no know what it is.

    Thank you thank you

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help! Yeah, it seems like the wizard used to work pretty well, and would create it properly if you didn't want the ID field. Now...
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 06-17-2018, 12:07 AM
  2. Replies: 3
    Last Post: 07-04-2017, 07:22 AM
  3. Replies: 1
    Last Post: 07-12-2012, 08:39 AM
  4. Replies: 5
    Last Post: 03-12-2012, 02:58 AM
  5. Replies: 0
    Last Post: 08-17-2008, 12:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums