Is there any way I can get a form to requery all the list boxes when I close one form and the other form becomes active? I have tried so many different methods for this but not found anything simple yet.
~Matt
Is there any way I can get a form to requery all the list boxes when I close one form and the other form becomes active? I have tried so many different methods for this but not found anything simple yet.
~Matt
Please give readers some context for your question. Sample data?
Often when you requery the form- Me.Requery - you get the latest data.
When you say "...all the list boxes...", which list boxes are you referring to? "All" doesn't tell us much. Requerying list boxes on a form you are closing is meaningless, because the changes are not saved when the form is closed. When a form opens, its list boxes are either initially populated according to the record source for each of them, or their recordsource can be defined with VBA when the form opens.
Please give us a bit more information on what you need to do.
Perhaps you should attach your database with clear instructions to help readers experience your issue, and a clear example of what you want to occur.
This might work:
Code:Option Compare Database Option Explicit Private Sub Form_Activate() lstOne.requery lstTwo.requery lstThree.requery . . . End Sub
Will refresh your listbox.Code:Me.ListBoxName.Requery
As others have stated we're not clear on what exactly you're doing but guessing the following info maybe of use:
Let's say you have a Listbox of items on your unbound form and you want to launch another from to add/edit the recordsource for that listbox.
You might have a command button on your main/unbound form with the following code
Note the last option of the DoCmd.OpenForm command "acDialog", this will pause the execution of the code in the CmdEditRows_Click() procedure until the "your_form_name" is closed. Then when it refreshes your listbox presumably your edits to the rowsource have already been saved.Code:Private Sub CmdEditRows_Click() DoCmd.OpenForm "your_form_name", , , "criteria_goes_here", , acDialog 'Because the form was opened with the acDialog option the following code wont execute until the form launched above is closed. Me.ListBoxName.Requery End Sub
Okay, I shall try to explain better.When you say "...all the list boxes...", which list boxes are you referring to? "All" doesn't tell us much. Requerying list boxes on a form you are closing is meaningless, because the changes are not saved when the form is closed. When a form opens, its list boxes are either initially populated according to the record source for each of them, or their recordsource can be defined with VBA when the form opens.
Please give us a bit more information on what you need to do.
I have a customer overview form with the customer address and other data. I have an unbound list box with all the customer quotes listed (data source is a query where the CustomerID field = Forms!frmCustomerOverview!CustomerID). When I add a new quote it opens frmQuote, I enter the data and send the quote. When I click close it goes back to to frmCustomerOverview and now I want the List Box with the customer quotes to requery to show the quote I just sent. At the moment I have a Requery module that is activated when my cmdClose is clicked. It closes the current form and then checks which form is now open and then Requerys the list boxes on that form. I was hoping there may be an easier way.
~Matt
We replied at the same time, check my post #7 and see if that will help you.Okay, I shall try to explain better.
I have a customer overview form with the customer address and other data. I have an unbound list box with all the customer quotes listed (data source is a query where the CustomerID field = Forms!frmCustomerOverview!CustomerID). When I add a new quote it opens frmQuote, I enter the data and send the quote. When I click close it goes back to to frmCustomerOverview and now I want the List Box with the customer quotes to requery to show the quote I just sent. At the moment I have a Requery module that is activated when my cmdClose is clicked. It closes the current form and then checks which form is now open and then Requerys the list boxes on that form. I was hoping there may be an easier way.
~Matt
Yes, that looks like it could help. I shall give it a try. Would I be stuck on the acDialog form until it is finished with? Sometimes I will open the frmQuote but then have to visit other forms IE frmPriceList, to get the pricing for the quote. If the acDialog keeps that form on top until I am done with my entry, it would not work.
I was hoping for something when the form gains focus but I have tried those and they do not seem to Requery.
~Matt
Yes. But you could open frmPriceList from frmQuote. davegri's suggestion may be more appropriate for your situation. Or even your current approach in reply #8 seems reasonable.
I have 2 public subs I use. I usually call them in an OnClose event.
the first, ReQListBoxes, will requery all listboxes on a given form.
The second, RequeryAllLists, will requery the listboxes in any open forms. It calls ReQListBoxes so you need both subs for this.Code:Call ReQListBoxes(Forms("YourFormName"),true)
Code:Call RequeryAllListsHTHCode: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 Public Sub RequeryAllLists() Dim frm As Variant Dim f As Access.Form 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
That looks like what I need, I shall give this a try later. Thank you.I have 2 public subs I use. I usually call them in an OnClose event.
the first, ReQListBoxes, will requery all listboxes on a given form.
The second, RequeryAllLists, will requery the listboxes in any open forms. It calls ReQListBoxes so you need both subs for this.Code:Call ReQListBoxes(Forms("YourFormName"),true)
Code:Call RequeryAllListsHTHCode: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 Public Sub RequeryAllLists() Dim frm As Variant Dim f As Access.Form 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
~Matt
@moke123,
That was exactly what I was looking for. I have integrated it into my database and it works perfect. Thank you for your help. This thread can now be solved
~Matt
Happy to help. Good luck with your project.