List Loaded forms and subforms (1 Viewer)

Jaye7

Registered User.
Local time
Today, 20:11
Joined
Aug 19, 2014
Messages
205
I have the following script which lists loaded forms and the controls sources.

What I would like is to list the loaded forms and there subforms control sources.

Code:
Public Sub ListOpenFormControls()
' Read control source property of all controls in all forms.
' Note.. the debug window may not be big enough to display
' all of the data.
Dim db As Database, doc As Document, ctl As Control
Set db = CurrentDb
Dim intX As Integer
Dim SourceName1 As String

'Add Temp Table
'FIELDS WITH SQL'S MUST BE MEMO FIELDS OR NO DATA WILL SHOWN
DoCmd.SetWarnings False
'========================================================================
'Delete Table if exists
Dim dbs As Database
 Dim tbl As TableDef
 Dim dbsExist As Object
 
 Dim TableName As String
 TableName = "zzzTempControlSources"   ' Your Table Name
 
     Set dbs = CurrentDb
     Set dbsExist = dbs.TableDefs
'--------------------------------------------------------------
 ' Search for AccessObject objects in AllTables collection.
 DoCmd.Close acTable, "zzzTempControlSources"
 
     For Each tbl In dbsExist
         If tbl.Name = TableName Then
             'MsgBox "Table Exists"
             DoCmd.DeleteObject acTable, "zzzTempControlSources"
             GoTo CreateT1
         End If
     Next tbl
CreateT1:
'========================================================================
DoCmd.RunSQL "CREATE TABLE [zzzTempControlSources] ([FormName] TEXT, [ControlName] TEXT, [FormRecordSource] Memo, [ControlSource] Memo, [RowSource] Memo)"
DoCmd.SetWarnings True

DoCmd.SetWarnings True

For Each doc In db.Containers("Forms").Documents

If CurrentProject.AllForms(doc.Name).IsLoaded = True Then

     ' Form is open
      
'DoCmd.OpenForm doc.Name, acDesign, , , , acHidden

Dim dbCurrent As DAO.Database
Dim rstWarr As DAO.Recordset

'set the Database and then the table name
   Set dbCurrent = CurrentDb
   Set rstWarr = dbCurrent.OpenRecordset("zzzTempControlSources") ' table name

'we are adding a record to our table
   rstWarr.AddNew
'we add a value to the 1st field of the table which is named FONID
rstWarr("FormName").Value = Forms(doc.Name).Name
SourceName1 = Forms(doc.Name).RecordSource
rstWarr("FormRecordSource").Value = SourceName1
'we now add the data to the table
   rstWarr.Update

'Debug.Print doc.Name
On Error Resume Next

For Each ctl In Forms(doc.Name)
'APPEND NEW RECORD
'we are adding a record to our table
   rstWarr.AddNew

'we add a value to the 1st field of the table which is named FONID
rstWarr("FormName").Value = doc.Name
rstWarr("ControlName").Value = ctl.Name
rstWarr("Controlsource").Value = ctl.ControlSource
rstWarr("RowSource").Value = ctl.RowSource

'we now add the data to the table
   rstWarr.Update
   
   Next
Else
     ' Form is closed
     'MsgBox "oh oh"
    
End If

'clean up memory
   Set dbCurrent = Nothing
   Set rstWarr = Nothing
   Set dbsExist = Nothing ' Clean up

Next
DoCmd.Close acForm, doc.Name

'=======================================================================



With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    If .Show <> -1 Then
    'MsgBox "No folder selected! Exiting sub...":
    Forms!frm_Order_edit.TimerInterval = 0
    Exit Sub
    End If
    mydir = .SelectedItems(1)
End With
'MsgBox mydir

    Dim cdb As DAO.Database, qdf As DAO.QueryDef
    Set cdb = CurrentDb
    
    Dim filename As String, DateName As String
    DateName = Format$(Date, "yyyymmdd")
    
     filename = "List of form control recordsources " & DateName
                
    Dim FILE_PATH As String
    FILE_PATH = mydir & "\" ' change this to your file location
    
    Dim strFullPath As String, strFileName As String
    strFullPath = FILE_PATH
    
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "zzzTempControlSources", FILE_PATH & filename, True, "Recordsources"
   
    Set cdb = Nothing

    'On Error Resume Next
    Workbooks.Open filename & ".xlsx"

DoCmd.OpenForm "GeneralMessageBox"
Forms!GeneralMessageBox.lbCaption.Caption = vbNewLine & "Excel file created - " & strFullPath & filename

End Sub
 

jdraw

Super Moderator
Staff member
Local time
Today, 06:11
Joined
Jan 23, 2006
Messages
15,414
Jaye,

I have a procedure whose purpose is:
To iterate all forms and report all controls by form, control name and control type.

If it is of interest to you I will post it. On each usage it will rebuild a table called tblControlsOnForms and store all forms and their controls.
 

Jaye7

Registered User.
Local time
Today, 20:11
Joined
Aug 19, 2014
Messages
205
Hi jdraw

I already have a script that lists all forms controls etc..., but I wanted to only list the loaded forms and subforms.

I can use the current one I have and look through it for the subforms, but if someone could provide a script it would save time, as some forms may have up to 10 subforms in them.
 

sneuberg

AWF VIP
Local time
Today, 03:11
Joined
Oct 17, 2014
Messages
3,506
Jaye,

I have a procedure whose purpose is:
To iterate all forms and report all controls by form, control name and control type.

If it is of interest to you I will post it. On each usage it will rebuild a table called tblControlsOnForms and store all forms and their controls.

I think the OP is look for something that does that recursively for the subforms. But I'd like to see yours regardless.
 

jdraw

Super Moderator
Staff member
Local time
Today, 06:11
Joined
Jan 23, 2006
Messages
15,414
Based on the table I created --all controls on all forms, I just wrote this routine to check for currently loaded tables and for each list the control type and control name.
It uses my table tblControlsOnForms with this design

Code:
table_name	    	field_name	 	ordinal_position	data_type	length	 
 tblControlsOnForms		form_name	       0			Text	250	
 tblControlsOnForms		control_name		1			Text	40	
 tblControlsOnForms		control_type		2			Text	25

Code:
'---------------------------------------------------------------------------------------
' Procedure : CheckLoadedFormControls
' Author    : mellon
' Date      : 23/05/2016
' Purpose   : To check for loaded forms and subforms and list all controls and control types to immediate window.
'
'Special note: You should run  GetAllFormsAndControls before running this subroutine to ensure latest info.
'---------------------------------------------------------------------------------------
'
Sub CheckLoadedFormControls()
          Dim db As DAO.Database
          Dim rs As DAO.Recordset, rs1 As DAO.Recordset
          Dim msql As String
10       On Error GoTo CheckLoadedFormControls_Error

20        msql = "Select distinct form_name from tblControlsOnForms"
30        Set db = CurrentDb
40        Set rs = db.OpenRecordset(msql, dbReadOnly)
50        Do While Not rs.EOF
60            If CurrentProject.AllForms(rs!form_name).IsLoaded Then
70                Debug.Print vbCrLf & rs!form_name & "  Is open  and contains controls "
80                Set rs1 = db.OpenRecordset("Select control_name, control_type from tblControlsOnForms where form_name ='" & rs!form_name & "'", dbReadOnly)
90                Do While Not rs1.EOF
100                   Debug.Print rs!form_name & "  " & rs1!control_Name & " " & rs1!control_type
110                   rs1.MoveNext
120               Loop
130           Else
                  'Debug.Print rs!Form_Name & "  Is not open"
140           End If
150           rs.MoveNext
160       Loop

170      On Error GoTo 0
180      Exit Sub

CheckLoadedFormControls_Error:

190       MsgBox "Error " & Err.Number & " in line " & Erl & " (" & Err.Description & ") in procedure CheckLoadedFormControls"
End Sub

Here is part of the output when I opened a few forms to test the routine.

Code:
Customer Orders  Is open  and contains controls 
Customer Orders  CompanyName TextBox  109
Customer Orders  CompanyName Label Label  100
Customer Orders  Country TextBox  109
Customer Orders  Country Label Label  100
Customer Orders  Line8 Line  102
Customer Orders  Customer Orders Subform1 SubForm  112
Customer Orders  Customer Orders Subform1 Label Label  100
Customer Orders  Customer Orders Subform2 SubForm  112
Customer Orders  Customer Orders Subform2 Label Label  100

Customer Orders Subform1  Is open  and contains controls 
Customer Orders Subform1  OrderID TextBox  109
Customer Orders Subform1  OrderID Label Label  100
Customer Orders Subform1  OrderDate TextBox  109
Customer Orders Subform1  OrderDate Label Label  100
Customer Orders Subform1  RequiredDate TextBox  109
Customer Orders Subform1  RequiredDate Label Label  100
Customer Orders Subform1  ShippedDate TextBox  109
Customer Orders Subform1  ShippedDate Label Label  100

Customer Orders Subform2  Is open  and contains controls 
Customer Orders Subform2  ProductName TextBox  109
Customer Orders Subform2  ProductName Label Label  100
Customer Orders Subform2  UnitPrice TextBox  109
Customer Orders Subform2  UnitPrice Label Label  100
Customer Orders Subform2  Quantity TextBox  109
Customer Orders Subform2  Quantity Label Label  100
Customer Orders Subform2  Discount TextBox  109
Customer Orders Subform2  Discount Label Label  100
Customer Orders Subform2  ExtendedPrice TextBox  109
Customer Orders Subform2  ExtendedPrice Label Label  100

I hope some of this helps.
Good luck.

Just saw sneuberg's post and have listed the original procedure below.

Code:
'---------------------------------------------------------------------------------------
' Procedure : GetAllFormsAndControls
' Author    : Jack
' Date      : 12/01/2013
' Purpose   : To iterate all forms and report all controls by form, control name and control type.
'
' posted question:
'http://www.accessforums.net/showthread.php?t=31409&p=154208#post154208
'---------------------------------------------------------------------------------------
'
Sub GetAllFormsAndControls()
10       On Error GoTo GetAllFormsAndControls_Error

20    On Error Resume Next
      Dim objAccObj As AccessObject
      Dim objForm As Object
      Dim strForm As String
      Dim ctl As Control
      Dim objActiveForm As Form
      '
      Dim db As DAO.Database
      Dim rs As DAO.Recordset
      Dim strSQL_Drop As String
      Dim strSQL_Create As String
       'Delete existing copy of this table
30           strSQL_Drop = "DROP TABLE tblControlsOnForms;"

40           DoCmd.RunSQL strSQL_Drop

50           strSQL_Create = "CREATE TABLE tblControlsOnForms" & _
                                "(form_name varchar(250), control_name varchar(40),control_type varchar(25));"

60           DoCmd.RunSQL strSQL_Create
70    Set db = CurrentDb
80    Set rs = db.OpenRecordset("tblControlsOnForms")
90    With rs
100   Set objForm = Application.CurrentProject
110   For Each objAccObj In objForm.AllForms


120       strForm = objAccObj.name
         ' If strForm <> "frmEATBloatV4" Then GoTo XXX 'for debugging
130       Debug.Print strForm

          
140       DoCmd.OpenForm strForm, acDesign
150       Set objActiveForm = Application.Screen.ActiveForm
160       For Each ctl In objActiveForm.Controls
170   .AddNew
180   !form_name = strForm
190           Select Case ctl.ControlType
              Case 119 ' acWebBrowser, Treeview, Calendar
200           !control_type = "Custom control"
210           Case acTabCtl
220           !control_type = "TabCtl"
230           Case acLabel
240           !control_type = "Label"
250           Case acTextBox
260           !control_type = "TextBox"
270           Case acComboBox
280           !control_type = "ComboBox"
290           Case acCheckBox
300           !control_type = "CheckBox"
310           Case acListBox
320           !control_type = "ListBox"
330           Case acOptionButton
340           !control_type = "OptionButton"
350           Case acToggleButton
360           !control_type = "ToggleButton"
370           Case acSubform
380           !control_type = "SubForm"
390           Case acCommandButton
400           !control_type = "CommandButton"
410           Case acObjectFrame
420           !control_type = "ObjectFrame"
430           Case acBoundObjectFrame
440            !control_type = "BoundObjectFrame"
450           Case acRectangle
460           !control_type = "Rectangle"
470           Case acLine
480           !control_type = "Line"
490           Case acImage
500           !control_type = "Image"
510           Case acPage
520           !control_type = "Page"
530           Case acPageBreak
540           !control_type = "PageBreak"
550           Case acOptionGroup 'some sort of frame???
560           !control_type = "Option Group"
570           End Select
580           Debug.Print " " & ctl.name & "  " & ctl.ControlType
590   !control_Name = ctl.name
600   !control_type = !control_type & "  " & ctl.ControlType  '  used to associate the control with the acControl constant
610   .Update
              
620       Next ctl
630       DoCmd.Close acForm, strForm
XXX:
640   Next objAccObj

650      On Error GoTo 0
660      Exit Sub
670   End With
GetAllFormsAndControls_Error:

680       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetAllFormsAndControls of Module AWF_Related"
End Sub
 
Last edited:

Jaye7

Registered User.
Local time
Today, 20:11
Joined
Aug 19, 2014
Messages
205
Thanks for your help, much appreciated
 

Users who are viewing this thread

Top Bottom