Printing Control Properties

  • Thread starter Thread starter Neal Ostrander
  • Start date Start date
N

Neal Ostrander

Is there a way to loop through the controls on a form and print the details
such as Top, Left, Width, Height
Thanks
Neal
 
Dim ctlCurr As Control
Dim strOutput As String

On Error Resume Next
For Each ctlCurr In Me.Controls
strOutput = ctlCurr.Name
strOutput = strOutput & ", Top = " & ctlCurr.Top
strOutput = strOutput & ", Left = " & ctlCurr.Left
strOutput = strOutput & ", Width = " & ctlCurr.Width
strOutput = strOutput & ", Height = " & ctlCurr.Height
Debug.Print strOutput
Next ctlCurr

I did it in that convoluted way because not all controls have all four of
those properties, and I was too lazy to look up which did and which didn't!
 
Hi Neal,

Here is a quickie function that I just threw together for you...
Notes
1.) If you have lots of controls, the results printed to the Immediate
Window may be lost, if the number of results exceeds the limit of the
Immediate Window. If that happens, then you can always open a text file and
print the results to the file.

2.) The Left, Top, Height, and Width properties are printed in pixels. To
convert to inches, you can divide by 1440. You can also adjust the line of
code that prints these results, by including the conversion in the code. For
example:

Debug.Print " ", ctl.Name, vbTab, "T: " & ctl.top/1440, vbTab, "L: " &
ctl.left/1440


Option Compare Database
Option Explicit

Public Function EnumerateFormControls()
On Error GoTo ProcError

' This code enumerates controls on all forms
' It requires the user to have design permissions. You must set a reference
' to the "Microsoft DAO 3.x Object Library" (x=.51 for A97, x=.6 for A2000+).
' Written by Tom Wickerath, 10-NOV-2009.
'
' Example usage from debug window:
' EnumerateFormControls

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

Set db = CurrentDb()

' Enumerate all forms
Set ctr = db.Containers!Forms

For Each doc In ctr.Documents
Debug.Print "Form: " & doc.Name
DoCmd.openForm doc.Name, acDesign
Set frm = Forms(doc.Name)

For Each ctl In frm.Controls
Debug.Print " ", ctl.Name, vbTab, "T: " & ctl.top, vbTab, "L:
" & ctl.left
Debug.Print vbTab, vbTab, vbTab, vbTab, vbTab, vbTab, "W: " &
ctl.Width, vbTab, "H: " & ctl.Height
Next ctl

DoCmd.Close acForm, doc.Name
Debug.Print: Debug.Print
Next doc

ExitProc:
'Cleanup
Set ctr = Nothing: Set db = Nothing
Exit Function
ProcError:
Select Case Err.Number
Case 438 'Property does not exist, so ignore this error
Case Else
Debug.Print doc.Name & ": " & ctl.Name _
& " Error " & Err.Number & ": " & Err.Description
End Select
Resume Next
End Function


The code seems to throw an error with a Page Break control in a form, so I
included a statement in the error handler to print the name of the form and
control, along with the error number and description. A Page Break has Left
and Top Properties, but no Height and Width properties. I also used two
debug.print statements for each set of properties. This way, you'll get the
Top and Left properties for a page break control, before an error is raised.


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________
 
Back
Top