version 2004.4 (Modified)
AP Add table and fields (tableName; fieldNamesArray; fieldTypesArray; fieldLengthsArray{; listFormTemplate{; detailFormTemplate}}) Longint
| Parameter | Type | Description | |
| tableName | String | Name of table to be created | |
| fieldNamesArray | String Array | Array of field names | |
| fieldTypesArray | Longint Array | Array of field types | |
| fieldLengthsArray | Longint Array | Array of field lengths for Alpha fields | |
| (0 for non-Alpha fields) | |||
| listFormTemplate | String | Template to use for the default list form creation | |
| detailFormTemplate | String | Template to use for the default detail form creation | |
| Function result | Longint | Number of the table created or error code |
Description
The AP Add table and fields command lets you add a table and its fields in a 4th Dimension database structure.
Note: This command was created mainly for the needs of 4D ODBC Pro plug-in users.
Pass the name of the table in tableName. This name can be up to 31 characters long. It can contain any combination of letters, numbers, spaces and underlines.
The command creates as many fields in the table as there are elements in the fieldNamesArray, fieldTypesArray and fieldLengthsArray arrays. The information passed in each element is used to create the corresponding fields. The first element of the arrays will be used for the first field of the table, and so on.
Note: To create an empty table, pass empty arrays.
Pass the names of all the fields to be created in the tableName table in fieldNamesArray. Each field name can be up to 31 characters long. It must begin with a letter but you can then use any type of letter or number, as well as spaces and/or underlines.
Pass the types of each field in fieldTypesArray. You can use the following 4D constants, located in the "Field and Variable Types" theme:
| Constant | Type | Value |
| Is Alpha Field | Longint | 0 |
| Is Text | Longint | 2 |
| Is Real | Longint | 1 |
| Is Integer | Longint | 8 |
| Is Longint | Longint | 9 |
| Is Date | Longint | 4 |
| Is Time | Longint | 11 |
| Is Boolean | Longint | 6 |
| Is Picture | Longint | 3 |
| Is BLOB | Longint | 30 |
For each Alpha field created, pass a length in fieldLengthsArray. For other types of fields, this value is not necessary and you can pass 0.
The fields are created with the default properties. The table is automatically placed in the "Top Level" group.
The table is created with its two default forms. These forms are designed using the current form template. You can define the templates to use for the creation of these default forms using the optional listFormTemplate and detailFormTemplate parameters. In listFormTemplate, pass the name of the template you want to be used for the default list (output) form. In detailFormTemplate, pass the name of the template you want to be used for the default detail (input) form.
The form templates can be selected in the Form Wizard dialog box. You can get the list of form templates available in your 4D application using the 4D Pack command AP Get templates.
If it has been successfully executed, the command returns the number of the table created. This number, which is automatically assigned by 4th Dimension, corresponds to the value returned by the expression: Count tables + 1.
If the table could not be created (for example, if you pass the name of an existing table), the command returns one of the following error codes:
| Code | Description |
| -1 | Invalid parameter (blank table name for example) |
| -2 | A table with the same name already exists |
| -3 | Incorrect array type (text array instead of integer array for example) |
| -4 | Incorrect array size (the arrays are not of the same size for example) |
| -5 | Incorrect field type (type does not exist for example) |
| -6 | An error occurred when adding a field |
| -7 | An error occurred when creating the table |
Note: This command does not work when it is executed from a compiled application that has been merged with 4D Runtime Volume License.
Examples
1. This method lets you create an [Employees] table:
ARRAY STRING(31;tFieldNames;5)
ARRAY LONGINT(tFieldTypes;5)
ARRAY LONGINT(tFieldLengths;5)
C_LONGINT($ret)
`Initialization of the arrays
tFieldNames{1}:="Name"
tFieldTypes{1}:=Is Text
tFieldLengths{1}:=0
`... Fill in all the array items (2, 3, 4 and 5)
$ret:=AP Add table and fields("Employees";tFieldNames; tFieldTypes;tFieldLengths)
2. This method will create a table with two fields; default forms will use the 2nd and 3rd templates:
C_STRING(25;TableName)
C_STRING(255;$FormTempList;$FormTempDetail)
ARRAY STRING(31;FieldsNameArray;2)
ARRAY LONGINT(FieldsTypeArray;2)
ARRAY LONGINT(FieldsLengthArray;2)
ARRAY STRING(255;$ArrTemplates;0)
TableName:="NewTable"
FieldsNameArray{1}:="FirstField"
FieldsTypeArray{1}:=Is Alpha Field
FieldsLengthArray{1}:=20
FieldsNameArray{2}:="SecondField"
FieldsTypeArray{2}:=Is LongInt
FieldsLengthArray{2}:=0
$error:=AP Get templates ($ArrTemplates)
If ($error=0)
$FormTempList:=$ArrTemplates{3}
$FormTempDetail:=$ArrTemplates{2}
$NumTable:=AP Add table and fields(TableName;FieldsNameArray;FieldsTypeArray;FieldsLengthArray;
$FormTempList;$FormTempDetail)
Else
$NumTable:=AP Add table and fields(TableName;FieldsNameArray;FieldsTypeArray;FieldsLengthArray)
End if