Level 3 Visual Fox Pro
Level 3 Visual Fox Pro
FoxPro 7.0:
Advanced Visual
FoxPro 7.0
Programming
Concepts
Table of Contents
INTRODUCTION .................................................................................................I-1
A Few Words About This Courseware.....................................................I-2
World-Class Courseware Taught by World-Class Instructors......I-2
What We Expect of You...............................................................I-2
What You’ll Get Out of this Training.............................................I-2
Intended Audience .......................................................................I-3
Courseware Conventions.............................................................I-3
The Practice CDs.....................................................................................I-4
Technical Requirements ..............................................................I-4
Installing the Practice Files ..........................................................I-4
What’s on the Practice CD?.........................................................I-4
Technical Requirements for the Labs ......................................................I-5
Required for All Labs ...................................................................I-5
About the Author......................................................................................I-6
Summary ..............................................................................................6-17
INDEX........................................................................................................INDEX-1
Introduction
• familiarity with PCs and Windows 95, 98, NT, XP, or 2000.
• a good understanding of relational data, including the concepts of
tables and fields.
• a good understanding of application development with Visual FoxPro.
• the desire to learn.
• Be able to work with data from Data Servers and other remote data
sources.
• Understand how to make the most of COM components.
• Access FoxPro data from other applications with the Visual
FoxPro OLE DB Provider.
• Learn how to use source code control with Visual FoxPro.
• Use the Visual FoxPro Internet tools.
• Create Internet applications using Visual FoxPro COM
components.
• Understand how to call the Windows API from Visual FoxPro.
• Understand how to design class libraries.
• Extend the Visual FoxPro Development environment.
Intended Audience
The training is intended for:
Courseware Conventions
We’ve followed several conventions to make this courseware easy to follow:
Technical Requirements
Many of the tools and technologies covered in this course are new and rapidly
evolving, so there are some minimum software requirements for running all
the course code:
Integrating Visual
FoxPro Web
Components
Objectives
• Understand how clients and servers interact in a distributed
application.
• Investigate the basics of creating a Web application with Visual
InterDev and Active Server Pages.
• Discover how to create Visual FoxPro COM servers.
• Learn how to call Visual FoxPro COM servers from Active Server
Pages.
Introduction
No matter where you turn, the Internet is there. Where only a few years ago
doing serious work on the Internet was a novelty, it is now commonplace.
Purchasing goods and services of all kinds on the Internet has become the rule,
not the exception.
The key of course is that application development tools have evolved to the
state of being able to create applications that provide a rich user experience on
the Web. Where before, Internet-based applications were unreliable and had
non-intuitive interfaces, today’s Internet-based applications have user
interfaces that are feature-rich and are very intuitive to navigate. Of course,
tools by themselves cannot get the job done alone. Never before has server
power been more affordable. Couple server power with high bandwidth
services today such as DSL, Cable, and T1/T3 and a suitable platform now
exists for mainstream Internet-based applications.
Visual FoxPro COM Servers coupled with Active Server Pages created with
Visual InterDev make an excellent environment for creating Internet-based
applications. Combining the Visual FoxPro language and Visual InterDev’s
ability to produce content that can be delivered to the Internet, makes creating
Internet-based applications easier than ever.
In the most basic terms, clients make requests and servers process the requests
of clients. In LAN scenarios, clients usually perform a portion of the
processing, whether it is business rule enforcement and/or data processing.
Regarding the latter, data processing, has been a strong point of Visual FoxPro.
Today, Visual FoxPro is still the best performing tool when it comes to local
data processing. In addition, Visual FoxPro sports powerful tools for quickly
building rich client user interfaces.
Figure 1. In a LAN-based scenario, very often, the core application logic exists in
a single EXE file, which may reside on either the client workstation or the server.
In this scenario, the data resides on the server.
Up to this point, the role of clients and servers has been relegated to hardware.
Client and server components can also be in the form of software. To make
things even more interesting, software components can take on both roles of
client and server. Consider a 3-tier scenario in which the user interface is on
the client, and the business rules are contained on the server. From a hardware
perspective, the architecture is the same as in Figure 1. From a software
perspective however, things are a bit more complex. The user interface is
squarely in the client domain. The data store is completely in the server
domain. The middle-tier component for business rules can be both a client and
a server. From the perspective of the user interface, the business rule
component is a server because the user interface requests its services. From the
perspective of the data store, the component is a client because it requests data.
Regardless of whether the business rule components are on the same physical
machine or spread across different machines, this distinction will still hold
true.
Figure 2. A physical connection does not exist between the client and server in an
Internet-based application.
As with a LAN-based scenario, the business rule components and data store
can reside on the same physical machine as the Web server. Alternatively, the
business rule components and data store can be spread across multiple
machines if necessary.
What Is HTML?
See HTML is a text markup language (as opposed to a programming language such
HTMLSample.htm as Visual FoxPro, Visual Basic, or Visual C++). While the implementation of
programming languages such as Visual FoxPro is to write code to accomplish
a task, the purpose of HTML is to deliver formatted content to the client.
Unlike other languages, HTML does not have commands. Rather, it is made up
of tags that dictate the appearance of Web browser content. The following code
shows the source code of a sample HTML document:
<HTML>
<HEAD>
<TITLE>A Sample HTML Document</TITLE>
</HEAD>
<BODY>
<H1>Heading 1</H1>
<HR>
<H2>Heading 2</H2>
<H3>Heading 3</H3>
<H4>Heading 4</H4>
<H5>Heading 5</H5>
Normal text...
<H1>A bulleted List</H1>
<UL>
<LI>Line Item One</LI>
<LI>Line Item Two</LI>
</UL>
<H1>A table</H1>
<TABLE Border>
<TR><TH>Table Heading</TR>
<TR><TD>Row one</TD></TR>
<TR><TD>Row two</TD></TR>
</TABLE>
</BODY>
</HTML>
Figure 3. The sample HTML document displayed using the Internet Explorer Web
browser.
There are many tags in the latest HTML specification, 4.0. A great online
reference of what comprises HTML can be found at the World Wide Web
Consortium Web site: http://www.w3.org/MarkUp/Guide/. Table 1 outlines the
most common HTML Tags.
Tag Definition
Many HTML tags actually come in pairs. The first is the beginning tag. The
beginning tag specifies where the specific format style should begin. These
tags are outlined in Table 1. The second component is the ending tag. The
ending tag specifies where the specific format style should end. Ending tags
look like beginning tags with the exception of being preceded by the /
character. For example, the ending tag for <H1> is </H1>.
Most tags also have attributes. Consider the <TABLE> tag. If you wish to
have a border appear for the table, the Border Attribute would be used as
follows: <TABLE Border>. The <FONT> tag has many attributes. For
example, to display text using the Arial font with a 14-point size, the HTML
code would appear as follows:
If you had to memorize all of the tags and their associated attributes, authoring
HTML pages would be an extremely difficult task. Fortunately, there are many
tools on the market today that make page authoring much simpler. Two
popular tools are Microsoft FrontPage and Allaire’s HomeSite. In a visual
authoring environment, you get to concentrate on the content, not the specific
tags that are in use. FrontPage allows you to author the pages as if you were
working with a word processor such as Microsoft Word. At any time however,
you can switch to view the raw HTML code and work with that syntax
interactively. Figure 4 and Figure 5 show Microsoft FrontPage.
What Is ASP?
To review, HTML is about content. How HTML appears is dependent on both
what the server produces and the specific browser being used. If you stay
within the general HTML specification, your HTML documents should have a
consistent appearance across different browsers. All of the code to this point
has dealt with tags. ASP on the other hand, deals with combining HTML and
code that executes on the server. You have two basic choices when it comes to
which language to use. The two primary scripting languages today are
VBScript and JavaScript (JScript). With server-side scripting schemes like
ASP, all of the code executes on the server, not the client. This is why server-
side processing is basically browser independent. As long as you remain
within the HTML specification with regard to what is sent to the client, you
can do just about anything you want on the server.
FoxPro. In many ways, ASP can be viewed as the glue that ties HTML content
and business logic housed in COM Components together.
You may recall that the point was made earlier that HTML does not have
commands. As such, it cannot make decisions such as looping or branching.
ASP allows you to mix the power of conditional branching and looping with
HTML to produce intelligent, self-configuring documents. It is not unlike what
you can do to produce text documents from a Visual FoxPro program using
Visual FoxPro’s text merge features.
The following ASP page calls the Employee class created in the COM chapter
and displays the result of the GetSalary method:
<%@ language=vbscript%>
<html>
<head>
<title>Test</title>
</head>
<body>
<p>Today's date is
<%=Now()%>
.</p>
<p>Salary is
<%=GetSal()%>
.</p>
</body>
</html>
<%
Function GetSal
Dim oEmp
Set oEmp = CreateObject("myServer.employee")
GetSal = oEmp.GetSalary()
'oEmp = Nothing
end Function
%>
Summary
• HTML is used for the development of content that is returned to the
client browser.
• Active Server Pages allow you to combine programming logic with
HTML to create dynamic content.
• An ASP script is a server-side script, written using VBScript or
JScript, and executes on the server prior to the page being returned to
the client. Server-side scripts act as the glue that ties HTML and COM
Components together and allows conditional generation of dynamic
HTML.
• Visual FoxPro COM Components consist of Visual FoxPro
Application code that is encapsulated in a COM DLL and callable as a
server from an Active Server Page.
• You can use Visual InterDev to tie together HTML pages, Active
Server Pages, and Visual FoxPro COM Components into a Web
application.
Lab 5:
Integrating Visual
FoxPro Web
Components
TIP: Because this lab includes a great deal of typed code, we’ve tried to make it
simpler for you. You’ll find all the code in Integrating VFP Web
Components.txt, in the same directory as the sample project. To avoid typing
the code, you can cut/paste it from the text file instead.
Lab 5 Overview
In this lab you’ll learn to create a small Visual FoxPro server and then create
an ASP to make test calls to it.
Objective
In this exercise, you’ll create a simple COM server that exposes the following
methods in order to provide functions that don’t have direct equivalents in
Visual Basic for Applications (VBA), the most commonly used server-side
scripting language in ASPs. You’ll then be able to gain this useful
functionality in your ASP code.
Things to Consider
• How to create a Visual FoxPro server DLL.
• How to test a Visual FoxPro server DLL.
Step-by-Step Instructions
NOTE The following instructions create the server entirely in code. You
may create the equivalent methods in the Class Designer if you
prefer. Don’t forget to mark the class “OLE Public.”
2. Click the Code tab, make sure Programs is highlighted, and click New.
FUNCTION StrTran(cText,cSearch,cReplace)
IF PCOUNT() = 2
RETURN STRTRAN(cText,cSearch)
ELSE
RETURN STRTRAN(cText,cSearch,cReplace)
ENDIF
ENDFUNC
FUNCTION DTOC(uDate,n)
IF PCOUNT() = 1
RETURN DTOC(uDate)
ELSE
RETURN DTOC(uDate,n)
ENDIF
ENDFUNC
ENDDEFINE
4. Save the program as TestServerStart.prg and then click the Build button
in the Project Manager.
oTest = CREATEOBJECT('TestServer.TestServer')
TIP: The DLL is automatically registered on the machine it’s compiled on. The
server name is the name of the project plus the name of the class within that
project (DLL) you wish to create an instance of.
7. Test the server from the Command Window with statements like these:
Objective
In this exercise, you’ll create a very basic Active Server Page that calls the
server and displays the results.
Things to Consider
• Basic ASP coding techniques.
Step-by-Step Instructions
1. This lab assumes that you don’t have any Web page design tools or a
server to test your ASP page. You can get a feel for creating the code even
if you can’t test it.
See Sample.asp
<%@ language=vbscript%>
<html>
<head>
<title>Test</title>
</head>
</body>
</html>
3. Here is the HTML that might be returned to the client browser based on
the previous code:
<html>
<head>
<title>Test</title>
</head>
</body>
</html>
4. If you have a Web server available, and time to install a copy of your
Visual FoxPro “TestServer,” you can test your ASP live. Figure 7 shows
how the finished page would look.