Results 1 to 13 of 13
  1. #1
    jmullan99 is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    4

    Listbox not functioning as expected???

    I have built a form with tab control. The listbox is on tab2 not bound to any control source. I simply want a listbox that populates from a rowsource query. The form is saved with an empty Control Source value, and in code, I update the property with the query SQL. Row Source Type is table/query.



    When my code updates the Row Source property, nothing happens, list box is empty. I've also tried a variety of methods like Requery, Refresh, Repaint. and nothing.

    In the code below, the various things I have tried are commented out. I am using a After Update from a combo box on tab 1. The debug.print query is copy and pasted into the MySQL client and runs perfectly.

    Can anyone suggest why the list box will not update. A couple hours searching has resulted is some answers (hence my commented out attempts) but nothing works.

    It "may" be that the query is based on linked tables. Maybe I need to build it as a pass-thru?

    Code:
            Else
                'Display the found record in the form tab#1.
                Me.Bookmark = rs.Bookmark
                
                ' Update the ingredients list box on tab#2
                squery = "SELECT ingredients.ingredient_id, ingredients.ingredient_name, " & _
                "recipe_items_links.recipe_ingredient_qty, uom.uom_description " & _
                "FROM recipe_items_links " & _
                "LEFT JOIN ingredients ON ingredients.ingredient_id = recipe_items_links.recipe_ingredient_id " & _
                "LEFT JOIN uom ON ingredients.ingredient_uom = uom.uom_id " & _
                "WHERE recipe_items_links.recipe_id = " & Me.recipe_id & ";"
                Debug.Print squery
                'Set rs2 = db.OpenRecordset(squery)
                
                Me.ingredient_list.RowSource = squery
                'Me.ingredient_list.Recordset = rs2
                
                Debug.Print ingredient_list.RowSource
                'Me.ingredient_list.Requery
                'Me.Repaint
                
                'DoCmd.save acForm, "Recipes"
                'Me.Refresh
                
            End If
            Set rs = Nothing
    Thanks in advance for any help.

    Cheers.

  2. #2
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    first take your debug.print output, paste it into a new query in sql view (just close the dialog for choosing tables) and run it. If it's an action query that you don't want to run at this point, simply choose datasheet view. If you get an error or no records, then there's something wrong with your sql.

    Also, you cannot refer to a listbox on a tab control using just Me.lstNameOfListbox. You have to include the tab control, and if it has more than one page, usually the page name/number is needed also. Verify the sql works first.
    Last edited by Micron; 02-14-2018 at 07:31 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    jmullan99 is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    4
    Thanks for jumping in. The query does indeed work. I've copied and pasted the code into an Access query SQL view and it runs fine. Again, that query was set up as a pass-thru. But there is still nothing in the listbox.

    Current code state for simplicity sake
    Code:
                ' Update the ingredients list box on tab#2
                squery = "SELECT ingredients.ingredient_id, ingredients.ingredient_name, " & _
                "recipe_items_links.recipe_ingredient_qty, uom.uom_description " & _
                "FROM recipe_items_links " & _
                "LEFT JOIN ingredients ON ingredients.ingredient_id = recipe_items_links.recipe_ingredient_id " & _
                "LEFT JOIN uom ON ingredients.ingredient_uom = uom.uom_id " & _
                "WHERE recipe_items_links.recipe_id = " & Me.recipe_id & ";"
                Debug.Print squery
    
                Me!ingredient_list.RowSource = squery
                
                Debug.Print ingredient_list.RowSource

  4. #4
    John_G is offline VIP
    Windows 10 Access 2016
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Me!ingredient_list.RowSource = squery
    That itself does not update the listbox contents. You have to requery the listbox after changing the row source:

    Me!ingredient_list.RowSource = squery
    Me!ingredient_list.Requery

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    I've also tried a variety of methods like Requery, Refresh, Repaint
    On what? The form? If you alter the rowsource of a control, you have to requery the control. I don't see where you do that in the code.

    With Me.ingredient_list
    .RowSource = squery
    .Requery
    End With

    or
    Me.ingredient_list.RowSource = squery
    Me.ingredient_list.Requery

    Note that if you use Me!ingredient_list instead of Me.ingredient_list, a typo on the control name won't be caught until run time. It will not be caught when compiling your code. Most often its use is explained as being needed when the preceding word denotes that what follows belongs to a collection, such as Forms!frmMain or Reports!rptMyReport, but that's not entirely accurate.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    jmullan99 is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    4
    Thanks folks. I have indeed tried .Requery (its one of the commented out lines in first post).

    After messing around a bit, the following is the code that is working for me. I made a QueryDef and saved it, and made it the Row Source for the listbox. I open it, change the SQL as necessary, and update the Row Source property (even though it is the same QueryDef name every time). Why the other methods I have seen, and people tell me, haven't worked is beyond me.

    Code:
                ' Update the ingredients list box on tab#2
                squery = "SELECT ingredients.ingredient_id, ingredients.ingredient_name, " & _
                "recipe_items_links.recipe_ingredient_qty, uom.uom_description " & _
                "FROM recipe_items_links " & _
                "LEFT JOIN ingredients ON ingredients.ingredient_id = recipe_items_links.recipe_ingredient_id " & _
                "LEFT JOIN uom ON ingredients.ingredient_uom = uom.uom_id " & _
                "WHERE recipe_items_links.recipe_id = " & Me.recipe_id & ";"
    
                'Debug.Print squery
                
                Set q = db.QueryDefs("list_ingredients")
                q.SQL = squery
                q.Close
                
                Me!ingredient_list.RowSource = "list_ingredients"

  7. #7
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    What about the proper way to reference a list box on a tab control mentioned in paragraph 2 of post 2. I don't see where you corrected that.

  8. #8
    jmullan99 is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    4
    I actually tried a couple variations of referencing that didn't work (but I am rather novice about the exact proper ways to reference in various scenarios). But still the assignment to RowSource in my second code sample DID work. I'm still leaning toward the fact that it doesn't work to assign the SQL if it is pass-thru. But does work to use the QryDef and change the SQL as necessary, then assign.

  9. #9
    John_G is offline VIP
    Windows 10 Access 2016
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Micron -

    Also, you cannot refer to a listbox on a tab control using just Me.lstNameOfListbox. You have to include the tab control, and if it has more than one page, usually the page name/number is needed also.
    Are you sure about that? I just tested it, and I was unable to make any syntax work using the tab control in the reference. From the main form, I was able to reference a list box on the tab control just using Me!List13, just as with any other control. I think controls on tab pages are not part of the tab control; they are still on the main form.

  10. #10
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    I guess I'm not. I used MSDN or other web pages on how to reference controls on a tab control as the source behind that statement. You are saying that regardless of how many pages a tab control has, you can refer to any control on any page via Me!nameOfControl? What about Me.nameOfControl?

  11. #11
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    both me. and me! will work. There is no need to reference a page in a tab control unless of course you are setting focus to that page.

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Tab control has no bearing on how controls positioned on it are referenced.

    Recommend you use dot (.) to trigger intellisense popup tips as well as the compile validation. The bang (!) will not do either.

    I do use ! when referencing fields of recordset object.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    The only distinction I do feel confident in is that what follows Me! won't be evaluated at compile time. That I have tested.

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

Similar Threads

  1. Insert SQL not functioning
    By doobybug in forum Forms
    Replies: 15
    Last Post: 05-15-2017, 08:55 PM
  2. Update Query functioning in second attempt
    By drunkenneo in forum Programming
    Replies: 2
    Last Post: 11-18-2013, 05:12 AM
  3. Combo Boxes Not Functioning
    By cdixon2098 in forum Forms
    Replies: 6
    Last Post: 08-27-2012, 02:51 PM
  4. After Update/concatenation not functioning
    By tariq1 in forum Programming
    Replies: 4
    Last Post: 08-19-2012, 11:54 AM
  5. After Insert macro not functioning
    By scottnew2ac in forum Access
    Replies: 2
    Last Post: 07-26-2012, 09:25 AM

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