Results 1 to 4 of 4
  1. #1
    Star Lounger
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reading the forms (A2K)

    I need to write a routine that scans evey control in every form in a database, regardless of whether the form is already open. Is there an equivalent to TableDefs and QueryDefs, but which applies to forms? The Forms Collection only provides information about forms that are already open. I want to look at all forms, and do not want to have to know the names of the forms in advance. How can I do this?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Reading the forms (A2K)

    You can either use the CurrentProject.AllForms collection, or the DAO Forms container:

    Dim frm As AccessObject
    For Each frm In CurrentProject.AllForms
    DoCmd.OpenForm frm.Name, acDesign
    ...
    DoCmd.Close acForm, frm.Name, acSaveYes ' or acSaveNo
    Next frm
    Set frm = Nothing

    or (you need a reference to the Microsoft DAO 3.6 Object Library for this)

    Dim dbs As DAO.Database
    Dim ctr As DAO.Container
    Dim doc As DAO.Document
    Set dbs = CurrentDb
    Set ctr = dbs.Containers!Forms
    For Each doc In ctr.Documents
    DoCmd.OpenForm doc.Name, acDesign
    ...
    DoCmd.Close acForm, doc.Name, acSaveYes ' or acSaveNo
    Next doc
    Set doc = Nothing
    Set ctr = Nothing
    Set dbs = Nothing

  3. #3
    Star Lounger
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reading the forms (A2K)

    Thanks! Both examples are helpful and enlightening, especially the DAO version. However, neither approach seems to give me access to a form's controls. What am I missing here? I tried

    dim ctl as control

    for each ctl in doc.controls
    ...
    next

    but there appear to be no controls available. What am I missing?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Reading the forms (A2K)

    A Document object in the Forms collection represents a form, but it is not the form itself. Actually, I indicated what to do in both code fragments: open the form in design view. To expand the DAO example a bit:

    Dim dbs As DAO.Database
    Dim ctr As DAO.Container
    Dim doc As DAO.Document
    Dim frm As Form
    Dim ctl As Control

    Set dbs = CurrentDb
    Set ctr = dbs.Containers!Forms
    For Each doc In ctr.Documents
    ' Open form in design view.
    DoCmd.OpenForm doc.Name, acDesign
    ' Set reference to form.
    Set frm = Forms(doc.Name)
    ' Loop through form's controls.
    For Each ctl In frm.Controls
    ' Insert code to manipulate or test the control here
    Next ctl
    ' Close the form.
    DoCmd.Close acForm, doc.Name, acSaveYes ' or acSaveNo
    Next doc

    Set ctl = Nothing
    Set frm = Nothing
    Set doc = Nothing
    Set ctr = Nothing
    Set dbs = Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •