Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

DoCmd, Eval, SetVar, DataToClip, RequestData

These methods all let you drive VFP from a distance. They’re methods of the VFP Automation server. DoCmd, Eval and SetVar let you execute various VFP commands and functions. DataToClip and RequestData take data from an open table and put it on the clipboard or in an array. You can use these methods from within VFP (referencing them through the _VFP object), but they’re far more useful when you’ve created an instance of the VFP Automation Server from some other application.

These functions are typically not used from within a FoxPro program, but rather within the code of another application that calls VFP as an Automation server. Just as we can call Word or Excel to perform their magic for us, we can call on FoxPro from these languages.

Usage

cEmptyString = oApp.DoCmd( cVFPCommand )
uResult = oApp.Eval( cExpr )
cEmptyString = oApp.SetVar( cVarName, uValue )

DoCmd, Eval and SetVar let you issue VFP commands, evaluate expressions and set variables, respectively. The only really tricky thing here is figuring out when you need quotes and when you don’t.

DoCmd accepts any legal VFP command and executes it. (At least, we haven’t yet found one it wouldn’t take.) Use it to set paths, open tables, move record pointers, and so forth. It is sort of like using a voice-dictation system and having to remember to switch from dictation mode to command mode, but it works.

Eval evaluates whatever expression you pass it and returns the result. It’s smart enough to return whatever type you come up with, so you don’t have to do a lot of conversion from character to what you really need. Beware: you pass the expression as a string.

SetVar lets you transfer data that belongs to the client application to the server. You pass it the name of a variable as a string, plus a value. The value is assigned to the variable. If the value is an expression, it’s evaluated by the local client before it’s passed to the VFP Automation server.

We were fairly confused about SetVar for a long time. It seemed to us that you could do the same thing with DoCmd by passing a STORE command. The key difference is that, when you pass a STORE command to DoCmd, the VFP Automation aerver evaluates the new value. When you use SetVar, the client application does the evaluation. So each has its place.

Example

oVFP = createobject("visualfoxpro.application")
oVFP.Visible = .T.  && So we can see it
* Change the background so we can tell the two apart.
oVFP.DoCmd("_SCREEN.BackColor = RGB(64,128,128)")
oVFP.DoCmd("USE [" + ADDBS(_SAMPLES) + ;
           " Data\Customer]")
? oVFP.Eval("DATE()")  && Returns DATETIME()! not date
oVFP.SetVar("lnRecCount",oVFP.Eval([RECCOUNT("CUSTOMER")]))
? oVFP.Eval("lnRecCount")  && Shows 92 records

Usage

uResult = oApp.DataToClip( [ cAlias | nWorkArea ]
                           [, nNumberOfRecords ]
                           [, nDataFormat ])
aArray = oApp.RequestData( [ cAlias | nWorkarea ]
                           [, nNumberOfRecords ] )

Parameter

Value

Meaning

cAlias

 

Character

The alias from which to gather data.

Omitted

If nWorkArea is also omitted, gather data from the current work area.

nWorkArea

Numeric

The work area from which to gather data.

Omitted

If cAlias is also omitted, gather data from the current work area.

nNumberOfRecords

Numeric

The number of records to place on the clipboard or in the array.

Omitted

Put the current record and all remaining records on the clipboard or in the array.

nDataFormat

1 (or 2) or omitted

Separate fields with spaces.

3

Separate fields with tabs.

aArray

Array

An array to hold the specified records.

uResult

Numeric

The number of records returned.

Character

The empty string. Returned only when the specified cAlias is not used or nWorkArea has no table open.

These two methods are quite similar. They both grab one or more records from an open table and do something with them. DataToClip puts them on the clipboard, while RequestData stores them in an array. In both cases, though, you’re left with data that you can manipulate from within another application.

Both methods have one confusing piece of syntax. Although all their parameters are optional, you still need placeholders to omit them. That is, even if you want to use the current work area, you need to put in the comma that precedes the number of records—otherwise, the number is interpreted as a work area number.

DataToClip gives you a choice of output format. Omitting the format or specifying 1 (or the undocumented 2) pads each field with spaces to its full size. Giving a format of 3 pads the fields as needed, then puts a tab between each pair of fields.

Example

* Continuing from the example above
? oVFP.DataToClip()  && Shows 92 records copied
aData = oVFP.RequestData()  && Loads aData with records
? ALEN(aData)   && 1104 elements
? ALEN(aData,1) && 92 rows
? ALEN(aData,2) && 12 fields each

See Also

Application, Evaluate(), _VFP