Command CREATE FROM Foundation

Creates and opens a new database from a structure extended file.

Syntax
CREATE <cDbFile> FROM <cStructFile> ;
[ALIAS <cAlias>] ;
  [VIA <cDbeName>] ;
  [NEW]
Parameters
<cDbFile>
<cDbFile> specifies the name of the file to create. The name must contain the drive and path if they are necessary. It can be specified either as a literal file name or as a character expression in parentheses. If the file name is specified without a file extension, the default file extension predetermined by the current database engine (DBE)is used. The default file extension of the DBFDBE is ".DBF".
<cStructFile>
<cStructFile> specifies the name of the file containing the field definitions used to create the new file. The name must contain the drive and path if they are necessary. It can be specified either as a literal file name or as a character expression in parentheses. If the file name is specified without a file extension, the default extension predetermined by the current database engine (DBE) is used.
<cAlias>
The option ALIAS specifies the alias name for the work area where the new file is opened. The alias name can be specified either as literal or as a character expression in parentheses. Alias names must begin with a letter and each may be used only once. If the option ALIAS is not included, the file name is used as the alias name.
<cDbeName>
The option VIA specifies the database engine (DBE) used to manage the new file. <cDbeName> is a character string containing the name of the DBE. It can be specified as a literal or as a character expression in parentheses. By default, the new file is managed by the database engine selected using DbeSetDefault().
NEW
The option NEW specifies that the new file <cDbFile> is opened in the next free work area and this area is activated. If NEW is not included, the new file is opened in the current work area. If files are already open in the current work area, they are first closed.
Description

The command CREATE FROM uses the field definitions from the structure extended file <cStructFile> to create a new database named <cDbFile>. This new file is opened after it is created. If the option NEW is specified, the file is opened in a free work area.

A structure extended file has at least four fields. Each record contains information about the fields of a separate database file. The following table lists the fields which must be available in a valid structure extended file:

Structure of an extended file
Field name Field type Field length Decimal places
FIELD_NAME Character 10 0
FIELD_TYPE Character 1 0
FIELD_LEN Numeric 5 0
FIELD_DEC Numeric 4 0

If additional fields are included in the structure extended file, they are ignored by CREATE FROM.

A file can also be created using the function DbCreate() which requires field definitions in the form of a two dimensional array.

Contrary to dBase or CA Clipper, the length of the field FIELD_LEN is not 3 but 5. The length of a character field with more than 999 characters can be entered directly and no longer needs to be encoded using the decimal places FIELD_DEC.

The functional equivalent of CREATE FROM is DbCreateFrom().

Examples
CREATE FROM
// In the example, a simple UDF is programmed which 
// allows DBF files to be interactively created. 

#include "Inkey.ch" 

PROCEDURE Main 

   DbfNew( "Address" ) 

   Browse() 

   CLOSE 
RETURN 

PROCEDURE DbfNew( cDbFile ) 
   LOCAL cScreen, nCursor, lReadExit 

   cScreen  := SaveScreen() 
   nCursor  := SetCursor(1) 
   lReadExit:= ReadExit(.T.) 
   CLS 

   ?? "Create file with Ctrl+End, terminate with Esc" 

   CREATE Struct                   // create and open 
                                   // structure file 

   DO WHILE .T.                    // entry field 
      IF Eof()                     // specifications 
         APPEND BLANK 
      ENDIF 
      @ 10, 0 SAY "Field name   : " GET Field_name  PICTURE "@!" ; 
                                  VALID .NOT. Empty( Field_name ) 

      @ 11, 0 SAY "Field type   : " GET Field_type  PICTURE "!" ; 
                                  VALID Field_type $ "CDLMN" 

      @ 12, 0 SAY "Field length : " GET Field_len ; 
                                  VALID ValidLength() 

      @ 13, 0 SAY "Decimal places: " GET Field_dec ; 
                                   VALID ValidLength() 
      READ 

      IF LastKey() == K_UP 
         SKIP -1 
      ELSEIF LastKey() == K_DOWN  .OR. ; 
             LastKey() == K_ENTER 
         SKIP 1 
      ELSEIF LastKey() == K_CTRL_END 
         EXIT 
      ENDIF 
   ENDDO 

   CLOSE 

   IF LastKey() <> K_ESC 
      CREATE (cDbFile) FROM Struct   // create DBF file 
   ENDIF 
   ERASE Struct.dbf                  // delete structure file 

   RestScreen(,,,,cScreen) 
   SetCursor(nCursor) 
   ReadExit(lReadExit) 

RETURN 

STATIC FUNCTION ValidLength()        // validate field length 
   LOCAL lValid := .T.               // and decimal places 
   FIELD Field_name, Field_type, Field_len, Field_dec 

   DO CASE 
   CASE Field_type == "C" 
        lValid := ( Field_len > 0 .AND. Field_dec == 0 ) 
   CASE Field_type == "D" 
        Field_len  := 8 
        Field_dec  := 0 
   CASE Field_type == "L" 
        Field_len  := 1 
        Field_dec  := 0 
   CASE Field_type == "M" 
        Field_len  := 10 
        Field_dec  := 0 
   CASE Field_type == "N" 
        lValid := ( Field_len > 0 .AND. Field_len <= 15 ) .AND. ; 
                  ( Field_len > Field_dec+1 ) 
   ENDCASE 
   IF .NOT. lValid 
      Tone( 1000 ) 
   ENDIF 
RETURN lValid 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.