Results 1 to 8 of 8
  1. #1
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581

    Refresh a list box

    I have a form with 2 list boxes on it. I tried to make it so that the list boxes will update when the form has focus. I went to OnGotFocus and added this:
    Private Sub Form_GotFocus()

    Me.lstCategories.Requery
    Me.lstInstructors.Requery



    End Sub

    It still did not update. It stayed the same. How would I do this?

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    A form can only receive the focus if there are no controls on it, or they are all disabled. Likely that is not the case here. You will have to use some other form event (current or activate?) or gotfocus for a control on it.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    I have one app where I have a lot of list boxes on several open forms. I use some public subs to requery them. I usually call it from the on close event of any form that alters data.
    The first sub loops through the forms collection and if a form is open it calls the second sub to iterate through the controls and requery the list boxes.

    Code:
    Public Sub RequeryAllLists()
    
    
        Dim frm As Variant
        
    
        On Error GoTo RequeryAllLists_Error
    
    
        For Each frm In CurrentProject.AllForms
    
    
            If frm.IsLoaded Then
    
    
                Call ReQListBoxes(Forms(frm.Name), False)
    
    
            End If
    
    
        Next
    
    
        On Error GoTo 0
        Exit Sub
    
    
    RequeryAllLists_Error:
    
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure RequeryAllLists of Module modLBX"
    
    
    End Sub
    Code:
    Public Sub ReQListBoxes(frm As Form, Optional NullValue As Boolean)
    
    
        Dim ctl As Control
        Dim ctl2 As Control
    
    
        On Error GoTo ReQListBoxes_Error
    
    
        For Each ctl In frm.Controls
    
    
            If ctl.ControlType = acListBox Then
    
    
                If NullValue = True Then
    
    
                    ctl.Value = Null
    
    
                End If
    
    
                ctl.Requery
    
    
            End If
    
    
            If ctl.ControlType = acSubform Then
    
    
                For Each ctl2 In ctl.Form.Controls
    
    
                    If ctl2.ControlType = acListBox Then
    
    
                        If NullValue = True Then
    
    
                            ctl.Value = Null
    
    
                        End If
    
    
                        ctl2.Requery
    
    
                    End If
                Next
    
    
            End If
    
    
        Next
    
    
        On Error GoTo 0
        Exit Sub
    
    
    ReQListBoxes_Error:
    
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ReQListBoxes of Module modLBX"
    
    
    End Sub
    HTH

  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,518
    Quote Originally Posted by Micron View Post
    A form can only receive the focus if there are no controls on it, or they are all disabled. Likely that is not the case here. You will have to use some other form event (current or activate?) or gotfocus for a control on it.
    Good call Micron. I saw this thread earlier and thought it should be working, then got distracted. The focus issue didn't occur to me. The activate event might be the closest equivalent.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    I tried On Activate, it didn't requery there either.

    I tried to use the code. I put "RequeryAllLists" on the On Current and also on the On Activate. My list boxes didn't requery. I'm not sure if that's how that code is designed to work though.

  6. #6
    essaytee's Avatar
    essaytee is offline Been around a while
    Windows 10 Access 2016
    Join Date
    Oct 2008
    Location
    Melbourne, Australia
    Posts
    27
    Check if your form is in 'Popup' mode, if so, there is no Activate.

  7. #7
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    The code I posted earlier was an old version which was the first I came accross when I was responding.
    Below is a more recent copy where the 2 procedures are merged into one.

    Code:
    Public Sub RequeryAllLists()
    
    
        Dim ctl As Control
        Dim ctl2 As Control
        Dim frm As Variant
    
    
        For Each frm In CurrentProject.AllForms
            If frm.IsLoaded Then
                For Each ctl In Forms(frm.Name).Controls
                    If ctl.ControlType = acListBox Then
                        ctl.Requery
                    End If
                    If ctl.ControlType = acSubform Then
                        For Each ctl2 In ctl.Form.Controls
                            If ctl2.ControlType = acListBox Then
                                ctl2.Requery
                            End If
                        Next
                    End If
                Next
            End If
        Next
    
    
    End Sub
    This code goes into a standard module, not a form module.

    When I use it I usually do something like this...

    I have forms open, usually several, with listboxes on them. Lets say one is a list of people. When I want to add a person, I open a form to add the persons info. In the on close event of that data entry form I include the call to the RequeryAllLists procedure.

    One way you can test to see if its the event thats not working is to put a button on your form and run it from there.

    Could you describe your workflow. How are you updating the data for the list boxes?

  8. #8
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    That worked. Thanks.

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

Similar Threads

  1. List Box Refresh? Frustrated!! PLEASE HELP!!
    By SmartestIdiot in forum Forms
    Replies: 13
    Last Post: 01-11-2014, 07:08 AM
  2. List Box Items Are De-Selected On A Screen Refresh
    By plengeb in forum Programming
    Replies: 2
    Last Post: 10-03-2012, 11:58 AM
  3. Dependent List Boxes do not refresh using ReQuery
    By weeblesue in forum Programming
    Replies: 2
    Last Post: 03-28-2011, 08:47 AM
  4. Loosing list box slection after refresh
    By oakoen in forum Forms
    Replies: 6
    Last Post: 11-23-2010, 11:21 PM
  5. Refresh a list box
    By Orabidoo in forum Forms
    Replies: 9
    Last Post: 04-17-2009, 04:07 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