Вы находитесь на странице: 1из 24

Working from the Command Window

Using the Command Window: When we made selection from the


menu system, we saw that Visual FoxPro generated the relevant
commands in the Command window. It may be easier to enter commands
than to make selections from the menu system.
Creating a New Table: To create a table file from command window,
type the word CREATE followed by the name of the table file, and press
Enter key.
1. Type CREATE and give file name and press Enter key in the
command window.
2. Fill the structure in appeared table and click on OK button.
3. Visual FoxPro displays a dialog box and asks if you want to input
data records now.
4. Give the answer. If answer is Yes then enter the records in table.
5. Press ^ + w keys to save the record.
Modify the structure of a Table: From the command window we can
modify the structure by typing the MODIFY STUCTURE command.
1. Type MODIFY STRUCTURE in the command window.
2. Move the pointer on the field, where we want to insert the new field.
3. Click Insert button. Now type the field name and enter the width.
4. Now we click Ok button to save the changes.
5. Click Yes to make changes permanent.
Displaying and Editing Data: Working from the command window,
we can USE a table and then type one of the following:
• APPEND to display the table in Append mode.
• EDIT or CHANGE to display the table in edit mode.
• BROWSE to display the table in browse mode.
• REPLACE to change the contents in more than one record.
Append command:
Syntax: APPEND [BLANK] [FROM <Filename> [FOR <expression>]]
This command is used to add records to the active database file. If
any file will not open then using this command will asked filename first.
APPEND will gives us Data Entry Screen for continues entry (may be
terminated by CTRL W).
APPEND BLANK will add a blank record at the bottom of the screen.
APPEND FROM <Filename> add records of a closed database file to
the active database file. The structure of both of the files should be same,
otherwise record nos. will increases but all or partially data will not
include.
If expression will add with Append From <Filename> then only
those records will added who fulfill the given expression.
Example: Use Kanak
Append
Append From Kumar
Append From Kumar For State="Bihar"
Edit/Change command:
Syntax: EDIT [FIELDS <Field List>] [condition] or CHANGE [FIELDS <Field
List>] [condition]
If we know the record number in advance, we can edit using EDIT command. For
example: EDIT 5
We may also specify the field list, conditions and scope while giving EDIT
command. For example: Use kanak
EDIT FIELDS NAME, BRANCH, ROLL_NO
FOR BRANCH = “computer science”
Browse command:
Syntax: - [BROWSE] [FIELDS <Field List>] [FREEZE] [WIDTH n]
[Lock]
This is a powerful editing command provide us facility to edit
records horizontally with showing us multiple records at a time.
Example: - Browse
Browse Fields Name, Salary
Browse Width 10
Browse Freeze Salary
Replace command:
Syntax: [REPLACE <field1> WITH <expr1> [, <field2> WITH
<expr2>
[<Scope>] [FOR <expL1>]
This is a powerful editing command. This is used to replace a new
value in a field with or without a condition. We can replace a new value in
more than one record in same command line.
Example: - Replace All Name with “Kanak” For Name=“Raja”
Replace All Salary with Salary*1.1 for Salary < 5000
Delete and Recall commands:
Delete command: - [DELETE] <SCOPE> [FOR <Expression>] [<Logical
Operator>
<Expression>]
This command is used to mark the specified records for deletion.
Delete does not remove records physically. It adds an astrick (*) sign
along with those records you want to delete.
Example: - Delete All
Delete Next 5
Delete Rest
Delete Record 10
Delete All For State = "Bihar"
Delete All For State = "Bihar" .And. City = "Patna"
Delete All For Salary <=5000
Delete All For Male
Delete All For Dob={10/10/80}
Pack command: -
Syntax: [PACK]
This command is used to physically remove all those records which
was marked for deletion. Remaining records will be copied to original file.
Example: - Pack

Zap command: -
Syntax: [ZAP]
This command is used to erases all records of active database
except its structure. This is very dangerous command, so use it
carefully because lost data can't be recovered.
Example: - Zap
Erase command: -
Syntax: [DELETE FILE <Filespec>]
or
[ERASE <Filename>]
This command is used to delete a file physically with all that’s
records and structure.
Example: - Delete File Akshay.Dbf
Erase Akshay.Dbf
Recall command: -
Syntax: [Recall] <SCOPE> [FOR <Expression>] [<Logical
Operator> <Expression>]
This command is used to unmark the specified record which was
marked for deletion.
Example: - Recall All
Recall Next 7
Recall Rest
Recall Record 7
Recall All For State = "Bengal"
Recall All For State = "Bihar" .And. City = "Patna"
Recall All For Salary <=5000
Recall All For Male
Recall All For Dob={10/10/80}
Run command: We can execute any DOS command or non Visual
FoxPro program without quitting from Visual FoxPro.
A DOS command is by placing executed with RUN or exclamation
mark (!) before writing the actual DOS commands.
The result of a DOS command appears in a separate Visual FoxPro
RUN command window.
Set Default: Visual FoxPro uses its default drive and directory for
creating files, unless we specify the path separately. For example: SET
DEFAULT TO C:
We can specify the full path name of a directory, by using:
SET DEFAULT TO C:\LEARNFOX\DATA
Display and List command:
List: - Syntax: [LIST] [FIELD <Field List>] [FOR <Expression>] [<Logical
Operator> <Expression>] [OFF] [TO PRINT]
This command is used to see or print the contents of a database
file. "OFF" suffix is used here for not print record no. "TO PRINT" suffix is
used for print information through printer if it is ON.
Example: - List
List Field Name, Salary
List For State = "Bihar"
List For State = "Bihar" .And. City = "Patna"
List Off
List Off Field Name,F_Name,City,Salary To Print
List For Salary <=5000
List For Male
List For Dob={10/10/80}
List For Dob=CTOD("10/10/80")
List For "Kumar"$ Name
DISPLAY: - Syntax: [DISPLAY] <SCOPE> [FIELD <Field List>] [FOR
<Expression>] [<Logical Operator> <Expression>] [OFF] [TO PRINT]
[FILES] [STRUCTURE] [MEMORY] [STATUS] [DELETED ()]
This command is used to display the records and some other
information. "OFF" suffix is used here for not print record no. "TO PRINT”
suffix is used for print information through printer if it is ON.
Example: - Display All
Display Next 5
Display Rest
Display Record 10
Display All Field Name,Salary
Display All For State = "Bihar"
Display All For State = "Bihar" .And. City = "Patna"
Display All Off
Display All Name,State To Print
Display All Off Field Name,F_Name,City,Salary To Print
Display All For Salary <=5000
Display All For Male
Display All For Dob={10/10/80}
Display All For Dob=CTOD("10/10/80")
Display All For "Kumar"$ Name
Display Files
(Displays .DBF files of current
Path/Directory.)
Display Structure
(Display structure of active database.)
Display Memory
(Display Memory Variables.)
Display Status
(Display current status of FOXPRO.)
Display For Deleted ()
(Display only those records which are marked for deletion.)
Using Logical Expression: Logical expressions are either true or false. They are
generally used to find an individual record or to isolate records that meet certain
criteria.
Logical Functions: Some functions also return .T. i.e. true or .F. i.e. false,
although they are generally used in programming.
Mathematical Operators:-
Unary minus -
Exponentiation ^ or **
Division /
Multiplication *
Subtraction -
Addition +
Relational Operators: -
> Less Than
> Greater Than
= Equal To
<> Not Equal To
# Not Equal To
<= Less Than Or Equal To
=> Greater Than Or Equal To
Logical Operators: -
1. .AND. :- Select only those records who fulfill both the conditions.
2. .OR. :- Select all those records who fulfill either first or second
condition.
3. .NOT. :- Select all those records who do not fulfill specified condition.
The IIF() Function: IIF is the function from an IF…ELSE…ENDIF structure, which
has three parameters. The first defines the logical condition, the second is the
expression returned if condition is true, and third is the one returned if it false. The
Syntax for this function: IIF (condition, expression1, expression2)
The IIF() function can save space in our programs, because in one line it can
take the place of the IF…ELSE…ENDIF commands. Example: In IF command: - IF
Result = “P”
? “Pass”
ELSE
? “Fail”
ENDIF
In IIF() function: - ?IIF (Result = “P”, “Pass”, “Fail”)
Sorting and Indexing: We can arrange records in our table in a way that best
suits our needs. Visual FoxPro has two commands for organizing records in a table
namely SORT and INDEX. We can perform Sorting or Indexing tasks at the
command level from Command window.
Sort: - Sorting means grouping records in a particular order say ascending or
descending based on a key field of the records.
Sorting a table: - Syntax : [SORT ON] <Field Name [/A] [/C] [/D]> [TO
<New Filename>]
This command is used to arrange the records of the active database
file according to a field to another database file into ascending or
descending order.
Example :- Sort On Name To Rajiv.Dbf
Sort On Name/A To Rajiv.Dbf
Sort On Salary/D To Msal.dbf
Sort On Name/C To Nameasc.Dbf
Index: - Indexing is a process of arranging records in a particular order.
In indexing, original record no do not change. We will note that the
indexed file on the right side of figure retains the original record number.
Indexing a table: - Syntax: INDEX ON <field expr> TO <.IDX file> | TAG <tag
name> [OF <CDX file>] [FOR <condition>] [COMPACT]
[ASCENDING/DESCENDING] [UNIQUE] [ADDITIVE]
Example: - INDEX ON NAME TO NAMES
Set Index: An index that has just been created is active. But by using
the SET INDEX command, we can activate any of the indexes.
Set Order: To activate an index file use the SET ORDER command. With
SET ORDER, we use numbers such as, SET ORDER TO 2. The number 2
here indicates the order in which the index file was originally opened.
Reindex: This command recreates all the open index files. It rebuilds all
open index files in the current work area. If any changes have been made
to the table while the index file was closed, we can update with the
REINDEX command.
Use of Macro: Macro substitution is a programming technique used in
command files. The macro is just like an ordinary memory variable, but
has an ampersand (&) in front of it namely, &NAME, &FLD etc. When
Visual FoxPro encounters a macro in command file, it replaces that macro
with the contents of the memory variable.
Use of Multiple Tables: Visual FoxPro is a relational database
manager, and hence gives us the choice to use one or more than one
table at a time. It also defines relationships between two or more table.
Relating Files: - Using Visual FoxPro relational power, we can create a
relation to connect or join records from different tables and thus we can
access their information at the same time.
In a relation, one table is called the parent table and the other is
called as child table. The parent table controls the child so that when we
move the record pointer in the parent table, Visual FoxPro automatically
moves the record pointer to the first corresponding record in the child
table. If a matching record cannot be traced in the child table, then
records pointer moves to the end of the last record in child file.

Working with Relational Databases in Visual FoxPro

Working with Relational Databases in Visual FoxPro:


A Relational database is defined as a database that allows us to group its
data items into one or more independent tables that can be related to one
another by using fields common to each related table.
Table in a relational database is similar to the traditional file system
with its records, fields and files. The table’s rows, called tuples, contain
records and columns, called attributes, contain fields.
Advantage of Relational Database:-
(a) The relational database structure can be efficiently used even with
a PC that has limited main memory and processing capability.
(b) Relational database is very effective for small databases.
(c) Relational database is much easier to use.
(d) Relational database is only concerned with data and not with the
structures, which improves the performance.
(e) In Relational database record relationship is implemented using
primary key or composite key field. Hence it is very simple and
represents the logical relationship among the data items rather
than physical relationship.
(f) Relational model is very useful for representing most of the real
world objects and the relationships among them.
Relationships among Tables: - A keyfield establishes relationship
among two or more tables.
There are three types of relationships that can be established
among tables. These are:
• One to one – It means that for every record in one table
there a single corresponding record in another table.
• One to many – one to much relation ship is a relationship
in which a record in one table has one or more related
records in another table.
• Many to many – in this relationship , a record n one table
has many corresponding records in a second table and for
every record in he second table, there are many
corresponding records in the previous table.
Creating a New Database in Visual FoxPro: - If we create an
application that requires several tables, we will probably find it easier
to store them all in one place. In Visual FoxPro, a database is a
collection of tables, views and relationships between the tables stored
as a file with .dbc extension.
Entering and viewing data using the Form Wizard:
The One-to-many wizard creates a form from related tables. One table is
considered as the parent table and another as the child table. The fields of
child table appear in a grid below the fields of parent table.
To run the One-to-many Form wizard
1. Click Tools menu, highlight Wizards and choose Form.
2. After this Wizard selection dialog box will appear. Now we choose
One-to-Many Form Wizard and then click OK.
3. In the dialog box that appears, the databases and Tables: area will
show the current database. If we want a table other than this, click
on the small button to the right of Databases and Tables: area. The
open dialog box will get opened and we can open the required table
using the dialog box.
4. From the Available Fields: area, select the field which we want in
our form, the click button. To select all the fields click the
button.
5. To remove a field from the selected fields, click the button. To
remove all the fields, click the button. After this we click on
Next> button.
6. Similarly, select the fields from the “child” table in the dialog box
that appears. Now we click on Next> button.
7. In the dialog box that appears, we can select the fields that
determine the relationship between the tables from the field lists.
Now we click on Next> button.
8. In the dialog box that appears, from the style box, select a style for
the3 form. The Wizard displays a picture in the magnifying glass as
an example of the style which we highlight.
9. The button type: area specifies the form’s navigation buttons. Now
we click on Next> button.
10. In the next dialog box that appears, select the fields based on which
we want to sort the records in the parent table. Then click Next>.
11. In the next dialog box, we click the Preview button to view the form
before we exit the One-To-Many Form Wizard. Otherwise select any
of the listed options and click Finish.
Query Wizard to work with Relational Database:
1. Click Tools menu, highlights Wizards and choose Query.
2. In the Wizard selection dialog box that appears, select Query
Wizard and Click OK.
3. In the Query Wizard dialog box that appears, first select fields from
one table. Move the required fields from the Available Fields: area
to the selected fields: area. Similarly, select fields from the other
table and move these also to the selected fields: area. Now we click
on Next>.
4. Now we select related fields from the field’s lists to determine the
relationship between the two tables. Now we click on Next>.
5. From the dialog box that appears, we have to choose whether we
want to include only related rows from both tables or all rows from
either table. By default, only related records are included. Now we
click on Next>.
6. In the screen that appears, we can specify our criteria. Criteria are
restrictions we place on a query or an advanced filter to identify the
specific records we want to work with. Now we click on Next>.
7. The next step is to determine the order in which our Query results
will be stored. Select a field from the Available fields: list and click
the Add> button to move it to selected fields. We can select up to
three fields. Now we select ●Ascending to sort the results in
ascending order or ●Descending to sort them in descending order.
Now we click on Next>.
8. In the dialog box that appears, mention whether we want all the
records or only a portion of the records. We have chosen to view all
the records.
9. Select an option to our choice from the dialog box that appears and
click Finish button.
Using One-To-Many Report Wizard:
The One-to-Many Report Wizard creates a Report that groups records
from a parent table with records from a child table.
To work with One-To-Many Report Wizard
1. Click Tools menu, highlight Wizards and choose Report.
2. In the Wizard selection dialog box, Choose One-to-Many Report
Wizard.
3. The rest of the steps are same as that of the form Wizard. In the
last step Check the “Wrap fields that do not fit” checkbox if we do
not want the fields to wrap. Wrapping occurs when the number of
selected cannot fit on a single line within the width of our report. If
we do not want the fields to warp, uncheck the checkbox.

Using the View Window:


The view window can be used to open and display tables, to establish
temporary relationships and to set work area properties.
To Work with the View Window
1. Click Window menu and chose Data session. The Data Session
window that appears is known as View window.
2. The Current session: drop down list displays the name of the
current data session. The Aliases area displays the view name or
table name without the file extension. The relations area indicates
any temporary relationship established between the tables or views
listed in the Aliases box.
3. Click the open button, to add a table or view to the Aliases list.
4. Click properties button to see the Work Area Properties dialog box.
In this dialog box we can modify the structure of a table, select
index files and fields, and define data filters. If no tables or views
are listed, an open dialog box will appear from where we have to
open a table or view.
5. Click the Browse button to display the table or view, currently
selected, in a Browse window where we can examine, edit and/ or
append data.
6. Click the Relations button to define the relations between tables or
views. A set Index Order dialog box will appear if no index order has
already been set between the two tables. When we click OK, the
Expression builder will appear in which we can set relationship
between the tables.
7. Click the One-To-many button to display the Create One-To-Many
Relationships dialog box, where we can establish a one-to-many
temporary relationship between child and parent tables. This button
gets enabled only when we establish relation between tables by
selecting them in the View dialog box and then clicking Relations.
8. Click Close button to Remove the selected table or view from the
Aliases list.

Using Queries and SQL:


SQL (Structure Query Language) is a simple high level language that
enables us to utilize a database completely and easily. It is a very simple
and powerful language because it uses compact, English-like statement
and performs very complex jobs to access information from very large
size database.
Purpose of using SQL:-
a. Creating, deleting, and modifying table structure.
b. Defining the relationship between two or more tables.
c. Inserting data into tables.
d. Extracting data in meaningful ways based on the tables defined
relationships that is accessing or retrieving data.
e. This includes maintaining database and manipulating the stored
values to keep the information current.
f. This includes system security and authorization which must be
controlled and maintained.
The syntax for the Visual FoxPro SQL SELECT statement is following:

SELECT <field list> FROM <table list> TO <output option> WHERE


<join criteria> GROUP BY <group column> HAVING <filter criteria>
ORDER BY <order item>
Clause Function
SELECT Select the fields to be output
FROM Specify the tables from which to retrieve information
TO Determine where should output go
WHERE Specify the primary section criteria
GROUP BY Specify any groups
HAVING Specify any secondary selection criteria
ORDER BY Specify a sort order for the final output
Queries versus View:
Views are tables whose contents are taken or derived from other tables.
Views themselves window through which certain contents of a table can
be viewed.
Queries and Views are similar to each other in many ways. Both are
created using the same method and both are used to retrieve data. A
Query, on the other hand, can only work with native Visual FoxPro
databases. Differences between Queries and Views are as follows:

Queries Views
Queries are simpler to create. Views are somewhat more than
difficult to create than Queries.
Outputs of Queries are read-only Views are not read-only which
which ensures that data in a query means that data in a view is
are not accidentally altered. modifiable. Also, the modifications
made in a view get automatically
reflected in the base tables. Hence,
Views are less safe than Queries.
Queries perform slightly better than Views perform less better than
Views. Queries.
Queries can be selected as input to
graph, cross-tab, report and label Outputs of views are limited to
creation. Browse windows and Screen Forms.
Query Designer: In Visual FoxPro, we create queries in special window
called the Query Designer. The Query Designer refers to the way that
Visual FoxPro allows us to get information from table. The Query Designer
is a versatile and powerful tool for retrieving information.
Working with Multiple Tables: Through Query Designer we work
with multiple pages. To add a table to the Query Designer:-
1. Right click the top pane of the designer. From the menu that
appears, select Add Table… In the Add Table or View dialog box
that appears select the table which we want to use and click Add
button.
2. A join condition dialog box will appear. Select the matching fields
from the two tables and specify whether we want Inner, Left, Right
or Full join.
3. Now the top pane of Query Designer displays two tables. The
designer shows join condition by displaying lines that connect the
matching fields between the two tables. We can drag a field from
one table to matching field in the other table to create further join
conditions.
To insert a join condition, click the Insert button and to
remove a selected join condition click the Remove button.
Directing output: The default output for the query designer is the
browse window. We can specify other output destinations using the query
destination button in the query designer toolbar.
The browse button will direct the output to the browse window.
The cursor button will direct the output to a cursor. Cursor is a
temporary file which can hold the results of a query temporarily and the
contents of which can be passed to another SQL statement.
The table button will direct the output to a dbf file.
The graph button will connect the output with Microsoft graph
wizard. It will guide us through the process of creating a rudiment graph.
The screen button will connect the output to an input/edit screen.
The report button will direct the output into a visual FoxPro report
or will create a quick report immediately.
The label button will direct the output to a label form.
After selecting the output destination, click OK.
Join: In this tab we have to specify join type, field name and criteria.
For specifying the criteria, we can use relational operators = (like), ==
(exactly like), > (more than) and < (less than). These relational operators
can be used with NOT operator to negate the logic.
To insert a join condition click the Insert button and to remove a
selected join condition clicks the Remove button.
Viewing SQL code: We can view the actual SQL code, using the SQL
button in the query designer toolbar.
Alternatively, one can also view the SQL code by selecting View SQL
from the query menu.
Maximizing the Table view: To maximize the top pane of the query
designer window, click the maximize the table view button in the query
designer toolbar.
Running the query: To run our query, choose run query from the
query menu. This will execute the SQL query we have built and send the
results to the output destination which we have specified. Alternatively
press ctrl + q keys to run the query.
A grouped query: Suppose we need to group the records on the basis
of department, select the field to be grouped from the available fields list
and click add button. Similarly, fields can be removing from the grouped
fields list by clicking the remove button.
We can also specify the selection criteria for groupings. This can be
done by clicking the having button.
SQL select command: SQL SELECT command retrieves data from a
table. Syntax: SELECT * FROM <Table name>
From: From is the keyword in SQL, which must be present in every
SELECT query. Syntax: SELECT <Column name> FROM <Table name>
Where: Suppose we want to see only a certain number of rows that
contain specific values. For this, we would need to place WHERE clause
along with the SELECT statement. Syntax: SELECT <Column name>
FROM <Table name> WHERE <Column name> <Operator> <Value>
Aggregate functions: SQL Aggregate functions produce a single
value for the entire group of table entries. These functions are SUM, AVG,
MAX, MIN and COUNT.
SUM: This function calculates the arithmetic sum of all selected values
of a given column. Syntax: SELECT SUM <Column name>
FROM<Table name>
AVG: This function calculates the average of all selected values of a
given field. Syntax: SELECT AVG <Field name> FROM<Table name>
MAX: This function calculates the largest of all selected values of a
given column. Syntax: SELECT MAX <Column name> FROM<Table
name>
Min: This function calculates the smallest of all selected values of a
given column. Syntax: SELECT MIN <Column name> FROM<Table
name>
Count: This function counts the number of rows in the output table.
Syntax: SELECT COUNT <Distinct/*> FROM < Table name>
ORDER BY: This command is used to sort in ascending or descending
order in a particular field. This is done by adding ORDER BY to the SELECT
command. Syntax: SELECT <Column name> FROM <Table name>
WHERE <Condition> ORDER BY<Column to order> [<ASC/DESC>]
GROUP BY: The GROUP BY clause allows us to form groups based on
the specified condition. Syntax: SELECT <Column>, function
(<Column>), functions (<Column>) FROM <Table> GROUP BY <Column>
HAVING: The HAVING clause defines criteria used to eliminate certain
groups from the output, just as the WHERE clause does for individual
rows. Syntax: SELECT <Column>, function (<Column>) FROM
<Table> HAVING <Column>
OTHER SQL Commands: Other than retrieving data, SQL provides
commands to update the data in a table. Updating data refers to inserting
and deleting rows in tables and changing values in columns. This invokes
command such as Insert, Update, Delete etc.
Also, SQL can be used to define security mechanism or scheme for
protecting data in a system from unauthorized access. This involves
commands such as Grant, Revoke etc.
INSERT INTO Command: INSERT INTO command adds new rows to
a table. Syntax: INSERT INTO <Table name> VALUES (<value>,
<value> …)
UPDATE Command: UPDATE is the SQL verb that changes or modifies
data values in a table. Syntax: UPDATE <table name> SET <column name
= new column value> [WHERE <condition>]
DELETE command: We can remove rows from a table with the
command DELETE. This command removes specific rows meeting the
condition and not the individual field values. Syntax: DELETE FROM
<table name> WHERE <condition>
GRANT command: This command is used to permit users access to
the database. Syntax: GRANT <privilege name> | ALL ON <object> TO
<user | PUBLIC> [WITH GRANT OPTION]
REVOKE command: This command is used to cancel database
privileges from user. Syntax: REVOKE<privilege name> | ALL ON
<object> TO <user | PUBLIC>
Using Visual FoxPro Database: In a typical database, one needs to
perform several operations, such as adding new information, modifying
existing information, arranging information in a desired order, removing
unwanted or outdated information. Visual FoxPro is such a database tool
for managing relational databases.
Opening a Database: Opening and existing Visual FoxPro database is
as same as opening other files.
To open a database, we must be:-
(i) Go to File menu and choose open
(ii) After appearing the open dialog box, drop down list of
“files of type” select “database (*.dbc)”
(iii) Now we select the location in “Look in: drop down list”.
(iv) Now double click the database, listed in the blank
rectangular area.
(v) Alternatively, we can also open a database, by entering
open database <database name> in the command
window.
Closing a Database: To close all open databases we give following
command in command window:
Close databases: For close all open databases and tables.
Close all: For close all open databases, tables, form designer,
report designer, query designer and label designer.
Deleting a Database: We can easily delete a database by giving the
following command. Syntax: delete database <database_name>
[delete tables]

Visual FoxPro Utility

Importing and Exporting Data using Wizard


We can import data from a spreadsheet program, such as MS-
EXCEL or any other program such as Paradox it is in a table format, into a
Visual FoxPro.
Steps of Importing data:-
1. Click File menu and chose Import.
2. After Importing dialog box appearing, in the type: drop down list,
specify the type of file to Import.
3. In the Form: drop down list, specify the path and name of the file to
import. Either type the full path and filename or click the button
with three dots () to locate the file.
4. If we have selected Microsoft EXCEL 5.0 in the Type: drop-down list,
another drop-down list called Sheet: appears. In this specify the
excel worksheet to import.
5. To: textbox shows the path and filename of the table to which the
file will import.
6. Click OK to import the file. If we wish to import the text file click on
the import Wizard button. This starts the Import Wizard which will
guide us through a brief series of steps to import ASCII text files.
Alternatively, use the import command in the command
window.
Syntax: import from <filename> [Database <database-name> [Name
<LongTableName>]] [Type <file type>]
Steps of exporting file:-
1. Click File menu and chose Export.
2. After Export dialog box appearing, from the Type: drop down list,
specify the type of file to export.
3. In the To: textbox, type the path and name of the destination file.
Either type the full path and filename or click the button with three
dots () to locate the file.
4. In the text delimiter: textbox, specify the character using which we
want to delimit strings of text in the output file. The default text
delimiter is the double quote (” “) character.
5. From the field separator: drop down list, select the character which
we want to separate data fields in the output file. The default field
separator is the comma (,) character.
6. In the Form: textbox, specify the path and file name of the source
file. Either type the full path and filename or click the button with
three dots to locate the source file.
7. Click OK to export the file. Clicking Options… button will display the
export Options dialog box where we specify which records and
fields are to export.
Alternatively, use the export command in the command
window.
Syntax: export to <filename> [Type <file type>]
Editing Text
We can enter and edit text in any of the following places:
a. Memo editing window: In the memo edit window, we should be able
to use all of Visual FoxPro’s text edit capability, namely cut, copy,
paste etc.
b. Command window: Commands can be tried out in the command
window and pasted into the program file.
c. Program files: Programs are edited in the program files.
d. Form’s and menu’s code properties: Program codes can also be
edited here.
e. Edit window fields: Edit window is also an ideal place for text
manipulation.
f. Browse window fields: Browse window can also be used for editing
text.
Edit Menu
The options in Edit menu can be divided onto two sections-one for
text editing(Cut, Copy, Paste, Find and Replace) and the other for linking
Visual FoxPro with another software(Paste special, Insert Object and
Links). The later portion of the edit menu involves OLE (Object Linking and
Embedding) which allows us to connect data in software.
Undo: - Through this option we can reverse our last editing command.
Redo: - Through this option we can reverse using the Undo command can
be redone.
Cut: - Through this option we remove selected text from the field or file
into a section of memory called the clipboard, from which it can be quickly
recall.
Copy: - Through this option we can copy of selected text from a field or
file into the clipboard without removing it from the current location.
Paste: - Through this option we paste while a piece of text is selected, the
contents of the clipboard replace the selected text otherwise without
selecting paste into current file at the cursor location.
Paste Special:-Through this option we can link or embedded on OLE
object from the clipboard.
Clear: - Through this option we can remove selected text or data without
placing on the clipboard.
Select All: - Through this option we can select all lines of text in the
current editing window.
Find: - Through this option we can search for a particular text from here.
Find Again: - Through this option we can locates the next occurrence of
the string found previously with the Find option. The search continues
forward from the cursor position.
Replace: - Through this option we can replace the specified string in the
Look for: box with the string in the Replace With textbox.
Insert Object: - Through this option we can list the Object Linking and
Embedding objects that we link or embed in an application from Forms or
general fields in Visual FoxPro.
Object: - This option provides us to editing a selected OLE object.
Properties: - Through this option we can specify whether we want a
centre, left or right alignment for his text, set color and font option
including proposed automatic indentation of lines for all our future editing
sessions.
To create an OLE link:
1. Open a table, containing a general type field, in the Browse window.
2. Double-click the field. A new window for the field will open.
3. Click Edit menu and choose Insert object.
4. If the object which we want to insert has not been created yet, then
select the Create new ratio button. From the list of applications in
the Object Type: list box, select the application to use.
5. Check the Link checkbox, if we want to link the Object and uncheck
it if we want to embed the object. Now we click on Ok.
6. We will note that the toolbars and working environment of the
application we choose appears within the Visual FoxPro window.
Create the object here. When we return the new object, it will be
incorporated into the general field.
7. If the object which we want to insert is already available, select the
create from File ratio button.
8. Either writes the full pathname of the file we need in the File:
textbox or use the File open dialog box. This dialog box will appear
when we click the Browse... button.
9. Choose the required file and click OK. The object will get inserted in
the general field. If we want to edit in object, double click it.
10. Alternatively, we can open the concerned application, Copy
the required object and use Paste or Paste Special…. To insert the
object in the general field. In the Paste Special dialog box, we can
select the type of link we want.
Format Menu
The Format menu contains that controls fonts, spacing, alignment,
indentation and object positioning. Additional commands are available on
the Format menu that would vary according to what window we can work
with.
Font: - Through this option we can set the font type, style and size in the
current window.
Enlarge Font: - This command is used to enlarge the font size.
Reduce Font: - This option reduces the font size to the next-smaller size
available in the current window.
Single Space: - This option displays text with no blank lines between text
lines in current window.
1 1 /2 Space: - This option sets line spacing at 1.5, between lines of text
in the current window.
Double Space: - This option sets line spacing of text at 2 in the current
window.
Indent Space: - This option indents the selected line or lines by one tab
in the current window.
Unindent: - This option removes previously inserted indents one a time in
the current window.
Comment: - This option marks selected lines of text as comment by
placing an *!* at the beginning of the line.
Uncomment: - This option removes previous comment formatting (*!*)
from the selected text.
Align: - This option displays the Align submenu, which lets we change he
size of selected object.
Size: - This option displays the size submenu, which lets we change the
size of selected object.
Horizontal Spacing: - This option displays the horizontal spacing
submenu, which lets we change the horizontal spacing of selected
objects.
Vertical Spacing: - This option displays the horizontal spacing submenu,
which lets we change the vertical spacing of selected objects.
Bring to Front: - This option brings selected object to the top of the
objects in a form.
Send to Back: - This option brings selected object to the back of the
objects in a form.
Group: - This option joins selected objects and they can be manipulated
as the group.
Ungroup: - This option separates previously grouped objects into
individual objects again and they can manipulate individually.
Snap to grid: - This option moves controls in grid increments when we
select object and drag them.
Set Grid Scale: - This option displays the Set Grid Scale dialog box,
where we can define horizontal and vertical increments of the grid in
pixels.
Text Alignment: - This option displays the text Alignment submenu,
which contains commands that let we adjust alignment and spacing of
text within a field or label control.
Fill: - This option defines a fill pattern for shapes such as rectangle,
rounded rectangle etc. Fill provides eight fill pattern options.
Pen: - This option displays the pen submenu, which contains commands
that let we set point size and style for different shapes.
Mode: - This option displays the Mode submenu, which lets we define
whether the selected control is opaque or transparent.
Spelling Tool
The Spelling Tool is very flexible and powerful tool that allows us to
check spelling in a program file or in the memo field.
Steps to spell check our program file or memo field:-
1. Click Tools menu and choose spelling….
2. The spell tool will search through our document, and selects words
that it does not recognize. The misspelled words are highlighted in
the text and displayed in the not in Dictionary: box.
3. Either type the current words in the change To: textbox or select
the correct word from the suggestions: list. Then choose anyone
from the following list:
• Click the change button to change the word.
• Click Change All to change all occurrences of the misspelled
word in document.
• Click Ignore to leave the word as it is.
• Click Ignore All to ignore all occurrences of the word in the
document.
• Click the Add button to add this word to the Visual FoxPro
dictionary.
4. Click the Undo Last button to undo the spelling change made.
5. Spelling tool will continue searching. Click Close to stop the spell
checking if so desired.
6. Clicking the Suggest button will display a list of suggestions after
we type a word in the change To: textbox.
7. Click the Options… button in the Spelling & Grammar dialog box to
open the Options dialog box. Various option available in this dialog
box are as follows:
Option Function
Always suggest Display a list of suggested correction for each
unmatched word when we check spelling.
Ignore words in Skip words in which each character is in
UPPERCASE uppercase letter.
Ignore Extra Spaces Skip extra spaces between words.
8. In the Directory area, in the language: list box, specify the language
of dictionary we want and in the Type: list box, specify whether we
want Normal, Concise, Complete, Medical or Legal dictionary. Now
we click on OK button.
Object Linking and Embedding
Object Linking and Embedding is a method by which an object
created in another application, such as an Excel spreadsheet or a Word
document, can be linked to or embedded in a Visual FoxPro table or form.
Linking: - Linking is the term used to describe the establishment of a
connection between the source document and the destination document.
A linked object maintains connection between the two documents. When
the information changes in the source document, the changes are
reflected in the destination document, automatically.
T edit the linked information, double click it, the toolbars and menus
from the original program will appear. Changes that we make to the
linked information will also appear in the original document.
Embedding: - Embedding is the term used to describe the insertion of a
copy of an object from one application into another. Once the copy is
inserted it has no further association with the original object. The
embedded copy does not change even if the original object changes.
If a document is embedded, hen we can edit the information by
opening the document in the original program and make changes to it.

Programming and Developing Applications using Visual


FoxPro

An Overview of Programming: A computer programming is a series


of instructions given to a computer to execute them, in general, one after
the other. These instructions are commands that cause the computer to
perform specific tasks. A command file created by the programmer in
Visual FoxPro contains a group of commands. The command file is saved
with an extension .prg in Visual FoxPro.
Creating Files:
1. Click File menu and choose New. From the appeared dialog box, we
choose Program radio button and click on the New file button.
2. A program editor will appear. Alternatively, types modify command
<file name> in the command window.
3. Type our program in this editor.
4. After we have entered the commands, invoke File menu and select
Save As, type the file name to save the current document.
5. Click Save button.
Running the program from a file: We can execute a program by
selecting DO from the program menu and selecting a program from the
Do dialog box. Alternatively, type Do <file name> in the command
window.
Variables: Variable is a memory location within the computer that is
used to store data temporarily.
Variables play very important role in programming by:
a. collecting and storing inputs
b. preserving data from a table record
c. storing the results of calculations for future use
d. storing individual items of information that must be globally
available throughout an application
Creating and using Variables: - There are mainly four types of variables
used in Visual FoxPro. These are character, numeric, date and logical
variables.
1. Character Variables: - This store strings of characters, which can be
letters, numbers or a combination of both.
2. Numeric Variables: - This is used to store numeric values such as
whole or decimal numbers.
3. Date Variables: - This store date values written in MM/DD/YY format.
4. Logical Variables: - This contain a logical value of T(true) or F(false).
Variables can be created in one of the following two way:-
a. Using the Store command
Syntax: STORE <expression> To <variable name>
b. With assignment (=) statement
Syntax: variable = expression
Simple Output using ‘?’ Command: Till now we have created many
memory variables with STORE or = (assignment) methods. We can view
the contents of memory variables by entering statements in the following
manner:
? MNAME, CHOICE, AMT
The “?” displays the specific values that had been stored in the
memory variable. However, we can use DISPLAY MEMORY or LIST
MEMORY commands to show existing memory variable values. Enter the
following command to display active memory variable DISPLAY
MEMORY .
Getting Input from the User: There are two commands, namely
ACCEPT and INPUT commands that allow we to get data from the
keyboard. When we use either Accept or Input commands, we enclose the
prompt to be asked in apostrophes (‘) or double quotes (“). We also need
to provide a variable to store the answer to the question.
Syntax: Accept “<Message>” To <Variable name>
Or Input “<Message>” To <Variable name>
@...Say…Get Command: We also use a combination of @, say, Get
and Read commands to prompt the user to enter the values from the
keyboard.
The @ command allows us o perform many things such as
displaying information, inputting data in fields/variables, clearing selecting
screen and designing custom screens.
Clearing Screen with @ Commands: - @...CLEAR statement blanks
rectangular area on the screen.
Drawing and Clearing Box Using @ Command: - @ Command I used to
draw lines and boxes on the screen. Boxes can be drawn with a single or
double-line border. For drawing we need to specify the top-left and
bottom-right coordinates of the box.
Displaying and Manipulation Information with ?/?? Commands: -
The ? and ?? Commands are used to ask Visual FoxPro to display
particular data on the screen. The ? command issues a carriage return
before printing the expression list, so I is positioned one line below the
previous item displayed. The ?? Command does not include a linefeed and
carriage return before the display, so the subsequent value is displayed
on the current line.
For example: ? “Visual FoxPro”
?? “Visual FoxPro”
Picture Clauses: It applies a specific format o a specific character
position. It restricts entry to specific characters and it validates formats
but not values.
Symbol Meaning
A Accepts only alphabetic characters
L Accepts only Y(yes) or N(no)
N Accepts letters and digits
X Accepts any character
9 Accepts only digits or sign
# Accepts digits, blanks and sign
! Converts lowercase letters to uppercase
$ Displays currency symbol
. Specifies decimal point precision
‘ Left of the decimal
Example: - If we want to get emp_code that must begin with an alphabet
and has three digits then we can write Picture”A999”.
Control Flow: Control statement allows us to change the sequence of
instruction for execution. Control statements are of the following two
types:
• Looping Statement
• Selection Statement
Looping: Repeating a step or a number of a program for a specified
number of times or until a condition is met, is known as looping or
iteration. Iteration is used when one or more instructions may be
executed several times depending on a condition. Visual FoxPro provides
three loop commands, these are:-
a. DO WHILE…ENDDO
b. SCAN…ENDSCAN
c. FOR…ENDFOR
DO WHILE…ENDDO: The most important command used for Iteration
in Visual FoxPro language is DO WHILE loop, which has the following form:
Do WHILE <logical exp>
….
ENDDO
Each time the program reaches this DO WHILE command, it checks to
confirm if the logical expression is true. If it does, the program executes
that commands that follow. When it gets to ENDDO, it loops-break.
SCAN…ENDSCAN: This is very useful structure for programming in
Visual FoxPro. A SCAN loop acts like a DO…WHILE loop.
Syntax: SCAN [scope] [FOR condition] [WHILE condition] [commands]
ENDSCAN
FOR…ENDFOR: FOR…ENDFOR executes a set of statements within a
loop for specified number fo times. We can use a memory variable as a
counter to determine how many times Visual Foxpro executes the
statement inside the loop.
Syntax: FOR <memvar> = <exp1> to <exp2> [STEP<exp3>]
ENDFOR/NEXT
SELECTION: Selection is used for making decisions. It is used for
selecting the proper path out of the two or more alternative paths in the
program logic.
Visual FoxPro has two basic methods of selection. When there are
one or two choices, it is most convenient to use the IF…ELSE…ENDIF
command. When we have a large number of choices, it is most convenient
to use the DO CASE…ENDCASE command.
IF…ELSE…ENDIF
Syntax: IF condition <list of commands>
ELSE <list of commands>
ENDIF
DO CASE…ENDCASE
Syntax: DO CASE
CASE condition1
<list of commands>
CASE condition2
<list of commands>
CASE condition3
_____ <list of commands>
______________________
OTHERWISE
<list of commands>
ENDCASE
Exit and Loop Command: The EXIT command lets us exit from the
loop created by DO WHILE…ENDDO, FOR…ENDFOR or SCAN…ENDSCAN
loops. Use this command from within a DO WHILE…ENDDO, FOR…
ENDFOR or SCAN…ENDSCAN statements.
Another command is the LOOP. It causes the program control to
directly return to the DO WHILE, FOR, or SCAN statements.
Procedures and Parameters: A procedure is a set of instructions that
work as a unit. It is a sequence of Visual FoxPro commands that is
functionally equivalent to the program, except that it resides in another
program file. We can create procedure in the same way as we create
programs.
Syntax: PROCEDURE <procedure_name>
<commands>
[Return]
ENDPROC
Scope of Variables: A variable’s scope specifies how the variable
makes its value known to other procedures and functions. Visual FoxPro
offers two commands, PRIVATE and PUBLIC that are used to classify
variables. If we have created a variable in one command file and we need
to use it in other command file then we should declare them Public.
However, if we want the variable we are declaring should be available
only to the program that has created the variable and also to programs
that are called by the specific program, then declare it as Private.
Using the Project Manager: The Project Manager is used to organize
and manage all our files in projects. Open the project manager:-
1. Click File menu and choose New…Now select the Project radio
button in appeared dialog box and click New File button.
2. Now we select the directory of our choice and provide a name
for the new project file in appeared dialog box and click Save.
3. When the Project window is active, Visual FoxPro displays the
Project menu on the menu bar.
4. A plus sign appears to the left of items if there are further
items of that type in the project. Once clicked, the plus sign
changes to a minus sign, which we click to collapse the expended
list.
5. The project manager window can be docked to the op of our
Visual FoxPro screen. It can be done by either dragging to the menu
bar or by double clicking the title bar of the Project Manager
window.
6. When the window is docked, click tabs o display their
contents. We can tear off a tab by dragging them away from the
menu bar. To replace it, just drag it back to its original position or
click the Close box.
7. You may see a list of items in the Project manager window
such as Tables, Forms, Reports, Queries, and Labels etc. Clicking
the plus (+) sign to the left of an item will display the sub-items. For
example, if you click the forms item, all the forms added to this
project will be displayed.
8. Click the Add button to add an existing item to the currently
selected item.
9. Click the New button to add a new file or object. The new file
or object will get added to the currently selected item.
10. Click the Modify button to make any changes to the added
item. This button gets enabled only when you select an item.
11. Click the Remove button to eliminate the selected file or
object from the project. A window will appear asking whether you
want to just remove the file from the project or you want to delete it
from the disk.
12. Click the Build button to build a project, application or an
executable or OLE server. This is done in the Build Options dialog
box that appears.
Using the Report Wizard
Auto Report: Auto Report are adequate when all we need is a
columnar listing of data in the fields, we have asked for. This method is
simplest to create. However it offers the least flexibility in presenting our
data.
To create Auto Report firstly we open a table then click the Auto
Report Wizard button on the standard toolbar. Auto Reports include all the
field of the table and use the field names as labels. Reports are saved
with the extension .frx. Auto Reports are given names made up of the
initial letter of the file name, followed by an underscore (_) and a number.
Ex- Employee_1.frx.
Creating Report using Report wizard: Report Wizard is used to
generate report using only table. We can create a Report Wizard in the
following ways:-
1. From the Tools menu, choose Wizard and select Report.
2. In the Wizard selection dialog box, choose Report Wizard and click
OK.
3. Click on New button on the standard toolbar. Now we choose Report
in appeared dialog box and click on Wizard and select Report
Wizard.
4. Now we select field form Available field box. From the Available
Fields: area, select the field which we want in our form, the click
button. To select all the fields click the button. To remove a field
from the selected fields, click the button. To remove all the fields,
click the button. After this we click on Next> button.
5. Now we choose Report style from the Report dialog box namely
Executive, Ledger and Presentation. After choosing the style click
Next> button.
6. Now after Report dialog box appearing we define the Report layout
of our report. In the field layout: we can select columns or Rows. If
we pick a column again we need to decide whether the orientation
should be Portrait or Landscape. Now we click on Next> button.
7. In the next step we select sorting order of record by selected field in
ascending or descending order. Now we click on Next> button.
8. In the next step we type a title for our report and click on Preview to
view the report without leaving the Wizard. We can close the
Preview by press ESC key.
9. Click as save report and modify in the report designer and click on
Finish button.
Creating a Group/Total Report: