This is the start of an ongoing Series. The subject is Xbase Programming for beginners to Xbase Programming. As time passes the series might slowly take on a new dimension, becoming perhaps something like "Red-Hot Tips for Xbase Programmers" or adopt a more imaginative title. but for the moment we'll start at the very beginning - learning the bare basics! No Carrots! I'm not about to dangle any juicy carrots. This is for those hungry souls who can see the carrots and want them. I will say in conjunction with that however, anyone who gains a solid knowledge of this subject and possesses the right sort of enthusiasm to continue learning, can progress to developing complex and extensive PC Databases with the latest breed of PC DBMS software such as Clipper 5.xx, FoxPro 2.xx, and others. The marketplace is being flooded with new products these days. Now is the time to join in. If you don't understand PC or MS DOS, or if you have just purchased your first computer and want to get started doing something on it quickly, bypass these lessons for the moment. They are not for beginners to Personal Computing. This assumes that already you understand an appreciable amount of PC terminology; you are comfortable at the keyboard and not frightened to touch something, you have at very least played around using a few Public Domain or Commercial programs; maybe attempted to teach yourself dBASE, Clipper, FoxBase or one of the others. You have a legal, installed copy of your software, including all manuals etc, and you are champing at the bit, ready to go. If you have programmed with any other language, you should sail through this easily. "C" Programmers could extend their abilities dramatically by following and learning this. Generic Xbase and the inimitable "C" language have many similarities. Before this series is complete we will have covered many of those similarities. You will see for yourself. One drawback with "C" is that you must develop all your own I/O routines and other functions, from scratch, and maintain them, or buy libraries from third party vendors. The beauty of the Xbase language is that whilst it gives you many existing, universal, easy to use commands and functions for performing the standard tasks, you have an unrestricted facility to develop and incorporate more of those clever, sophisticated and task specific routines that help to set your systems well apart from others. Nantucket took a brave line with the development of the new Clipper 5.xx series. They provided a "C" like development environment, apparently believing it would attract many "C" programmers to using their product. That particular strategy may or may not work for Nantucket, but if you are a "C" programmer, this series can open a great many doors for you. Nantucket might get hold of you yet, one day. Fox Software might get you also - the new FoxPro is just superb. Your "C" programming skills and knowledge mixed with the incredible power and flexibility of FoxPro 2.0 somewhat pales the phrase about the sky being the limit. There is no limit any more! With FoxPro 2.0 you can incorporate your own "C" and assembler routines to create systems so powerful and versatile that previously you would only have dreamed about. Developing similar systems in "C", from scratch, could take almost a lifetime. If you are NEW to all this! For those whose Personal Computing experience is limited please get out your copy of PC Update from November 1990 and study my article titled "Eight Bits of Impact", thoroughly. Then study it again, even more thoroughly. Eat it! Take some copies and eat them too! Keep one for reference though. "Eight bits of Impact" contains 12,500 words, covers nine pages and it will NOT fit into a small mailing envelope. Nor is it light reading. Study it thoroughly and understand it though, and you're on your way to much bigger and better things. For those who have ever designed and successfully implemented a new System using dBASE, FoxBASE or any other popular Xbase System then almost certainly you will learn little here during the first few lessons. My intention is to start as close to the beginning as possible without covering the alphabet. This is how we do it... There must be many ways to learn how to develop a PC database using today's popular DBMS Software; the hard way is on your own. These lessons will take you through the exercise in the most practical way I can devise. Firstly we will get part of an application up and running, as quickly as possible. The main discussion at this point will be Database Design. Then we will stop and do some light revision. We will discuss various ways that the data might be used and manipulated and examine whether our current effort would be suitable for a wider range of uses. As a sample we will use a Club Membership database because that is common and one to which everybody can relate. If you decide to make yours serve a different purpose, that's great. It will demonstrate that you are game enough to try new things. A major plus. Following that we will learn how to validate data entry to minimise operator error. We will learn how to modify the data held on file, import data, export data, print reports, extract statistics, print mailing labels, archive old records, handle errors and perform a variety of tasks that previously you would have become proficient at, only after much experience and exposure to the subject. We will not bother using things like standard "Report Writers". All work we do will involve programming to produce exactly what we require, then you can modify and experiment with them, as you desire. Using the Report Writers is a relatively straightforward exercise and after you learn everything we cover here, they will be a breeze. Your Progress As we proceed many of you will become better able to read and understand your software manuals because often a good start is all that's required. From a certain point onwards your ingenuity and resourcefulness must surely dictate the standard of your work and many of you will reach that point quickly. An introduction to the task will be sufficient. Your Questions I cannot undertake to answer great heaps of mail because put simply, I am far too committed with time. However, If I find that certain areas require a more detailed explanation, we'll cover them before we go too much further. Please let me know if you want a more detailed coverage of anything we do. The mere fact that you have trouble following or understanding something means there will be others like you out there. Don't hold back, the only stupid question is the one you don't ask. One final but extremely Important note If I make reference to any topic not already covered and you don't understand it: Where that topic is something we will learn further down the track, I will tell you by writing words like, "We'll cover that later!" But, if there is something you cannot follow, either because I explain it awkwardly or I consider you should already know about it, do not let that pass by without learning something about it. Bear in mind also that I will use examples from time to time just for the purpose of demonstrating how programmers using other languages might achieve the same result. There are two ways you can approach this .... (a) Ignore them completely, because in fact they will be incorporated for the benefit of such programmers and will not be essential reading, or (b) try to follow the samples and work out what they do. It will help your overall education to see how simple, how easy and how similar all this stuff really is. Terminology One important thing we must do is get our terminology and working environment understood. Throughout this I will use the word Xbase to mean any dBASE III PLUS compatible system. That is, any of the Xbase languages which are based on the very popular dBASE III PLUS Standard. In general, if your system is claimed to be dBASE III PLUS compatible, or better, chances are it will be suitable. Advanced Features As we come across situations where the advanced features of dBASE IV, FoxBase or FoxPro make it possible to do things a little "smarter", we will show both methods, the original and the new. That should be an interesting and educational aspect of this series. I will spend the time where appropriate, describing how the new systems have developed way past the capabilities of the original dBASE. At some point down the track we may be coding everything twice, once in the original way, then with the new approach made possible by the advanced features of the new breed of software. A few Tips Programs must be produced as ASCII text. If you type your programs into a Programmer's Text Editor you should be OK. On the other hand, if you use one of the popular commercial Word Processors, such as MS Word or Word Perfect, Make sure you save your programs as pure ASCII text. Xbase Software will not understand and will not work if your programs contain unwanted, embedded Word Processing junk. The way to test this is use DOS to display your program file on the screen after you save it. Use the following command at the DOS C:> prompt and a pure ASCII text file will always display on screen exactly as you intended to create it... MORE < FILENAME.PRG If you see garbage mixed with your ASCII text, you have in all probability saved it in Word Processor format The Program HEADER This is utterly self explanatory and I won't dwell nor bother to say more than "Every program you write should have a HEADER". You should make space as necessary from time to time immediately below the header to keep notes of changes. You might get away with cutting corners for a long time then one day, you'll sadly regret not making a note of program changes (see Figure 1). *|***************************************************** *| Program | MEMBERS.PRG *| Author | Gary Taig *| Date | August 1991 *| | Xbase Programming Tutorial - Lesson 1 *| | PC Update, October 1991 *| | *| Purpose | THIS IS A SAMPLE WORKING SYSTEM FOR ALL *| | BEGINNERS TO Xbase PROGRAMMING. *| | Suits dBASE, FoxBASE and compatible *| | software for the PC. *| | *| Calls: | Programs: *| | List program names as required & *| | describe briefly what each of those *| | programs does. *| | *| Called | Where a program is called by another, *| by... | note that in the header as well. Saves *| | much time searching later. *| | *|***************************************************** * * REVISIONS: * * Mods: (date) (name) changed this, added that etc etc * (date) (name) deleted ... routine, replaced by * this and that routine. * * All revisions completed and the REASONS for * the changes can save many hours of work at a * time when you don't have free time available. * ******************************************************* Figure 1. The Program Header.
Program Comments
Your Xbase software ignores everything from the double ampersand onwards. You should leave at least TWO blank spaces between the end of your Xbase commands and the "&&".
The problem there is that your Xbase software will look to the second line as a continuation of the first. Therefore it will NOT recognise the asterisk as the start of a comment line and it will try to perform several commands in one, becoming horribly confused in the process.
QuickBasic Programmers will easily equate the concept of records and fields with defining records and fields in Random Access Files. The QuickBasic programmer would finish up with something resembling the following .....
*************************************************** Data files can be and often are opened and closed many times during the operation of an Xbase program. An open data file is subject to corruption in the event of a power failure. A closed data file is much less vulnerable to damage or corruption. It is good practice to leave files closed at all times and open them ONLY for the purpose of writing data into them after that data has been obtained from the operator and verified. Remember this important principle when you are programming. Our DATA FILES are CREATED at the DOT PROMPT. This is the line on your screen immediately above the Status Bar in dBASE and FoxBase. If your CONFIG.DB (or CONFIG.FX) file contains a line prompt=<something else> you will NOT see the dot prompt, you will see the <something else>. Experiment with it! If you place a line in your config.db or config.fx file that reads... prompt=Command(255) where the (255) means to hold down the ALT key and press 255 on your numeric keypad - next time you start Xbase your DOT PROMPT will be replaced with the word "Command" followed by the invisible ASCII character 255 to separate it from the commands you type. You should also read the appropriate section of your Xbase manual and test all the other options able to be set in the config file. We will cover some of those options in later lessons. With FoxPro you have a command window which is essentially the same thing as the DOT PROMPT. The only difference is that when FoxPro starts up you don't see the Status Bar. If you have upgraded to FoxPro but would feel more comfortable with the Status Bar visible, type Set Status ON {enter}. It will appear, and mess up your nice FoxPro screen. This activity is all interactive. "Interactive" means, you type the command and press {enter} or perform some other action, the activity occurs. Irrespective of which System you are using, if your software starts up with something like the ASSIST in dBASE or FOX CENTRAL in FoxBASE, already activated, get out of it. Change your Xbase config file to ensure that it does NOT appear any more. We are going to learn how to get by without them, we will learn how to program our way through each task. The one thing we won't program yet is creating data files - that activity will take place at the DOT PROMPT. When necessary we can create data files from within an Xbase Program but it requires use of some of the more advanced features and I will leave that until later. We may even cover it in a separate article. Back to our discussion about the DATA itself. If you are not completely comfortable with the term "field name" refer to "Eight Bits of Impact" mentioned earlier. Field Names Names selected for fields should always be easy to understand. For example, lets say we are storing a persons preferred name on file as well first name and surname and so forth. We should select a name like PREF_NAME. We will not mistake it in future and this makes everything easier to follow. Refer Figure 4, field listing. When we name a field "PREF_NAME", to hold the person's preferred name, we would select DATA TYPE as character and we might choose a length of (say) 12 characters. These are the field's attributes - its USAGE, its NAME, its LENGTH and most important, its DATA TYPE. Your software is not affected by the USAGE. Nor is it necessary to enter that anywhere, just keep a note of it, as I have demonstrated in the listing in Figure 4 - we will describe how to do that later in this lesson. Data Types Data Types are very easy to understand and select. Data is stored on disk or in memory in a form that your Xbase Software system can best manipulate, based upon the way in which you will use it. Character Data Character is the most common data type. Character data is like what I'm creating as I type this. Anything that is straight TEXT is usually referred to in a data environment as CHARACTER. Names and addresses, product names, short comments and similar things are always stored as data type CHARACTER In a few moments we will cover "MEMO"s, another kind of Xbase character data Note that street numbers, post codes, telephone numbers and ALL OTHER data that some people THINK is numeric. Just because they are digits, should ALWAYS be stored as data type CHARACTER. Often you will come across the phrase "character string" . This is simply a string of characters, like a telephone number, or a sentence. Any group of characters appearing together make up a "character string". Numeric Data Data Type Numeric is selected and used ONLY when calculations are to be performed on the data. For instance you WILL add or subtract salary or wages, so store this as data type NUMERIC. You WILL multiply by TAX RATES, so store those as NUMERIC. Got the picture? Surely! There is a further breakdown of numeric data if you are using dBASE IV or FoxPro. The available types are N and F. Study your manual to determine how these can be selected and used to suit varying requirements. Logical Data The next data type we use in Xbase systems is LOGICAL data. Logic is a word used in many ways but here it relates to a condition and simply equates to TRUE or FALSE. A LOGICAL field in a record uses only ONE byte of space and often this proves to be very handy. We can use LOGICAL data fields to store lots of very handy information in a small space. We'll see examples of that later. Data Type Date DATES are stored specifically as type DATE to make easy the task of what is known as "date arithmetic". This is quite simple to comprehend. If we have a certain date stored in a field named "birthday" and wish to find the date of the same day TWO WEEKS EARLIER we could use the simple expression "birthday-14". That is, the date of the birthday LESS 14 days. We might use that in a program Each morning our system could run a birthday card advance reminder activity. One way we might do that is as follows... Our first line of program code might read: today = DATE() "today" is a memory variable we have just created and we have assigned to it, the value of the date on your machine. DATE( ) is an internal Xbase function which reads and returns your computer's date. (Naturally the correctness of your program results depend upon the date you have set on your computer). We will cover the words "variable" and "expression" next lesson so don't worry about these if you are not familiar with them. Our second line of program code could be .... action = today+l4 This uses date arithmetic to create yet another variable named "action" which holds the date TWO WEEKS FROM TODAY. With this we could scan through a file conducting the following check against a field named "birthday", in each record...
Before we leave data type DATE - how would you create the variable named "action" in just ONE LINE? Yes, we would use the following, more efficient expression to achieve the same result...
Length of the Field
Structure for database:D:members.dbf Number of data records: 0 Date of last update : 08/14/91 Field No. Name Type Width Dec Field Usage 1 MEMBER Character 8 Membership Number 2 SURNAME Character 20 Member's Surname 3 FIRST Character 12 First Name 4 INIT2 Character 1 Initial No.2 5 INIT3 Character 1 Initial No.3 6 PREF_NAME Character 12 Preferred Name 7 TITLE Character 4 Miss, Mrs, Mr, Ms etc 8 UNIT_FLAT Character 4 Unit or Flat No if applicable 9 FLOOR Character 2 Floor or level if applicable 10 BLDG_NAME Character 25 Name of Building 11 STR_NO Character 5 Street Number 12 STREET Character 20 Street Name 13 STR_TYPE Character 10 Avenue, Boulevard, Street etc 14 CITY_SUB Character 20 City and/or Suburb Name 15 STATE Character 3 State or County 16 POST_ZIP Character 10 Post or Zip Code 17 MAIL_AREA Character 10 Mail Code if applicable 18 COUNTRY Character 15 Country if required 19 PHCODE_AH Character 4 After Hours area code 20 PHONE_AH Character 7 After Hours telephone 21 PHCODE_BH Character 4 Business Hours Area Code 22 PHONE_BH Character 7 Business Hours telephone 23 FAX_AH Character 7 After Hours Facsimile 24 FAX_BH Character 7 Business Hours Facsimile 25 D_BIRTH Date 8 Date of Birth 26 MARRIED Logical 1 Married or single 27 JOINED Date 8 Date joined as member 28 VALID_TO Date 8 Membership Valid to (date) 29 FEE Numeric 10 2 Current membership fee 30 INTEREST Memo 10 Main Area of Interest 31 DUTY Character 20 Special Club Duties 32 ASSIST Character 25 Assistant name if applicable 33 DONATION Logical 1 Made any donations? 34 COMMENTS Memo 10 Comments from Member 35 NOTES Memo 10 Special Notes on this Member ** Total ** 330 Figure 4. Data file field listing.
The File Listing
If you wish to print the file structure at any time, open the data file and type.. Be careful when selecting your text filename because if you have an existing file named MYFILE.DOC it will be overwritten. As I said earlier, next month we will discuss creating other files to hold some of the additional data you might require stored. Then we will create a small data entry program. For those planning on importing and using an existing database, well describe how to import data from an ASCII file into a new Xbase data file. In the time you have between now and then I suggest you work out what is the best Text Editor for you to use and check that it creates pure ASCII text files. Experiment with making file listings, add notes to the documents you create and become as familiar as you possibly can with creating and modifying data files. Pay lots of attention to the type of data you will store and the maximum length of field you will need for each. Always be sure to document what you do and beware - when you use the method above to create a text file of your data file structure - what happens to an existing file that just happens to have the same name?? Have fun, Enjoy [
Note this series of articles is continued in the following issue.] |