The magazine of the Melbourne PC User Group
Xbase Programming - Part 2
Gary Taig
garyt@melbpc.org.au
|
 |
Last month I made too many of those "we'll cover that next month" promises as this
month has been one of the busiest for a long time. However, the code below will enable
you to enter data into your Sample Membership
Database. Read this, then key it in and test it out. [Download program in text format.]
My unexpected lateness means we have a fixed amount of space because the magazine is just about to go to bed. This sample program code is bare minimum code only. It has no frills and no specials but naturally that will make it easier to follow. Lets take it from the top.
After the PROGRAM HEADER we set up our environment so that the screen appears exactly as we want it Each setting has a special purpose but we cant cover them all now. The first one we MUST look at is SET PROCEDURE TO mbinput. This tells our Xbase System that the program contains PROCEDURES - known in some circles as SUBROUTINES. If you give your program a name other than
MBINPUT.PRG you must change the name in that statement to suit. The .PRG is not required in SET PROCEDURE TO because Xbase knows you are gluing it a PROGRAM NAME.
The next line. DO MAIN, tells Xbase to go off and DO the procedure named MAIN. Later, when it returns from MAIN, it sets the color back to normal, closes the file and dumps you at the DOT PROMPT. If you wish to end your Xbase session at that point, simply replace that particular RETURN with QUIT.
The PROCEDURE named MAIN is our main program for the moment and processing will continue in MAIN until we decide to finish our data entry session. I had to delete some lines due to space but you can easily key them in, it's all good practice.
The first thing we do in MAIN is check for the existence of our data file named MEMBERS.DBF and its associated .DBT Me. You must replace the three lines that check for the .DBT Me. Just type them exactly as I did for MEMBERS.DBF, replacing "MEMBERS.DBF" with "MEMBERS.DBT". Also, make the screen message location ® 11.10 instead of repeating the
@ 10,10 because if both files happen to be missing Xbase will paint the first message on screen row 10, then immediately overwrite it with the next message on row 10. You will not see the first message.
Notice we have created a special memory variable named "goback". That is a LOGICAL variable, set initially to false (.F.), and it will be set to true (.T.) ONLY IF one of those files is missing. Our next statement then is to check the value of "goback". The program line that says "IF GOBACK", with nothing following, actually means "If the value of the variable named GOBACK is TRUE"... then perform the following. If it's found to be NOT true, (false), program flow will continue on after the ENDIF.
Our sample system has four indexes. Only one of them remains now so you have to key in the other three just like we did above. They are as follows:
surnamdx.ndx INDEX ON surname
fullname.ndx INDEX ON sumame+first+init2
pcode.ndx INDEX ON post_zip
|
Carefully repeat the same bit of code and replace where appropriate with the names I have even you. This really is fun, isn't id All good practice.
Next we create an EMPTY date variable. That allows the operator to key in any date. More on
this in another lesson. We also create a variable named SPCE10 at this point and we store ten
blank spaces in it. See if you can identify where we use it and what it does?
Next we create more variables by declaring them
PUBLIC. It's necessary to create them here in MAIN because we cannot do that in a
PROCEDURE or some other program that might subsequently be called by MAIN.]
When MAIN calls another program and there just
happens to be some variables created within that other program, the variables are released and the
used memory is freed up again at the time of return. The variables and contents are lost.
We avoid that problem by creating the variables
now. Called programs can now use them, and modify them, but they cannot release them.
Immediately after the PUBLIC declaration we have
a line that says DO BLNKVARS. This tells Xbase to run off and find a PROCEDURE (or a separate
PROGRAM) named BLNKVARS and execute it. It will do just that if it can find one, then it will
return to here and continue processing.
If we had left out the "SET PROCEDURE TO
mbinput" back at the start, Xbase would crash at this point because it would not find BLNKVARS. It
would not know to look further down inside this program that it's currently executing.
When called, the procedure named BLNKVARS actually stores lots of BLANK SPACES to the
variables we made PUBLIC earlier.
Back to our MAIN program. We set the color for our field titles and then start processing at the top
of this page, painting words on the screen at ROW, COLUMN locations. When we say @ 1,20 we mean
at ROW ONE, COLUMN 20.
After our titles we draw some boxes, and then we place the variables full of blank spaces, on screen,
for the operator to enter some data.
Each of the variables we created and filled with varying numbers of blank spaces is placed on the
screen for data entry by use of the GET and READ.
As you issue each of those GETs the appropriate variable is painted at the designated ROW,
COLUMN position. Then, when the READ command is encountered by the program it places
the cursor at each of these in turn and accepts operator input.
Processing STOPS at the READ command while you enter all your DATA into the blank variables.
When you have completed this and you press one of the suggested key combinations to conclude the
READ the program checks to see what you pressed and branches accordingly.
Notice if you press the Ctrl-PgUp key, this has a
readkey( ) value of 290, the program runs off and does the PROCEDURE named
PUTITIN. There we add a new blank record to the file, store all the data just obtained from the operator and return.
Immediately upon return the program again runs BLNKVARS to store more blank spaces in the
variables, ready for the next GET and READ.
Unfortunately I am out of space. Play with this and practice as many variations as you can until
next month where I promise, we will cover this again, in much greater detail.
Enjoy!
[
Continued in December Part 3 article.]
**************************************************
* Program I MBINPUT.PRG
* Author I {your name)
* Date {date)
* purpose Membership Data entry.
* Part 2 - Melb PC Xbase Programming Tutorial.
* 1. set up environment
SET talk off
SET bell off
SET safety OFF
SET date british
SET status OFF
CLEAR
SET PROCEDURE TO MBINPUT
DO main
SET color TO
SET status ON
close databases
RETURN
PROCEDURE MAIN
**************
goback = .F.
* 1. Check the main file hasn't been deleted
if .NOT. file("members.dbf")
@ 10, 10 SAY "Main datafile is MISSING!"
goback = .T.
endif
** repeat this for MEMBERS.DBT (see text)
if goback
clear typeahead
wait space (9)+"Please recreate MEMBERS.DBF"'
RETURN
endif
* 2. Open file and create required indexes.
SELECT 1
USE members
** check index files recreate if missing
if .NOT. file("memberdx.ndx")
INDEX ON member TO memberdx
endif
** repeat for surnamdx.ndx fullname.ndx pcode.ndx
* make empty date variable for convenience
blnkdate = CTOD(' / / ')
spce10 = space(10) && column width (see text)
PUBLIC m_init2,m_init3,m_floor,m_state,m bldg name
PUBLIC m_title,m_unit_flat,m_str_no,m_fee
PUBLIC m_phcode_ah,m_phcode_bh,m_member,m_assist
PUBLIC m_phone ari,m_phone bh,m_fax_ah,m_fax bh,
PUBLIC m_str_type,m_post_zip,m_mail_area,m_married
PUBLIC m_first,m_pref_name,m_country,m_surname
PUBLIC m_street,m_city sub,m_donation
PUBLIC m_d_birth,m_joined,m_valid_to
DO BLNKVARS
* blank variables created - now paint input screen
SET COLOR TO rg/n
@ 1, 20 SAY "(club name) Membership Data Entry"
@ 3, 2 SAY "Member Number:"
@ 3, 29 SAY "Name:"
@ 3, 49 SAY "Surname:"
@ 4, 2 SAY "Preferred name:"
@ 4, 33 SAY "Initials:"
@ 4, 49 SAY "Title:"
@ 7, 2 SAY "Unit/Flat:"
@ 7, 19 SAY "Floor/Level:"
@ 7, 36 SAY "Building Name:"
@ 8, 2 SAY "Street No:"
@ 8, 19 SAY "Street Name:"
@ 8, 54 SAY "Street Type:"
@ 9, 2 SAY "City:"
@ 9, 36 SAY "State:"
@ 10, 2 SAY "Post Code:"
@ 10, 26 SAY "Mail Area:"
@ 10, 53 SAY "Country:"
@ 13, 5 SAY "Contact Business: Area Code:"
@ 13, 47 SAY "Phone:"
@ 13, 64 SAY "Fax:"
@ 14, 1 SAY "Information After Hours:"
@ 17, 2 SAY "Date/Birth:"
@ 17, 27 SAY "Date Joined:"
@ 17, 50 SAY "Membership Valid to"
@ 18, 2 SAY "Married Y/N:"
@ 18, 27 SAY "Membership Fee:"
@ 18, 57 SAY "Donations ?"
@ 19, 2 SAY "Duty:"
@ 19, 37 SAY "Name Assistant:"
@ 21, 2 SAY "Main Interest:"
@ 21, 27 SAY "Comments:"
@ 21, 47 SAY "Notes:"
@ 23, 2 SAY "Ctrl-PgDn Save EXIT I (esc);
discard entry I Ctrl-PgUp - Save ADD MORE"
SET COLOR TO g/n
@ 2, 0 to 5, 79
@ 6, 0 to 11, 79
@ 12, 15 to 15, 79
@ 16, 0 to 22, 79
SET COLOR TO w+/b
addmore = .T.
do while addmore
@ 3, 18 GET M_MEMBER picture "@!"
@ 3, 35 GET M_FIRST
@ 3, 58 GET M_SURNAME picture "@!"
@ 4, 18 GET M_PREF_NAME
@ 4, 43 GET M_INIT2 picture "@!"
@ 4, 46 GET M_INIT3 picture "@!"
@ 4, 58 GET M_TITLE
@ 7, 13 GET M_UNIT_FLAT
@ 7, 32 GET M_FLOOR
@ 7, 52 GET M_BLDG_NAME
@ 8, 13 GET M_STR_NO
@ 8, 32 GET M_STREET
@ 8, 67 GET M_STR_TYPE
@ 9 13 GET M_CITY_SUB picture "@!"
@ 9, 45 GET M_STATE picture "@!"
@ 10, 13 GET M_POST_ZIP picture "9999"
@ 10, 38 GET M_MAIL_AREA
@ 10, 62 GET M_COUNTRY
@ 13, 40 GET M_PHCODE_AH
@ 13, 54 GET M_PHONE_BH
@ 13, 70 GET M_FAX_BH
@ 14, 40 GET M_PHCODE_BH
@ 14, 54 GET M_PHONE_AH
@ 14, 70 GET M_FAX_AH
@ 17, 16 GET M_D_BIRTH
@ 17, 40 GET M_JOINED
@ 17, 70 GET M_VALID_TO
@ 18, 16 GET M_MARRIED picture "Y"
@ 18, 44 GET M_FEE picture "999999.99"
@ 18, 70 GET M_DONATION picture "Y"
@ 19, 16 GET M_DUTY
@ 19, 53 GET M_ASSIST
READ
DO CASE
case readkey() = 12 .OR. readkey() = 268
RETURN
case readkey () = 14 .OR. readkey () = 270
RETURN
case readkey() = 290
DO PUTITIN
DO BLNKVARS
case readkey() = 34
LOOP
case readkey() = 291
DO PUTITIN
addmore = .F.
case readkey() = 35
addmore = .F.
OTHERWISE
loop
ENDCASE
ENDDO
RETURN
PROCEDURE PUTITIN
APPEND BLANK
REPLACE MEMBERS->MEMBER WITH m_member
REPLACE MEMBERS->FIRST WITH m_first
REPLACE MEMBERS->SURNAME WITH m_surname
REPLACE MEMBERS->PREF NAME WITH m_pref_name
REPLACE MEMBERS->INIT2 WITH m_init2
REPLACE MEMBERS->INIT3 WITH m_init3
REPLACE MEMBERS->TITLE WITH m_title
REPLACE MEMBERS->UNIT FLAT WITH m_unit_flat
REPLACE MEMBERS->FLOOR WITH m_floor
REPLACE MEMBERS->BLDG NAME WITH m_bldg _ame
REPLACE MEMBERS->STR NO WITH m_str_no
REPLACE MEMBERS->STREET WITH m_street
REPLACE MEMBERS->STR TYPE WITH m_str_type
REPLACE MEMBERS->CITY SUB WITH m_city_sub
REPLACE MEMBERS->STAT WITH m_state
REPLACE MEMBERS->POST ZIP WITH m_post_zip
REPLACE MEMBERS->MAIL AREA WITH m_mail_area
REPLACE MEMBERS->COUNTRY WITH m_country
REPLACE MEMBERS->PHCODE AH WITH m_phcode_ah
REPLACE MEMBERS->PHONE BH WITH m_phone_bh
REPLACE MEMBERS->FAX BH WITH m_fax_bh
REPLACE MEMBERS->PHCODE BH WITH m_phcode_bh
REPLACE MEMBERS->PHONE AH WITH m_phone_ah
REPLACE MEMBERS->FAX AH WITH m_fax_ah
REPLACE MEMBERS->D-BIRTH WITH m_d_birth
REPLACE MEMBERS->JOINED WITH m_joined
REPLACE MEMBERS->VALID TO WITH m_valid_to
REPLACE MEMBERS->MARRIED WITH m_married
REPLACE MEMBERS->FEE WITH m_fee
REPLACE MEMBERS->DONATION WITH m_donation
REPLACE MEMBERS->DUTY WITH m_duty
REPLACE MEMBERS->ASSIST WITH m_assist
RETURN
PROCEDURE BLNKVARS
STORE space(1) TO m_init2,m_init3
STORE space(2) TO m_floor
STORE space(3) TO m_state
STORE space(4) TO m_title,m_unit_flat
STORE space(4) TO m_phcode ah,m_phcode bh
STORE space(5) TO m_str_no
STORE space(7) TO m_phone_ah,m-phone bh
STORE space(7) TO m_fax_ah,m_fax bh
STORE space(8) TO m_member
STORE spce10 TO m_str_type,m_post_zip
STORE spce10 TO m_mail_area
STORE space(12) TO m_first,m_pref name
STORE space(15) TO m_country
STORE space(20) TO m_surname,m_street
STORE space(20) TO m_city sub,m_duty
STORE space(25) TO m_bldg name,m_assist
STORE .F. to m_married,m_donation
STORE blnkdate to m_d_birth,m_joined
STORE blnkdate to m_valid_to
STORE 0 to m_fee
RETURN |
Reprinted from the November 1991 issue of PC Update, the magazine of Melbourne PC User Group, Australia
|