locked
Ways to get list of controls for every form? RRS feed

  • Question

  • I would like to be able to get a list of Controls for a form without opening the form in the traditional sense. For example, by creating a New instance of the Form, scanning it for its controls, then destroying the instance. I have a table with all the form names, they populate in a ListBox. I want to click on a form name in the listbox, and have all it's Control Names populate in another ListBox. I'm just worried about disrupting current Instances of forms I have open when that runs.

    I only want to see the list of controls for the form I click on in my ListBox.
    I am already creating a local table and looping over CurrentProject.AllForms to get a list of the forms for the FormNames ListBox. I put them in a table so I can alphabetise them.

    Thanks for the advice.

    • Edited by HTHP Wednesday, March 1, 2017 9:04 PM
    Wednesday, March 1, 2017 9:02 PM

Answers

  • Note that not all forms can have a non-default instance created via the New keyword.  Only those that have a class module -- those with VBA code -- can be instantiated this way.  Also, if the form has code or macros in its Open, Load, or Current events, that code would fire, which might be a problem when all you want to do is list its controls.

    You might consider making a copy of the form, opening that copy invisible in design view (so no code would fire), and looping through the controls on that form.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by HTHP Thursday, March 2, 2017 7:35 PM
    Wednesday, March 1, 2017 10:08 PM
  • Hi HTHP,

    here is the code example that other community members had already suggested you the logic to open form in design view and then list the controls.

     
    Sub demo()
    Dim ctl As Control
     Dim strFormName As String
    
     strFormName = "customimport"
     DoCmd.OpenForm strFormName, acDesign
     For Each ctl In Forms(strFormName)
     Debug.Print ctl.Name
     Next
     DoCmd.Close acForm, strFormName
    
    End Sub
    

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by HTHP Thursday, March 2, 2017 7:34 PM
    Thursday, March 2, 2017 2:44 AM

All replies

  • Hi,

    I suppose you could try "copying" the form instead and then destroying the copy afterwards?

    Just a thought...

    Wednesday, March 1, 2017 9:12 PM
  • Note that not all forms can have a non-default instance created via the New keyword.  Only those that have a class module -- those with VBA code -- can be instantiated this way.  Also, if the form has code or macros in its Open, Load, or Current events, that code would fire, which might be a problem when all you want to do is list its controls.

    You might consider making a copy of the form, opening that copy invisible in design view (so no code would fire), and looping through the controls on that form.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by HTHP Thursday, March 2, 2017 7:35 PM
    Wednesday, March 1, 2017 10:08 PM
  • I only want to see the list of controls for the form I click on in my ListBox.
    I am already creating a local table and looping over CurrentProject.AllForms to get a list of the forms for the FormNames ListBox. I put them in a table so I can alphabetise them.

    Hi HTHP,

    I think it becomes time that you come and visit me here in Zesgehuchten.

    A little explanation:

    column 1: Choose an application to look in.

    column 2: After selecting one of the applications (yellow) the available Items (Entities, Tables) are shown in column 3

    column 3: After selecting one of the Items (yellow) the available Controls (Fields) are shown in column 4.

    Column 4 is a multiselect colums, so 0 to many controls can be selected for further processing.

    Imb.

    • Edited by Imb-hb Wednesday, March 1, 2017 10:58 PM a little explanation
    Wednesday, March 1, 2017 10:49 PM
  • Hi HTHP,

    here is the code example that other community members had already suggested you the logic to open form in design view and then list the controls.

     
    Sub demo()
    Dim ctl As Control
     Dim strFormName As String
    
     strFormName = "customimport"
     DoCmd.OpenForm strFormName, acDesign
     For Each ctl In Forms(strFormName)
     Debug.Print ctl.Name
     Next
     DoCmd.Close acForm, strFormName
    
    End Sub
    

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by HTHP Thursday, March 2, 2017 7:34 PM
    Thursday, March 2, 2017 2:44 AM
  • Thanks for the replies! I'm actually able to use the MSysObjects table to find get a list of the Forms, its much better performance than iterating over the AllForms collection. As you guys suggest I am opening the form hidden in design view and iterating over the Controls collection. I'm not sure how to copy an instance of an open form, however. And is there a known way to alphabetize the list of the controls with good performance? All I can think of is inserting the results into a local table, then querying the table with an Order By clause.
    Thursday, March 2, 2017 1:30 PM
  • Thanks for the replies! I'm actually able to use the MSysObjects table to find get a list of the Forms, its much better performance than iterating over the AllForms collection. As you guys suggest I am opening the form hidden in design view and iterating over the Controls collection. I'm not sure how to copy an instance of an open form, however. And is there a known way to alphabetize the list of the controls with good performance? All I can think of is inserting the results into a local table, then querying the table with an Order By clause.

    Come to think of it, why would you need to copy an instance of a form that is open?  You could check each form to see if it's open, and if it is, just loop through the controls collection of the open instance.  If it isn't, then open the form hidden in design view and loop through that instance's controls.  (There may be a problem in the case of forms that are open as subforms of forms that are open; I don't know.)

    There's code out there on the web to sort an array using any of several different sort algorithms (though mostly bubble sort or quick sort).  Still, using a local table and querying it isn't a terrible idea, though it would lead to some bloating.  A somewhat more elaborate version of this is to create a temporary work database to hold the table, and delete it when you're done.  I have a class module for managing work databases, if you want to go that route.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Thursday, March 2, 2017 4:32 PM
  • Thanks! I I'll pass on the class module for now. Performance is decent using local table. I'm not worried about bloat as this app is compacted and automatically deployed to users as an accde file every update(which is about daily). Plus this isn't something that the users will be doing in their FE.
    Thursday, March 2, 2017 7:34 PM