KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Get all form names
PRODUCT: 4D | VERSION: 12 | PLATFORM: Win
Published On: June 3, 2011

The Tech Tip "How to programmatically get the forms for a given table" shows a technique for getting the forms for a table. This technique can be expanded upon to get the name of every form in the database, meaning table forms and project forms.

The following method, Get_Forms, will return an array of the form names for a table, or can return the names of all the project forms.

//Get_Forms
C_POINTER($1)
C_TEXT($2)

ARRAY TEXT($elemRef_a;0)
ARRAY TEXT($formName_a;0)
C_TEXT($elemRef;$rootRef;$tableName;$formName;$path;$xPath)
C_INTEGER($loopIndex;$namePos;$numParams)

$numParams:=Count parameters
If ($numParams>1)
 $tableName:=$2
End if

//In specify the actual name of the database
$path=Get 4D folder(Logs Folder)+"_Verify_Log.xml"
$rootRef:=DOM Parse XML source($path)

If ($numParams>1)
   //element step[7] contains the information for the table forms
 $xPath:="/verifylog/step[7]/step"
Else
   //element step[6] contains the information for the project forms
 $xPath:="/verifylog/step[6]/step"
End if

$elemRef:=DOM Find XML element($rootRef;$xPath;$elemRef_a)

For ($loopIndex;1;Size of array($elemRef_a))
 DOM GET XML ATTRIBUTE BY NAME($elemRef_a{$loopIndex};"title";$formName)

 If ($numParams>1)
  $namePos:=Position($tableName;$formName)
  If ($namePos>0)
   $formName:=Substring($formName;Length($tableName)+4)
   APPEND TO ARRAY($formName_a;$formName)
  End if
 Else
  APPEND TO ARRAY($formName_a;$formName)
 End if

End for

DOM CLOSE XML($rootRef)

COPY ARRAY($formName_a;$1->)


This Get_Forms method can then be used to get back all forms. To illustrate how to do this, here is a method that iterates through all the table forms of a database and then through all the project forms of a database.

ARRAY TEXT($names_at;0)
C_TEXT($TableName_t;$FormName_t;$folderPath_t)
C_LONGINT($tableNum_l;$totalTables_l;$page_l)
C_PICTURE($image)

$folderPath_t:=Get 4D folder(Database Folder)+"Forms"

If (Test path name($folderPath_t)#Is a directory)
 CREATE FOLDER($folderPath_t)
End if

//iterate through all table forms
$totalTables_l:=Get last table number
For ($tableNum_l;0;$totalTables_l)
 If (Is table number valid($tableNum_l))
  $TableName_t:=Table name($tableNum_l)
  Get_Forms (->$names_at;$TableName_t)
  For ($i;1;Size of array($names_at))
   $FormName_t:=$names_at{$i} // $FormName_t now has the name of the table form
     ////////////////////////////////////////////////////
     //do something
   FORM GET PROPERTIES(Table($tableNum_l)->;$FormName_t;$width;$height;$numPages_l)
   For ($page_l;1;$numPages_l)
    FORM SCREENSHOT(Table($tableNum_l)->;$FormName_t;$image;$page_l)
    If (Picture size($image)>0)
     $filePath_t:=$folderPath_t+Folder separator+$TableName_t+" "+$FormName_t+\
       " "+String($page_l)+".jpg"
     WRITE PICTURE FILE($filePath_t;$image)
    End if
   End for
    ////////////////////////////////////////////////////
  End for
 End if
End for

 //iterate through all project forms
ARRAY TEXT($names_at;0)
Get_Forms (->$names_at)

For ($i;1;Size of array($names_at))
 $FormName_t:=$names_at{$i} // $FormName_t now has the name of the project form
  ////////////////////////////////////////////////////
  //do something
 FORM GET PROPERTIES($FormName_t;$width;$height;$numPages_l)
 For ($page_l;1;$numPages_l)
  FORM SCREENSHOT($FormName_t;$image;$page_l)
  If (Picture size($image)>0)
   $filePath_t:=$folderPath_t+Folder separator+"Project "+$FormName_t+\
     " "+String($page_l)+".jpg"
   WRITE PICTURE FILE($filePath_t;$image)
  End if
 End for
   ////////////////////////////////////////////////////
End for


In this specific code sample, as each form is found, it is saved to a file as an image using the new FORM SCREENSHOT command, although any code can be inserted in its place.

Commented by Douglas Von Roeder on January 8, 2017 at 5:07 PM
Very handy method. One issue to watch out for is that the code in Get_Forms uses a hard coded value to find the nodes that contain the Table and Project Forms. The format for the verify file has changed in V13 (and perhaps again in later versions). In V13, the code for Table forms should ready "step[8]" and 'step[7]" for Project forms.