home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 003 / db3tips4.arc / DB3MISC < prev    next >
Encoding:
Text File  |  1987-03-03  |  15.8 KB  |  372 lines

  1.                      Recycling dBASE Records
  2.         (PC Magazine Vol 6 No 2 Jan 27, 1987 Power User)
  3.  
  4.      A system required that records be constantly appended into the
  5. "current activity" database, then later archived and deleted.  PACKing
  6. out the obsolete records was time-consuming, and records were APPENDed
  7. every day.
  8.      The solution is to reuse the unwanted records instead of throwing
  9. them away and replacing them with new ones.  When a record has been
  10. archived and is no longer wanted, REPLace all the character fields
  11. with a null character and all the numeric fields with zero.
  12.      Since the file is INDEXed, the empty key field(s) cause the
  13. recycled records to appear at the top of the file.  When entering
  14. new information, first GO TOP and check if the key field of the record
  15. is blank; if not, no reusable record is available, and a new one must
  16. then be APPENDed.
  17.  
  18. GO TOP
  19. IF <key field> = " "
  20.    * Enter new data into this recycled record
  21.   ELSE
  22.    APPEND BLANK
  23.    * Enter new data into a new record
  24. ENDIF
  25.  
  26.      Editor's Note:  If you have a high turnover in a database (and
  27. normally USE it with an INDEX), recycling records in this way will
  28. save loads of time, not only because PACKing is less frequent, but
  29. also because it's faster to REPL data than to APPEND a new record.
  30. An additional advantage is that unwanted records are dispatched
  31. immediately instead of skulking around on death row, waiting for the
  32. file to be PACKed.  Further, every time a file is PACKed, there's the
  33. danger of a power outage or some other glitch.  If you will be
  34. recycling records interactively from the dot prompt, it might be
  35. convenient to create a memory variable to do the work, SAVE it in
  36. a .MEM file, and RESTore it for use the next day.  As a simple
  37. example, suppose your file has 3 fields: Name(C,20), Course (C,6),
  38. and Year (N,4), and the file is INDEXed on Name.  Entering this
  39. statement will create the memory variable:
  40.  
  41. recycle=[REPL Name with "",Course with "",Year with 0]
  42.  
  43. When you want to blank an obsolete record, simply position on it and
  44. enter:
  45.  
  46. &recycle
  47.  
  48. Because the Name field is now empty (that is, because dBASE will
  49. evaluate Name=" " as true), the index will position this record at
  50. the top of the file where you needn't even perform a FIND to locate
  51. it: just GO TOP.
  52.  
  53. -----------------------------------------------------------------
  54.                        Debug with DOTPROM
  55.         (PC Magazine Vol 6 No 2, Jan 27, 1987 Power User)
  56.  
  57.      When debugging a menu-driven program, it's often useful to be
  58. able to step outside for a moment, simulate the "." prompt so you can
  59. issue commands interactively, and then resume program operation.  The
  60. DOTPROM.PRG works perfectly for any command that does not use a macro
  61. ("&").
  62.      For the menu choices that call DOTPROM.PRG, display a quiet little
  63. "." at the right screen margin.  When a "." is entered, the program is
  64. routed to issue the DO DOTPROM command, which clears the screen below
  65. line 16, issues a realistic-looking "." prompt, and awaits a dBASE
  66. command.
  67.      Any command you enter is picked up as a memory variable: cuscom.
  68. If you enter nothing (i..e, simply hit Enter), you return to the
  69. calling program; otherwise, dBASE issues your command and returns the
  70. next "." prompt.
  71.  
  72. ** DOTPROM.PRG
  73. @ 16,0 CLEA
  74. @ 17,79
  75. cuscom="incoming"
  76. DO WHIL cuscom>" "
  77.   ?
  78.   @ ROW(),0 SAY [Enter any dBASE command]
  79.   ?? [ (except one using "&")   <CR>=Xit]
  80.   ACCE ". " TO cuscom
  81.   IF cuscom>" "
  82.     SET TALK ON
  83.     &cuscom
  84.     SET TALK OFF
  85.   ENDI
  86. ENDDO
  87. RETU
  88.  
  89.      Editor's Note:  dBASE III Plus does provide a SUSPEND command
  90. that gives you the full power of a real "." prompt, but you must enter
  91. RESUME to restart your program.  For earlier dBASE releases, there is
  92. no alternative to DOTPROM.PRG.  With this method, you lose whatever is
  93. on the screen below line 16, but during debugging that's a small price
  94. to pay.
  95.  
  96. -----------------------------------------------------------------
  97.                       Clearing with Clipper
  98.         (PC Magazine Vol 6 No 4 Feb 24, 1987 Power User)
  99.  
  100.      The dBASE CLEAR TO command, which clears a rectangle section of
  101. the screen, is especially useful for blanking out a screen area for
  102. menus, boxes, etc.  Unfortunately, this feature is not supported by
  103. the Clipper Compiler.
  104.      However, Clipper's BOX command offers a solution to the problem.
  105. To draw a box with Clipper, you specify the coordinates of the four
  106. corners and the eight ASCII characters to be used for the four corners
  107. and four sides.  If you add an optional ninth character, Clipper will
  108. fill the inside of the box with it.  If this ninth character is
  109. CHR(32), the ASCII space, the interior of the box is cleared.
  110.      If all nine characters are specified as CHR(32), the corners and
  111. sides will be duly drawn with spaces and the ninth CHR(32) will fill
  112. the area.  Thus, for example, what in dBASE III would be:
  113.  
  114. @ 2,20 CLEAR TO 6,59
  115.  
  116. simply becomes
  117.  
  118. @ 2,20,6,59 BOX REPLICATE(CHR(32),9)
  119.  
  120. in Clipper's instructions.
  121.  
  122. -----------------------------------------------------------------
  123.                       Memo Field Diet Plan
  124.         (PC Magazine Vol 6 No 4 Feb 24, 1987 Power User)
  125.  
  126.      If you're working with a database that contains a memo field, the
  127. normal dBASE III delete/pack commands will remove a record from the
  128. DBF file, but the space used by the associated DBT memo file is not
  129. removed.  The problem is magnified when there is a lot of append/delete
  130. activity in your file.  Your DBT file gets like the attic -- stuff goes
  131. in but nothing ever comes out.
  132.      There is one way to remove the deleted memo space, however, and
  133. that is by using dBASE's COPY TO command.  This is a typical command
  134. sequence:
  135.  
  136. USE <filename>
  137. COPY TO temp FOR .NOT. DELE()
  138. ERASE <filename.dbf>
  139. ERASE <filename.dbt>
  140. RENAME temp.dbf TO <filename.dbf>
  141. RENAME temp.dbt TO <filename.dbt>
  142.  
  143. Remember that any indexes associated with your fill will have to be
  144. rebuilt.
  145.  
  146. -----------------------------------------------------------------
  147.                            Zero Zapper
  148.         (PC Magazine Vol 6 No 4 Feb 24, 1987 Power User)
  149.  
  150.      When downloading data from other systems (especially from main-
  151. frames), it is not unusual to find numeric fields packed with leading
  152. zeros.  If the imported dBASE field is numeric, this is not a major
  153. problem, though it's awkward to read numbers preceded by zeros; and,
  154. when any dBASE operation is performed on them, dBASE strips them
  155. without even being asked.
  156.      But when you want the import field to be a character type, real
  157. problems arise.  Indexes get skewed because the leading zeros don't
  158. index the same as leading spaces.  To strip leading zeros from numeric
  159. fields, use:
  160.  
  161. REPL ALL <fieldname> WITH <fieldname>
  162.  
  163.      Character fields are more complicated.  Assuming that the field
  164. being stripped is called Fld:
  165.  
  166. SET CONS OFF
  167. DO WHIL .NOT. EOF()
  168.   x=1
  169.   DO WHIL "0"=SUBS(Fld,x,1).AND.x<LEN(Fld)
  170.     x=x+1
  171.   ENDDO
  172.   REPL Fld WITH REPL(" ",x-1)+SUBS(Fld,x)
  173.   SKIP
  174. ENDDO
  175. SET CONS ON
  176.  
  177.      Editor's Note:  Because it employs the REPLicate functions, the
  178. program will work only with dBASE Plus.  If you're using an earlier
  179. dBASE version, STOR a string of spaces with the same length as your
  180. target field before entering the first DO WHIL loop.  Then, instead
  181. of the line containing the REPLicate function, substitute:
  182.  
  183. REPL Fld WITH SUBS(slug,1,x-1)+SUBS(Fld,x)
  184.  
  185. Actually, if the character field to be stripped contains only numeric
  186. characters, the leading zeros can be stripped with one command line.
  187. Assuming again that the field in question if called Fld and that its
  188. length is 8,
  189.  
  190. REPL ALL Fld WITH STR(VAL(Fld),8)
  191.  
  192. One word of caution.  You can encounter mainframe data that not only
  193. fills in leading zeros, but also substitutes an alpha character among
  194. the numerics to indicate a negative value.  Before using any of the
  195. above routines, make sure the imported numerics are all numeric --
  196. otherwise your data will be corrupted.
  197.      If you do encounter alpha characters mixed into the numeric data,
  198. the fix is a hassle and should be attempted only if you know what you
  199. are doing.  It's weird to look at a freshly imported number-type field
  200. and see an alpha here and there.  You know it can't possible be there,
  201. but it is.  You know dBASE can't possibly put up with this nonsense,
  202. and you're right again.
  203.      The steps to rescue this kind of data are:
  204. 1. Restructure your dBASE import file so that all the fields are
  205. character-type.  Add an extra 1-byte flag field for each numeric where
  206. there are alpha characters.  Then (with appropriate grumbling) reimport
  207. the raw data.
  208. 2. Mark the flag field where an alpha exists in the numeric data.  If
  209. there aren't too many records, a visual scan may suffice.  Otherwise,
  210. you'll need to write a short procedure to examine each byte, marking
  211. the flag field each time it hits something other than a number, space,
  212. or numeric sign.
  213. 3. Determine from the mainframe operator what the blasted code is --
  214. for example, that an A in the number stream means -1 and B means -2.
  215. 4. Correct the data.  You can use BROWse if there aren't too many
  216. corrections, setting a filter to see only the flagged records.  Other-
  217. wise, another routine will be required to STUFF in the replacement
  218. number.
  219. 5. Then:
  220.  
  221. REPL ALL Fld WITH -1*Fld
  222.  
  223. for flagged fields.
  224. 6. As a last step, you might ask the mainframe operator if he can
  225. format his output differently next time.  And you can judge what kind
  226. of person he is by whether he answers, "Sure!" or "Gee, I don't think
  227. we can do that."
  228.  
  229. -----------------------------------------------------------------
  230.                            Pause Plus
  231.         (PC Magazine Vol 6 No 6 Mar 31, 1987 Power User)
  232.  
  233.      The pause routine in PC Mag Vol 5 No 20 for dBASE applications is
  234. very useful; however, different messages require different amounts of
  235. attention.  Moreover, because different processors run at different
  236. speeds, a delay routine based on the execution time of a DO Loop is
  237. not very portable: a satisfactory delay for a PC running at 4.77 MHz
  238. will be almost halved by a 6-MHz AT; compile and run it on a Compaq 386
  239. and you may not even see the message.
  240.      One solution is to use the system clock for the timing of delays.
  241. The enhanced routine below passes two parameters: a message and the
  242. desired duration in seconds.  For example,
  243.  
  244. DO pause WITH message,3
  245.  
  246. The message will be displayed for 3 seconds in this case.  By using
  247. teh system clock, we have time on our side.
  248.  
  249. *** PAUSE.PRG
  250. PARA mess,dur
  251. ?? CHR(7)
  252. SET CONS OFF
  253. mess=CHR(26)+" "+mess+" "+CHR(27)
  254. @ 24,0
  255. @ 24,(80-LEN(mess))/2 GET mess
  256. CLEA GETS
  257. cntl=0
  258. DO WHIL cntl<dur
  259.   time=TIME()
  260.   DO WHIL time=TIME()
  261.   ENDDO
  262.   cntl=cntl+1
  263. ENDDO
  264. @ 24,0
  265. SET CONS ON
  266. RETURN
  267.  
  268.      Editor's Note: Unfortunately, dBASE reads the system clock only
  269. in whole seconds.  Depending on where you land in the cycle, a
  270. "1-second" pause could be as short as .01 seconds or as long as 1.99.
  271. Hence, this routine will be a little unpredictable if 1-second displays
  272. are passed.  By contrast, Nantucket's Clipper compiler offers a
  273. SECONDS() function accurate to 1/100 of a second -- just like the DOS
  274. system clock.  Perhaps future releases of dBASE will, too.
  275.  
  276. -----------------------------------------------------------------
  277.                      Enhancing dBASE Reports
  278.         (PC Magazine Vol 6 No 6 Mar 31, 1987 Power User)
  279.  
  280.      In along dBASE report, Group and Sub-Group headings don't stand
  281. out at all; they seem to get lost in the body of the report.  However,
  282. you can embed printer control characters right in the dBASE .FRM file
  283. to make your headings as bold and brassy as you please.
  284.      For example, suppose you are producing a report sorted by State
  285. and then by City, and you are using these as Gruop and Sub-Group fields
  286. on page 2 of the dBASE III Report Form Generator.  Normally, you would
  287. enter the field names, State and City, as the Group and Sub-Group
  288. expressions, and the character strings, STATE and CITY, as the Group
  289. and Sub-Group headings.
  290.      Assume further that CHR(14) turns on doublewide printing for one
  291. line, that CHR(27)+CHR(69) turns on emphasized printing, and that
  292. CHR(27)+CHR(70) cancels emphasized printing.  To make your Group
  293. information print doublewide, just enter the following as the Group
  294. expression:
  295.  
  296. CHR(14)+"STATE: "+STATE
  297.  
  298. Note that the Group heading ("STATE: ") is incorproated as a character
  299. string within the Group expression, and both will print doublewide.
  300.      To emphasize the Sub-Group similarly, enter this as the Sub-Group
  301. expression:
  302.  
  303. CHR(27)+CHR(69)+"CITY: "+CITY+CHR(27)+CHR(70)
  304.  
  305. In this case, the escape sequence, CHR(27)+CHR(70), is required to
  306. cancel emphasized printing -- otherwise, it will continue throughout
  307. the report.
  308.      dBASE's .FRM file can be used in this way to send any other
  309. printer control characters and escape sequences you like; just remember
  310. to cancel them each time.
  311.      The main Title and Column headings are more difficult because
  312. dBASE treats these strictly as character strings.  The Alt key plus
  313. numeric keypad combination seems to work.  ASCII characters 1-32 don't
  314. work, but when 128 is added to the desired ASCII number, the 128-160
  315. character range produced the desired results.
  316.      Editor's Note:  Here are a few more suggestions.  Group and Sub-
  317. Group headings will stand out even more if you insert a carriage return
  318. -- CHR(13)+CHR(10) -- at the end of the Group and/or Sub-Group
  319. expression.  You can underline the headings either by using escape
  320. sequences or by inserting a carriage return followed by a string such
  321. as: "=======".  With some experimentation, you can create an algorithm
  322. to replicate the underlining string so that it's exactly as long as
  323. the contents of your heading.
  324.      To make your Group and Sub-Group expressions more readable to
  325. yourself, you may prefer to initialize some memory variables and use
  326. these instead of the actual escape sequences.  For example,
  327.  
  328. STOR CHR(27)+CHR(45)+CHR(1) TO uline
  329.  
  330. Then, for your Group expression, enter:
  331.  
  332. uline+"STATE: "+STATE
  333.  
  334. instead of:
  335.  
  336. CHR(27)+CHR(45)+CHR(1)+"STATE: "+STATE
  337.  
  338. In some cases, you might want to enhance the contents of a whole column
  339. though this will slow your printer's output considerably.  Again, you'll
  340. have to experiment to get the desired result.
  341.      Since dBASE's HEADing option transmits escape sequences, you might
  342. substitute a HEADing in place of the regular Title.  Another work-
  343. around to enhance the main Title takes advantage of the fact that in
  344. printing the report, the optional HEADing always precedes the main
  345. Title and the Group heading always follows it.  Thus, for example, you
  346. could use the HEADing to transmit the main Title's sequence, then start
  347. the Group expression with the cancelling sequence.
  348.  
  349. -----------------------------------------------------------------
  350.                       Double Macro Function
  351.         (PC Magazine Vol 6 No 6 Mar 31, 1987 Power User)
  352.  
  353.      While the dBASE macro substitution function (the "&" symbol) is
  354. very useful in itself for shortening code, you can get even more mileage
  355. from it by concatenating two macros.  For example,
  356.  
  357. STOR "mag" TO a
  358. STOR "name" TO b
  359. STOR "PC Magazine" TO magname
  360. ? a+b       magname
  361. ? &a&b      PC Magazine
  362.  
  363. This powerful capability allows greater flexibility in array simulation
  364. and field selection routines.  Keep it in mind when naming fields for
  365. any new database systems.
  366.      Editor's Note:  In simulated arrays or other complex routines,
  367. this routine lets you replace whole blocks of commands with a little
  368. DO WHILe loop.  (But the code for double macros will make tough reading
  369. 6 months later.)  dBASE II, dBASE III version 1, and dBASE III Plus all
  370. accept both the double macro and even the &a&b syntax.
  371.  
  372.