Share via


Controlling Visual FoxPro from Other Applications

Because Visual FoxPro acts as a server (with level 2 compliance) as well as a client, applications that support Automation can create instances of Visual FoxPro, run Visual FoxPro commands, and access Visual FoxPro objects.

You control Visual FoxPro from other applications by using the Visual FoxPro Application object. An Application object is automatically created whenever Visual FoxPro is launched, either directly, through DDE or through Automation.

For example, the following lines of code in Visual BasicĀ®, or a Microsoft Excel module create a reference to a Visual FoxPro application object:

Dim oFox as Object
Set oFox = CreateObject("VisualFoxPro.Application")

Once you have a reference to the Visual FoxPro Application object, you can call methods associated with the application object and access other objects through the collection properties of the Application object.

Application Object Methods

DataToClip

Help

DoCmd

Quit

Eval

RequestData

The following example uses Visual Basic for Applications code in an Excel module to create a Visual FoxPro Application object, open a Visual FoxPro table, and add the results of a query to the active spreadsheet:

Sub FoxTest()
Dim oFox as Object
Set oFox = CreateObject("VisualFoxPro.Application")

oFox.DoCmd "USE customer"
oFox.DoCmd "SELECT contact, phone FROM customer 
   WHERE country = " + Chr$(39) + USA+ Chr$(39) + " INTO CURSOR cust"
oFox.DataToClip "cust",,3
Range("A1:B1").Select
ActiveSheet.Paste
End Sub

The Visual FoxPro Application Object Model

An application object is automatically created whenever Visual FoxPro is launched, either directly, through Automation or DDE. This application object provides access to all other objects created in a Visual FoxPro session through Collection properties.

Visual FoxPro application object model

Visual FoxPro application object model graphic

Accessing Objects Through Collection Properties

The Visual FoxPro application object and all container objects in Visual FoxPro have a count property and a collection property associated with them. The collection property is an array referencing each contained object. The count property is a numeric property indicating the number of contained objects.

The following table lists objects and the corresponding collection and count properties.

Object

Collection Property

Count Property

Application

Objects Forms

Count FormCount

FormSet

Forms

FormCount

Form

Objects Controls

Count ControlCount

PageFrame

Pages

PageCount

Page

Controls

ControlCount

Grid

Columns

ColumnCount

CommandGroup

Buttons

ButtonCount

OptionGroup

Buttons

ButtonCount

Column

Controls

ControlCount

ToolBar

Controls

ControlCount

Container

Controls

ControlCount

Control

Controls

ControlCount

These properties allow you to use a program loop to manage all or specific contained objects. For example, the following lines of code set the Visible property of all forms to True (.T.):

FOR EACH Form IN Application.Forms
   Form.Visible = .T.
ENDFOR

See Also

Other Resources

Automation and COM Servers

Sharing Information and Adding OLE