home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / d_b_a / 86_12 / begetter.prg next >
Text File  |  1986-11-04  |  5KB  |  107 lines

  1. * BEGETTER.PRG
  2. *  by Steve Titterud
  3. * Create any number of data files from a single file, begetter.ext,
  4. * and do it from within a program without prompting by db3+, even if
  5. * NO database files are available.
  6. * Begetter.ext can be created from ANY database file by the following:
  7. *    use <any db3+ datafile>
  8. *    copy to begetter.ext stru exte
  9. *    use begetter.ext
  10. *    zap
  11. * disp stru then gives the following:
  12. * Structure for database: C:begetter.ext
  13. * Number of data records:       0
  14. * Date of last update   : 09/16/86
  15. * Field  Field Name  Type       Width    Dec
  16. *     1  FIELD_NAME  Character     10
  17. *     2  FIELD_TYPE  Character      1
  18. *     3  FIELD_LEN   Numeric        3
  19. *     4  FIELD_DEC   Numeric        3
  20. * ** Total **                      18
  21. * This is all we need to create new files from within a program - simply
  22. * append records which represent the structure of <newfile>, then:
  23. *          CREATE <newfile> FROM begetter.ext
  24. * How do we get begetter.ext ?  We can use dBase III+'s ability to create a
  25. * catalog file, which is an ordinary .dbf. Opening a catalog file also opens
  26. * a file called catalog.cat, which documents existing catalog files.  Since
  27. * it's going to be open anyway, let's use IT (catalog.cat) in the first place.
  28. * Since even catalog.cat may not be available, we create it if absent, or
  29. * if available, use it as is.
  30. * Set environment as needs dictate, but the following will do for purposes
  31. * of demonstrating the idea here.  Let's assume you have no .dbf's of any
  32. * kind available currently:
  33. clear
  34. set talk off
  35. set safety off  && no prompts, please
  36. if .not. file('catalog.cat')  && it's not there...
  37.    @ 10,20 say "Creating catalog.cat to generate begetter.ext..."
  38.    set title off  && suppress prompt for title
  39.    set catalog to catalog.cat
  40.    set catalog to
  41.    use catalog.cat
  42.    copy to begetter.ext stru exte
  43.    use begetter.ext
  44.    zap  && clear out records so we can append records giving desired structure
  45.    erase catalog.cat  && it wasn't there in the first place
  46. else  && it IS there...
  47.    @ 10,20 say "Using catalog.cat to generate begetter.ext..."
  48.    use catalog.cat
  49.    copy to begetter.ext stru exte
  50.    use begetter.ext
  51.    zap
  52. endif
  53. * Let's use a small and simple file as an example (people.dbf), but any
  54. * legal file structure (I believe) is possible.  Note the lengths of the
  55. * logical, date, and memo fields need not be supplied.  The code here is
  56. * spelled out step by step, despite its bulk, for clarity.  A loop as in
  57. * Paul C.'s 'creatfil' takes up less space, but its action may not be as
  58. * obvious to some of the magazine's readers.
  59. * Let's suppose you want to create a file with the following structure:
  60. * Structure for database: C:people.dbf
  61. * Number of data records:       0
  62. * Date of last update   : 09/22/86
  63. * Field  Field Name  Type       Width    Dec
  64. *     1  NM1         Character     12
  65. *     2  NM2         Character     15
  66. *     3  ADDR        Character     30
  67. *     4  CITY        Character     35
  68. *     5  STATE       Character      2
  69. *     6  ZIP         Character      5
  70. *     7  OWES_AMT    Numeric        7      2
  71. *     8  CUST_SINCE  Date           8
  72. *     9  OK_CREDIT   Logical        1
  73. *    10  NOTES       Memo          10
  74. *    Total **                     126
  75. if .not. file('people.dbf')  && just in case reader has file by this name
  76.    @ 10,0
  77.    @ 10,20 say "Using begetter.ext to create people.dbf..."
  78.    ** now append records to give desired stucture for later CREATE FROM step
  79.    appe blan
  80.    repl field_name with 'nm1',field_type with 'C',field_len with 12
  81.    appe blan
  82.    repl field_name with 'nm2',field_type with 'C',field_len with 15
  83.    appe blan
  84.    repl field_name with 'addr',field_type with 'C',field_len with 30
  85.    appe blan
  86.    repl field_name with 'city',field_type with 'C',field_len with 35
  87.    appe blan
  88.    repla field_name with 'state',field_type with 'C',field_len with 2
  89.    appe blan
  90.    repl field_name with 'zip',field_type with 'C',field_len with 5
  91.    appe blan
  92.    repl field_name with 'owes_amt',field_type with 'N',field_len with 7,field_dec with 2
  93.    appe blan
  94.    repl field_name with 'cust_since',field_type with 'D'
  95.    appe blan
  96.    repl field_name with 'OK_credit',field_type with 'L'
  97.    appe blan
  98.    repl field_name with 'notes',field_type with 'M'
  99.    use  && close begetter.ext
  100.    create people.dbf from begetter.ext  && at last, a usable .dbf
  101.    erase begetter.ext
  102.    use
  103.    @ 10,0
  104.    @ 10,20 say "Done !!"
  105. endif
  106. return
  107.