FOXPRO (Aj)
FOXPRO (Aj)
FOXPRO (Aj)
INTRODUCTION TO DATABASE
What is a database ?
A database is an organised collection of related information. Any unorganised
information is just a pile or dump, and it cannot be called a database. A database serves as an
information base and can use it for:
? Retrieving desired information
? Taking meaningful decision
? Re-organising information
? Processing information
Data Structure
It is a term used to refer the logical structuring of data. So, in a database, information is
stored in records (rows) and fields (column).
RECORD
Collection of data or related facts is a Record. Each data makes up fields of the
database. Each field has different field name, type and size. The relevant information is stored
in the fields.
File Creation
Modifying a file structure
Adding records
Deleting records
Selecting records
Printing reports
File Creation :
Creating a database file means defining the data to be maintained and organising the
database to accommodate the data keeping requirements to be met by the system.
Once the file is designed, data can be added to it.
2.
Modifying a File
Modifying a file means changing its structure. To add or delete fields to the records in a
file or change the width of one or more fields.
2
3.
PROGRAMMING IN FOXPRO
Adding Records
To add records in a file, you enter data in the fields, record by record.
4.
Deleting Records
Unnecessary records may be deleted or removed from the data file.
5.
Selecting Records
The ability to select just those records which meet certain criteria in an essential
capability of any DBMS.
6.
Printing Reports
Most data management operation require the production of printed reports that show the
status of data in the file in both detailed and summary form.
Introduction to FoxPro
FoxPro is one of the leading Data Base Management System (DBMS) software for PC.
This is an enhanced and updated version of the FoxBASE+ software.
One of the first DBMS for the PC was developed by ASHTON-TATE in 1979-80 that was
called dBASE II. Then that company introduced the updated version of dBASE II in 1984 that
was called dBASE III. Soon, dBASE III became the industry standard. Later, ASHTON-TATE
released the enhanced and updated version of dBASE III, i.e. dBASE III PLUS.
To get a share of the expanding DBMS market for the PC segment, Fox Software Inc.
came out with FoxBASE+ that was almost compatible with dBASE III PLUS. Then it was
updated and the new software was called FoxPro. Gradually, new versions of FoxPro were
released to make it more powerful and user friendly. However, this has also widened the gap
between dBASE and FoxPro, and now they are only partly compatible.
FoxPro is also called the Relational Database Management System (RDBMS). It helps
you to design database files as per the requirements and as per the specified format. It also
helps you enter and manage data in database files. FoxPro also helps you to edit, view, change
data in the database through simple built-in commands. Once the database is ready, you can
use it to retrieve selected information from it. The retrieved information can be displayed or
printed as per the desired report format. The best part is that the data stored in the database is
flexible. i.e. you can change / modify the contenets as well as the structure of a database any
number of times. FoxPro has many other powerful commands for managing multiple database
files, protection and documentation of files.
PROGRAMMING IN FOXPRO
Several versions of FoxPro are available for use. Earlier versions of FoxPro were DOS
based, but since version 2.5, FoxPro can run under four different operating platforms DOS,
Windows, UNIX and Macintonsh. Because of differences in these operating environments,
there are a few differences among these four versions of FoxPro 2.5.
In order to reduce the gap between dBASE IV and FoxPro, the new release of FoxPro
FoxPro 2.6 is a definite step in that direction. With FoxPro 2.6 you can directly use all dBASE
IV database, screen, report and label files. FoxPro 2.6 will automatically convert dBASE IV files
to FoxPro format, whenever necessary.
Having identified all the data items, you must decide how many database files you need
and which items belong in each database. When you actually set up the file structures, you
must assign each field a name and specify the data type, field length, and for numeric fields, the
number of decimal places. If you wish, you can also select the fields that will serve as the basis
for indexes; several of the necessary index keys are usually evident from the outset, but you will
undoubtedly add more indexes as the system evolves.
The first-and, for a beginner, the most difficult-step is deciding how to group the data
items into database files. The file structures should be setup to support the overall goals for any
database application :
?
?
?
?
?
?
?
?
PROGRAMMING IN FOXPRO
?
?
?
While defining the structure, you have to specify the following details:
A unique field name for each field, i.e. each field must have a name that is not being used
by any other field.
Field type, i.e. the type of data, such as numeric or character to be stored in each field.
Width of each field. For numeric field, also specify the number of decimal digits.
The fields in each database file should store items of information that belong together
logically. A FoxPro 2.5 or 2.6 database can contain up to 255 fields totaling up to 65,500
characters. In the Extended version, you can open 255 database simultaneously, and in the
Standard version, you can open 25. Because you can open multiple databases and link them
based on common fields, you can avoid a great deal of redundancy by eliminating from each
database most items of information that you can look up in other files. For example, there is no
need to store a persons name in both the Names File and the Supplementary Information File,
and there is certainly no need to store the name in Contacts File or Transaction File.
PROGRAMMING IN FOXPRO
Numeric, float These fields can hold only numeric data. Thus you can only store digits 0 to 9,
decimal point (.) and the optional leading plus (+) or minus (-) sign. The maximum length is
20 digits, including up to 18 decimal places. FoxPro stores more significant digits for the
float type data as compared to the numeric data. Therefore, the float type data (field) is
better for scientific calculations. However, since FoxPro is generally used for business
applications, the float fields are not commonly used.
Date - This type accepts only valid calendar dates. FoxPro stores the month, day, and fourdigit year (although most date display formats show the year as a two-digit number).
Logical This type accepts only the logical values true(yes), entered as T, t, Y, or y, and
false (no), expressed as F, f, N, or n. Logical fields are particularly useful for storing
information that has only two states.
Memo This type accepts an unlimited amount of data of any kind (subject to memory
and disk space limitations), usually free-form text. The data to be stored in the memo field is
not stored in the database file but in an auxiliary database file with the same name but with
the extension name .fpt. FoxPro allots a ten byte space in the main database file to store
the location of the memo data in the auxiliary database file. Memo fields are useful in
reducing the size of a database file. For entering data into the Memo filed, press CTRL +
Home (or) CTRL + PgDn.
PROGRAMMING IN FOXPRO
?
They support the fast, efficient record selection operations carried out by FoxPros
Rushmore query optimizer.
Menu popup
Title bar
Menu bar
Window
Scroll bars
PROGRAMMING IN FOXPRO
When you start up FoxPro, only one window, the Command window, is active. All other
editing activities also take place in windows, and you may have as many windows open at once
as you wish. Whenever you start up the FoxPro text editor, invoke any of the form design tools (
such as the Screen Builder, Menu Builder, or Report Writer), or type a command that requires a
window, such as BROWSE, FoxPro automatically opens a new window for this activity, leaving
all other active windows open and placing the new window on top.
PROGRAMMING IN FOXPRO
The following command displays current date at a specified column (say 20).
? DATE( ) AT 20
The ?? Command :
The ?? command is a minor variation of the ? command. Whereas the ? command
displays information in the next row, ?? displays it in the same row. In other word, FoxPro
issues a line-feed before displaying data with ?, whereas it does not issue line-feed with ??.
??
NAME
??
LACALITY
??
CITY
Controlling printer with ??? :
Regardless of the status of SET PRINTER, the ??? command sends the codes to the
printer without affecting the print head position. The following command sends the codes to the
printer without affecting the print head position. The following command sends the code for
selecting the condensed printing mode on Epson and compatible printers.
???
CHR(015)
The ASCII code for selecting the condensed printing mode is 015. The CHR function
returns (passes) the ASCII code 015 to the ??? command, that sends it to the printer.
Using Operators :
Operators are symbol that represent specialized operations that FoxPro can carry out.
All the FoxPro operators as they are used with data of various types you are familiar with the
arithmetic operators and the symbols used to represent them: addition (+), subtraction (-),
multiplication (*), and division (/). FoxPro can also raise a number to a power, using either ^ or **
as the exponentiation operator. For example, you could express 2 cubed (2 raised to the third
power) as
2 ** 3
or
2 ^3
You can use the modulus operator to compute the remainder that results from dividing
one number by another. This operator is handy for carrying out unit-of-measure conversions.
For example, to convert 87 inches to feet and inches , you would divide the number of
inches(87) by 12; the number of whole feet is the integer portion of the result, and the number of
inches is the remainder.
PROGRAMMING IN FOXPRO
You can use the INT function, which is described later to obtain the integer portion of the
result of the division, and use the modulus operator to compute the remainder, in an expression
like the following:
87 % 12
The notation of carrying out calculations on character, date, or logical data may at first
seem foreign, but the fact that FoxPro uses some of the familiar arithmetic operator symbols to
represent these calculations should help to alleviate your initial confusion. In most cases these
symbols represent operations that are roughly equivalent to their mathematical counterparts.
For example, adding two character strings together means combining them end-to-end, with no
intervening spaces. For a particular record in the Names File, the expression
CITY + STATE + ZIP
might evaluate to
SAN FRANCISCO
CA94101
The spaces between San Francisco and CA result from the fact that the CITY field is
longer (20 Characters) than its contents (13 characters). If you use the - operator instead of +,
FoxPro removes the trailing blanks from all but the last character string, and combines all the
blank spaces at the end of the resulting string. You can use this operator when you must
produce character strings of a given length but with no embedded blank spaces. For the same
record used in the previous example, the expression
CITY - STATE - ZIP
Would evaluate to
SAN FRANCISCOCA94101
Note that the result of evaluating this last expression includes seven trailing spaces, derived
from the CITY field
The FoxPro Operators
Operator
Data Types
^ or **
*
/
%
+
=
==
N, F
N, F
N, F
N, F
N, F
C
D+N
N, F
C
D-N
C, M, N, F, D
C, M
Meaning
Exponentiation
Multiplication
Division
modulus
Addition
Concatenation
Adds days to a date
Subtraction
Concatenation, embedded blanks combined
at end
Subtracts day from a date
Equal to
Equal to and equal in length
10
PROGRAMMING IN FOXPRO
Operator
Data Types
Meaning
<>, #, or !=
>
>=
<
<=
$
NOT or .NOT.
AND or .AND.
OR or .OR.
C, M, N, F, D
C, M, N, F, D
C, M, N, F, D
C, M, N, F, D
C, M, N, F, D
C, M
L
L
L
Not equal to
Greater than
Greater than or equal to
Less than
Less than or equal to
Substring (contained within)
.T. if following expressions .F.
.T. if both expressions are .T.
.T. if either expression is .T.
When you combine two conditions with AND, the resulting condition is .T. only if both the
component conditions are true. For example, the following display records in the Names Field in
which the state is California and the last contact date was January 1,1992:
LIST FOR STATE = CAOR LASTCONT > {01/01/92}
When you combine two conditions with OR, the resulting condition is .T. if either of the
component conditions is true. For example, the following selections all people in California,
regardless of their last contact date, together with all those whose last contact date was after
January 1, 1992, regardless of where they live:
LIST FOR STATE = CAOR LASTCOUNT >{01/01/92}
The NOT operator is used to negate a condition - that is, to select all records in which
the condition is not true. For example, the following command selects records in which the state
is either California nor Oregon.
LIST FOR STATE <> CAANDF STATE <> OR
You can also use the NOT operator to select records in which a logical field has the
value .F. for example, you can display all the records from the Names File in which the MAIL
field is .F. with the following :
LIST FOR NOT MAIL
*****
PROGRAMMING IN FOXPRO
11
FoxPro Functions
A function is a named operator that adds to the FoxPro language the ability to perform
more specialized calculations or operations than you can with explicit expressions constructed
using only the operators. Each function requires a specific type of input performs a particular
transformation on this input, and yields a result a specific type of output.
Every FoxPro function returns as output a single quantity of a particular data type, and
you can use a reference to the function anywhere a quantity of that data type is permitted. A
function reference consists of the function name followed by a pair of parentheses that enclose
the input. A function may require one input, more than one, or none, but even when you do not
provide input, you always include the parentheses to differentiate the function reference from a
field or memory variable with the same name.
Mathematical Functions
SQRT( )
SQRT( ) returns the square root of a positive numeric expression. Enclose the numeric
expression within parenthesis.
Examples:
? SQRT(49)
7.00
? SQRT(100)
10.00
LOG( ) and LOG10( )
FoxPro has two functions to calculate logarithm of a positive numeric expression. LOG()
returns the natural logarithm (to base e) and LOG 10( ) returns the common logarithm (to base
10)
Examples:
? LOG(100)
4.61
? LOG10(100)
2.00
12
PROGRAMMING IN FOXPRO
PROGRAMMING IN FOXPRO
13
MIN( )
The MIN( ) function returns the expression with the lowest value. You can use two or
more expressions with MIN( ). The expressions can be of numeric, character or date type.
However, all expressions used with MIN( ) must be of the same type.
Examples:
? MIN(12, 15, 9)
9
? MIN(CA, AZ, BA, ZD)
AZ
MAX( )
The MAX( ) function is similar to MIN( ), except that it returns the expression with the
highest (maximum) value. You can use two or more expressions of numeric, character or date
type with MAX( ).
Examples:
? MAX(12, 15, 9)
15
? MAX(CA, AZ, BA, ZD)
ZD
BETWEEN( )
BETWEEN( ) determines if the value of an expression lies between the values of two
other expressions of the same data type. The expressions may be of numeric, character or
date type.
Example:
X = DATE( )
Y = DATE( ) + 1
Z = DATE( ) - 1
? BETWEEN(X, Z, Y)
.T.
BETWEEN( ) checks if the date variable X is between Y and Z. Since, X (current date)
lies between Y (tomorrows date) and Z (yesterdays date), the command displays .T.
MOD( ) and %
The MOD( ) function returns the remainder from a division of two numbers.
Example:
? MOD( 1965, 100 )
65
You can also use %, the modulus operator to get the remainder from a division of two
numeric expressions.
14
PROGRAMMING IN FOXPRO
Example:
? 1965 % 100
65
EXP( )
The EXP( ) function returns the value of en.
Example:
? EXP(1)
2.72
SIGN( )
The SIGN( ) function returns the numeric value of 1, -1 or 0 depending on the sign of a
numeric expression. It returns 1 if the numeric expression is positive; -1 if it is negative; and 0 if
it is equal to 0.
Examples:
STORE 0 to MARKS
? SIGN(MARKS)
0
? SIGN(20.3)
1
? SIGN(-12.34)
-1
LEN( )
The LEN( ) function returns the length of a specified character expression. Example:
CH = RAJAN
? LEN(CH)
5
String Functions
TRIM( )
TRIM( ) returns the specified character expression after removing all trailing blanks. You
can use this function in expressions that combine several character string on a report or
screen display if you wish to eliminate unsightly embedded spaces.
Example:
? TRIM( RTI
)
RTI
PROGRAMMING IN FOXPRO
15
UPPER( )
UPPER( ) returns the specified character expression in upper-case.
Example:
? UPPER(Regional)
REGIONAL
LOWER( )
LOWER( ) returns the specified character expression in lower-case.
Example:
? LOWER(Regional)
regional
PROPER( )
PROPER( ) returns the specified character expression with the first letter of each word in
uppercase and the remaining characters in lowercase.
Example:
? PROPER(ORDNANCE FACTORY)
Ordnance Factory
SUBSTR( )
SUBSTR( ) returns a specified number of characters from the specified character
expression or memo field.
Example:
? SUBSTR(ORDNANCE FACTORY, 10, 4)
FACT
STR( )
STR function is used to convert a number to a character string, and VAL to make the
opposite conversion. The command accepts three inputs- the number to be converted, the total
length of the resulting character string, and the number of decimal places.
Example:
? STR(1234.12, 6, 1)
1234.1
16
PROGRAMMING IN FOXPRO
DATE( )
The DATE( ) function returns the current system date.
Example:
? DATE( )
21 / 04 / 03
DAY( )
The DAY( ) function returns the numeric day value from a date expression. Example:
STORE DATE( ) TO CURRENT
21/04/03
? DAY(CURRENT)
21
MONTH( )
The MONTH( ) function returns the numeric value of the month from any date
expression. Example:
? MONTH(CURRENT)
4
CMONTH( )
The CMONTH( ) function returns the name of month (as character expression) from any
date expression.
Example:
? CMONTH(CURRENT)
April
YEAR( )
The YEAR( ) function returns the numeric year from any date expression. It always
returns the year with the century, regardless of the setting (ON or OFF) of SET CENTURY.
You can use a date expression (field, variable etc.) with YEAR( ). You can also use a literal
date string with YEAR( ).
Example:
? YEAR(CURRENT)
2003
? YEAR({12/06/95})
1995
PROGRAMMING IN FOXPRO
17
DTOC( )
The DTOC( ) (Date to Character) function is an important function that converts a date
type data to characters.
Example:
STORE DTOC(CURRENT) TO TODAY
21/04/03
CTOD( )
The CTOD( ) (Character to Date) is the reverse of DTOC( ). That is, it converts a date
stored as character data to a date-type data. An interesting feature is that it does not allow you
to store invalid dates. The following commands first create a character variable CH_VAR with
09/08/95and then convert it to a date variable DATE_VAR.
Example:
CH_VAR = 09/08/95
STORE CTOD(CH_VAR) TO DATE_VAR
DOW( )
The DOW( ) (Day Of Week) function returns a number corresponding to the day of week
from the specified date-type expression. The day 1 of the week is Sunday.
Example:
? DOW(CURRENT)
2
CDOW( )
The CDOW( ) (Character Day Of Week) function returns the name of a day from the
specified date-type expression.
Example:
? CDOW(CURRENT)
Monday
Precedence of Evaluation
When you construct a complex expression, you must take into account the order in
which FoxPro evaluates the components; it does not, as you might guess, simply proceed
from left to right. The rules that govern the order in which an expression is evaluated depend
on the precedence of the various operators the expression contains. FoxPro evaluates
expressions in the following order:
18
PROGRAMMING IN FOXPRO
1. Expressions enclosed in parentheses, starting with the innermost set of parentheses, and
adhering to the rules of precedence within each set
2. Functions
3. Mathematical operations, in the following order:
a. Exponentiation
b. Multiplication and division, from left to right
c. Modulus calculations
d. Addition and subtraction, from left to right
4. Character string operations
5. Relational operators, from left to right
6. Logical operators, in the following order:
a. NOT
b. AND
c. OR
To adhere to this sequence of evaluation, FoxPro may make two or more passes
through a complex expression in order to produce the final output.
PROGRAMMING IN FOXPRO
19
Field Type
Width
FNAME
LNAME
ADDRESS
CITY
STATE
ZIP
Char
Char
Char
Char
Char
Numeric
15
15
20
15
15
6
There are two ways to create a program file. You can use the FoxPro menu system (through
File
New
Program) or by using MODIFY COMMAND. The command to create a
new command file, or edit an existing one, is MODIFY COMMAND plus the name of the
command file. The command will be as follows:
MODIFY COMMAND LABELS ?
FoxPro displays the prompt
EDIT: LABELS.PRG
and gives a blank screen on which to write the command file. Make sure you are typed it as per
syntax below.
? TRIM(FNAME),LNAME ?
? ADDRESS ?
? TRIM(CITY)+ , + STATE + STR(ZIP) ?
If you make errors go through the program and make changes. To save the file press ^W or
^End.
Now you have written a command file and stored it on the disk as LABELS.PRG.
20
PROGRAMMING IN FOXPRO
PROGRAMMING IN FOXPRO
21
Ruth Doe
1142 J.St.
Los Angeles, Ca 91234
Record no.2
You can get rid of the record numbers by asking FoxPro to SET TALK OFF.
USE MAIL ?
SET TALK OFF ?
DO LABELS ?
Here it will display all mailing labels without record no.
To add a blank line between the mailing labels use a ? command in the program.
GO TOP ?
the command returns FoxPro to the top of the database.
To put comments in FoxPro programs, start the line with an asterisk (*).
To add a comment to any command, type && at the end and then enter the desired
comment.
Keys
Function
Modify structure
CTRL + W
CTRL + PgUp
CTRL + PgDn
CTRL + I
Save changes
To move a field up
To move a field down
Insert field
Browse window
CTRL + N
CTRL + T
Append Record
Delete Record
Modify command file
CTRL + N
CTRL + V
CTRL + Y
CTRL + W
22
PROGRAMMING IN FOXPRO
Making Decisions
Using FoxPro in an Interactive Manner
If the command file is going to print certain labels, it needs to know which labels you
want. That is, it needs to ask you which labels you want to print. You can make your command
files ask questions with the ACCEPT and INPUT commands. When you use either of these
commands, you enclose the question to be asked in apostrophes. You also need to provide a
memory variable in which to store the answer to the question. To make the memory variable
accessible outside of the command file, you need to define it as PUBLIC. Lets try out the
ACCEPT command with a command file called TEST.
MODIFY COMMAND TEST ?
When the blank screen appears, type these lines :
PUBLIC NAME
ACCEPT What is your names?TO NAME
and save it with CTRL+W. Then DO TEST. Youll see the prompt on-screen:
What is your name? _
FoxPro will leave the prompt on the screen until you type an answer. You can type any answer.
FRED ASTAIRE ?
After you press Return, the dot prompt appears because the program is over. If you type the
following in dot prompt,
? NAME ?
youll see
FRED ASTAIRE
If you were to DISPLAY MEMORY now, youd see that you have a memory variable called
NAME, of the Character type, with the contents FRED ASTAIRE. So youve been able to have
the command file ask a question, wait for an answer, and remember the answer by storing it to
a memory variable.
The INPUT command is very similar to the ACCEPT command, except that it is used
when the answer to the question is a number. For example, try making a command file called
TEST2 that looks like this:
PUBLIC ANSWER
INPUT Enter a numberto ANSWER
Save it. Then DO TEST2, and the request appears:
Enter a number _
Type any number, say 888, and press Return. The dot prompt reappears. Next type
? ANSWER ?
Youll see
888
PROGRAMMING IN FOXPRO
23
When you DISPLAY MEMORY, you see that you have a memory variable called ANSWER, and
it is Numeric.
1.
2.
3.
4.
The @ command is one of the most powerful and flexible commands that can be used
for several purposes, such as
To display the desired data in a required format
To input data in a field or variable
To draw or clear box and lines
To create custom screen format files, and so on.
24
PROGRAMMING IN FOXPRO
Notice that Sudha Sharma is displayed in reverse video (or different colour) and you cannot
enter the new name yet. To enter data in GET variable(s), you have to use the READ
command.
? Enter READ in the command window.
PROGRAMMING IN FOXPRO
Code
X
Y
!
$
*
.
,
25
26
PROGRAMMING IN FOXPRO
Code
A
B
E
T
!
^
$
Code
A
L
N
X
9
#
!
$
*
.
,
The WAIT command presents a message and waits for the user to press any key. The
variations of the WAIT command are explained below.
------------------------------------------------------------------------------------------------------------------------------Variation
Effects
------------------------------------------------------------------------------------------------------------------------------WAIT
When used as a single command, WAIT presents the message,
Press any key to continue,and waits for a keystroke. It does not
store the resulting keystroke.
WAIT TO <MemVar>
When used with TO and a memory variable, WAIT will store the
resulting keystroke to a memory variable.
WAIT message
You can use your own messages with the WAIT command,
enclosed in quotation marks, to replace the default Press any key
to continuemessage.
-------------------------------------------------------------------------------------------------------------------------------
PROGRAMMING IN FOXPRO
27
Lets try a small command file to test the @ and WAIT commands. Type
MODIFY COMMAND TEST@ ? at the dot prompt. Type the following program.
***** Program to test @ and WAIT commands
***** First set up variables.
ANYNUMBER = 0
ANYWORD = SPACE(254)
ANYDATE = DATE()
***** Next, use @ SAY, GET to read in new data
CLEAR
@ 2,5 SAY Enter a Number GET ANYNUMBER
@ 5,5 SAY Enter a sentence or two GET ANYWORD
@ 10,5 SAY Enter a dateGET ANYDATE
READ
***** use WAIT to pause before memory display
@ 15,1 WAIT Press any key to see memoryTO KEYPRESS
DISPLAY MEMORY
Save the command file and then enter DO TEST@ ? to execute the program.
28
PROGRAMMING IN FOXPRO
PROGRAMMING IN FOXPRO
29
30
PROGRAMMING IN FOXPRO
Please notice the following program.
CLEAR
? 1. Add new names
? 2. Print Labels
? 3. Edit a record
? 4. Exit
INPUT Enter choiceto CHOICE
DO CASE
CASE CHOICE = 1
APPEND
CASE CHOICE = 2
DO LABELS
CASE CHOICE = 3
BROWSE
OTHERWISE
QUIT
ENDCASE
Inside the DO CASE clause, the program decides what to do based upon the value of
CHOICE. If CHOICE = 1, the program appends. If CHOICE = 2, the program executes LABEL
program. If CHOICE = 3, the program goes into BROWSE mode. If none of these situations
occurs (OTHERWISE), the program quits.
PROGRAMMING IN FOXPRO
31
Macro Substitution
The Macro substitution is a useful feature that makes the programs flexible and
interactive. The macro allows you to use a variable name for a file name, field name, etc. in a
program file. A macro is similar to a normal character variable, except that it has the
ampersand character (&) in front of it, e.g. &NAME, &XYZ, &INVOICE etc.
e.g.
NEWFILE = DEPOSIT
USE &NEWFILE
When these commands are used in a program file, the first command creates a character
variable NEWFILE and stores DEPOSIT in it. When it executes the USE command, it
substitutes &NEWFILE with its contents. That is, USE &NEWFILE becomes USE DEPOSIT.
Accordingly, FoxPro opens the DEPOSIT database file.
e.g.
To allow macro feature this flexibility, you must modify the LABELS command file as
below :
***** Mailing Labels Program
SET TALK OFF
CLEAR
ACCEPT Search on what field ? TO FLD
ACCEPT Look for what &FLD ? TO COND
ACCEPT Shall I send labels to the printer ? (Y/N)TO YN
IF UPPER(YN) = Y
SET PRINT ON
ENDIF
GO TOP
DO WHILE .NOT. EOF( )
IF &FLD = &COND
? TRIM(FNAME), LNAME
? ADDRESS
? TRIM(CITY) + , + STATE + STR(ZIP)
?
?
ENDIF
SKIP
ENDDO
SET PRINT OFF
RETURN
When you run the above program, screen clears and displays
Search on what field ?
32
PROGRAMMING IN FOXPRO
Lets answer by entering CITY. The next question to appear is
Look for what CITY ?
And you can answer by typing San Diego. Then it asks
Shall I send labels to the printer ? (Y/N)
Answer N for now. Youll see the mailing labels for the San Diego residents. You can try
running the program again and observe the use of macro substitution.
PROGRAMMING IN FOXPRO
33
Select Database option and click OK option. Then the following screen will appear.
Please enter field name, field type, field width and Decimal places (if field type is float) for your
database. Use Field <Insert> or <Delete> for inserting a new field or deleting an existing field.
After completing your field entries, Select < OK >. Then the following screen will appear.
34
PROGRAMMING IN FOXPRO
Enter the name for your database. If you want to change default drive/directory, you can
change by clicking and selecting on it. Select < Save > option for saving your database. Then
the following screen will appear.
Select < Yes > option for inputing data records now. Then enter data records. After
completing, press <Esc> Key to close the menu.
LIST
Lists record contents of current database
LIST TO PRINTER
Prints record contents of current database
CLOSE DATABASES
Close all opened databases
PROGRAMMING IN FOXPRO
?
SET STATUS ON
Sets status bar on
SET CONFIRM ON
Sets confirmation on
APPEND
To select append mode
APPEND BLANK
To add a blank record at the end of the database file
CREATE SAMPLE
Creates a SAMPLE database
QUIT
Quits from FoxPro
DISPLAY
Displays current record only
DISPLAY NEXT 4
Displays next 4 records from current record
CONTINUE
Continues Locating/Searching
EDIT or CHANGE
Edit/Change record contents
35
36
PROGRAMMING IN FOXPRO
BROWSE
Shows several records on the screen in the form of a horizontal table
BROWSE WIDTH 10
Displays fields of current database file in columns that are 10 characters wide
GOTO 7
Goto 7th record
GOTO TOP
Goto Top of the record
Deleting Records :
For deleting records, Please select Record menu ?
appear.
You can choose Scope / For / While for deleting records under condition.
PROGRAMMING IN FOXPRO
SCOPE :
?
?
?
?
?
?
?
OPTION
ALL
NEXT
RECORD
REST
DELETE RECORD 2
DELETE REST
DELETE FOR RATE < 10
RECALL
RECALL NEXT 3
PACK
MODIFY STRUCTURE
DELETES
All records in the database
Specified no.of records starting from current record
Specified Record
Current record and all subsequent record
-
Sorting a Database :
To sort a database, Please select Database ? Sort. Then the following screen will appear.
1.
2.
3.
4.
5.
6.
7.
8.
37
38
PROGRAMMING IN FOXPRO
USE ADDRESS
Open Address database
Indexing a database :
Indexing a database creates an index file and not database file. Indexing a database is
better than sorting. Indexing is better for frequent changes in records. Sorting is better for
fixed, rarely changes in records. For indexing a database, Select File ? New ? Index
PROGRAMMING IN FOXPRO
1.
2.
3.
4.
5.
6.
7.
Press OK to complete
39
USE ADDRESS
Opens Address database
REINDEX
FoxPro rebuilds the index files
The Compact index files reduce the size of the index files and make them efficient.
FIND RAJ
To find the record that contains Raj in the NAME field (indexed field).
40
PROGRAMMING IN FOXPRO
&
&&
*
@ SAY
@ GET
@ CLEAR
ACCEPT
ALLTRIM( )
APPEND
APPEND FROM
AVERAGE
BROWSE
CHANGE
CHR( )
CLEAR
CLEAR ALL
CLEAR GETS
CLOSE ALL
CMONTH( )
PROGRAMMING IN FOXPRO
COL( )
CONTINUE
COPY FILE
COPY
STRUCTURE
COUNT
CTOD( )
DATE( )
DELETE
DISPLAY
DO
DO CASE
DO WHILE
DTOC( )
EDIT
EOF( )
FIND
FOR
FOUND( )
GO / GOTO
41
42
PROGRAMMING IN FOXPRO
GOTO
TOP
BOTTOM
IF
IIF( )
INPUT
INT( )
LEN( )
LIST
LOCATE
LOWER( )
LTRIM( )
MAX( ) / MIN ( )
MODIFY
COMMAND
MONTH( )
PROPER( )
READ
RECNO ( )
RENAME
REPLACE
PROGRAMMING IN FOXPRO
ROUND( )
ROW( )
RTRIM( )
RUN
SEEK
SELECT
SET
SET BELL ON /
OFF
SET CARRY ON /
OFF
SET
CENTURY
ON / OFF
SET
CONFIRM
ON / OFF
SET DATE
SET DECIMALS
SET DEFAULT TO
SET DEVICE TO
SCREEN
/
PRINTER / FILE
SET EXACT ON /
OFF
SET FILTER
43
44
PROGRAMMING IN FOXPRO
SET ORDER
SKIP
SORT
SPACE( )
SQRT( )
STORE
STR( )
SUBSTR( )
SUM
TRIM( )
UPPER( )
USE
WAIT
YEAR( )
ZAP