Page 1 of 1

How to find the control names on forms

PostPosted: Fri Oct 31, 2008 9:41 am
by Mike.Sheen
This comes up a lot - our breakout scripting will often provide you with a "FormObject" - this is the VB6 form which contains all the controls that you see.

The problem is knowing what controls are available - VB6 doesn't expose these controls publicly for things like object browsers to see.

This little script will loop through each control on the form, and write some interesting values out to a text file - there's sometimes a few hundred controls on a form, so this is preferable to a messagebox being displayed for each control.

Where you put this code fragment is up to you - I tend to just put it in the form loaded, and then close and re-open the form. Just don't forget it's there if you do this on a live system, or users may get errors opening forms because it attempts to write to the local C:\ folder.

Code: Select all
Dim WorkStr
Dim Control
Dim fso, f
Dim lTop
Dim lLeft
Dim lWidth
Dim lHeight
Dim lIndex

   FileName = "C:\ControlListing.txt"

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.CreateTextFile(FileName, True)   
   
   f.WriteLine "Control Name, Index, Top, Left, Width, Height"
   
   
   For Each Control In FormObject.Controls      
      On Error Resume Next
      lIndex = ""
      lIndex = Control.Index
      
      lTop = ""
      lTop = Control.Top
      
      lLeft = ""
      lLeft = Control.Left
      
      lWidth = ""
      lWidth = Control.Width
      
      lHeight = ""
      lHeight = Control.Height
      
      f.WriteLine Control.Name & "," & lIndex & "," & lTop & "," & lLeft & "," & lWidth & "," & lHeight
   Next

   f.Close