• Listing Forms in an Application

    Author
    Topic
    #462310

    I’ve tried –
    For each frm in Forms
    debug.print frm.name
    Next

    But of course this only finds forms which are currently open.

    How do I list all forms even if they are closed?

    Viewing 2 reply threads
    Author
    Replies
    • #1176247

      In order to do that you need to refer to the AllForms Collection. See MSDN AllForms Collection for the details and an example of exactly how to list all forms in a database or a project. It is specific to Access 2003 but other version work in a similar fashion.

      • #1176249

        In order to do that you need to refer to the AllForms Collection. See MSDN AllForms Collection for the details and an example of exactly how to list all forms in a database or a project. It is specific to Access 2003 but other version work in a similar fashion.

        Thanks Wendell, that’s what I wanted.

      • #1176252

        In order to do that you need to refer to the AllForms Collection. See MSDN AllForms Collection for the details and an example of exactly how to list all forms in a database or a project. It is specific to Access 2003 but other version work in a similar fashion.

        I’m having problems trying to find the desctription property of the form.
        I’m using the name of the allforms object to open the form and I can then get all the other properties but I just can’t find the description.

    • #1176339

      Try the example shown in MSDN Properties Collection – it should enumerate the properties of a form if you have one open, and give you the name of each property.

      • #1176372

        Try the example shown in MSDN Properties Collection – it should enumerate the properties of a form if you have one open, and give you the name of each property.

        Thanks Wendell but I’d already iterated throught the properties collection, but the Description if not there.
        I’ve checked both the properties of AllForms and of open forms. No Description property.
        Any ideas as to where else I might find it?

        • #1176378

          Here is a function that will return the Description property of a form. I found this Here

          Code:
           Function FormDescription(FormName As String, Optional DefaultDescription = Empty, Optional ClearDescription As Boolean = False) As String
          	 Dim loDb   As DAO.Database
          	 Dim loForm As DAO.Document
          	 Dim loProp As DAO.Property
          	 Dim strRet As String
           
          	 On Error GoTo FormDescription_err
          	 Const PROP_NAME_DESCRIPTION = "Description"
          	 Const CONTAINER_FORMS = "Forms"
           
          	 strRet = vbNullString
          	 Set loDb = CurrentDb
          	 Set loForm = loDb.Containers(CONTAINER_FORMS).Documents(FormName)
           
          	 If Not ClearDescription Then
          		 strRet = loForm.Properties(PROP_NAME_DESCRIPTION)
          	 Else
          		 On Error Resume Next
          		 With loForm.Properties
          			 .Delete PROP_NAME_DESCRIPTION
          			 .Refresh
          		 End With
          		 On Error GoTo FormDescription_err
          	 End If
           FormDescription_end:
          	 On Error Resume Next
          	 Set loForm = Nothing
          	 Set loDb = Nothing
          	 Set loProp = Nothing
          	 FormDescription = strRet
          	 Exit Function
           
           FormDescription_err:
          	 Select Case Err
          		 Case 3265	'Item not found in this collection.
          			 MsgBox "The form " & FormName & " does not exist."
          		 Case 3270	'Property not found.
          			 If IsEmpty(DefaultDescription) Then
          				 strRet = vbNullString
          			 Else
          				 If Len(DefaultDescription) > 0 Then
          					 With loForm
          						 Set loProp = .CreateProperty(PROP_NAME_DESCRIPTION, dbText, DefaultDescription)
          						 With .Properties
          							 .Append loProp
          							 .Refresh
          						 End With
          					 End With
          					 Resume
          				 Else
          					 strRet = vbNullString
          				 End If
          			 End If
          		 Case Else
          			 MsgBox Err.Description
          	 End Select
          	 Resume FormDescription_end
           End Function

          Then this will loop through each form in Allforms, print its name and Description.

          Code:
           Private Sub Command70_Click()
          	 
          	 Dim strDescription As String
          	 Dim obj As AccessObject, dbs As Object
          	 Set dbs = Application.CurrentProject
          	 ' Search for open AccessObject objects in AllForms collection.
          	 For Each obj In dbs.AllForms
          		Debug.Print obj.Name
          		strDescription = FormDescription(obj.Name)
          		Debug.Print strDescription
          	  Next obj
           
           End Sub
          • #1176386

            Here is a function that will return the Description property of a form. I found this Here

            Code:
             Function FormDescription(FormName As String, Optional DefaultDescription = Empty, Optional ClearDescription As Boolean = False) As String
            	 Dim loDb   As DAO.Database
            	 Dim loForm As DAO.Document
            	 Dim loProp As DAO.Property
            	 Dim strRet As String
             
            	 On Error GoTo FormDescription_err
            	 Const PROP_NAME_DESCRIPTION = "Description"
            	 Const CONTAINER_FORMS = "Forms"
             
            	 strRet = vbNullString
            	 Set loDb = CurrentDb
            	 Set loForm = loDb.Containers(CONTAINER_FORMS).Documents(FormName)
             
            	 If Not ClearDescription Then
            		 strRet = loForm.Properties(PROP_NAME_DESCRIPTION)
            	 Else
            		 On Error Resume Next
            		 With loForm.Properties
            			 .Delete PROP_NAME_DESCRIPTION
            			 .Refresh
            		 End With
            		 On Error GoTo FormDescription_err
            	 End If
             FormDescription_end:
            	 On Error Resume Next
            	 Set loForm = Nothing
            	 Set loDb = Nothing
            	 Set loProp = Nothing
            	 FormDescription = strRet
            	 Exit Function
             
             FormDescription_err:
            	 Select Case Err
            		 Case 3265	'Item not found in this collection.
            			 MsgBox "The form " & FormName & " does not exist."
            		 Case 3270	'Property not found.
            			 If IsEmpty(DefaultDescription) Then
            				 strRet = vbNullString
            			 Else
            				 If Len(DefaultDescription) > 0 Then
            					 With loForm
            						 Set loProp = .CreateProperty(PROP_NAME_DESCRIPTION, dbText, DefaultDescription)
            						 With .Properties
            							 .Append loProp
            							 .Refresh
            						 End With
            					 End With
            					 Resume
            				 Else
            					 strRet = vbNullString
            				 End If
            			 End If
            		 Case Else
            			 MsgBox Err.Description
            	 End Select
            	 Resume FormDescription_end
             End Function

            Then this will loop through each form in Allforms, print its name and Description.

            Code:
             Private Sub Command70_Click()
            	 
            	 Dim strDescription As String
            	 Dim obj As AccessObject, dbs As Object
            	 Set dbs = Application.CurrentProject
            	 ' Search for open AccessObject objects in AllForms collection.
            	 For Each obj In dbs.AllForms
            		Debug.Print obj.Name
            		strDescription = FormDescription(obj.Name)
            		Debug.Print strDescription
            	  Next obj
             
             End Sub

            Well that’s just fantastic. I’d googled my little fingers to the bone and couldn’t find anything.

            This does the job. A very big THANKYOU!!
            Kent

    • #1176423

      Just to wrap this up, I’m curious why you wanted to do this to begin with? I presume you are aware of the Document facilities in Access that will do this sort of thing and a fair bit more without writing and VBA.

    Viewing 2 reply threads
    Reply To: Listing Forms in an Application

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: