Command CREATE FROM Foundation
Creates and opens a new database from a structure extended file.
CREATE <cDbFile> FROM <cStructFile> ;
[ALIAS <cAlias>] ;
[VIA <cDbeName>] ;
[NEW]
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:
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.
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().
// 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
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.