Passing a parameter to a custom property of a VFP form

It is easy to pass a parameter into a Visual FoxPro form, the syntax is almost exactly the same as when you are passing a parameter into a procedure:

Do myProc With myParameter          && Passing a parameter to a procedure

Do Form myForm With myParameter     && Passing a parameter to a form

The only difficulty arises when you are receiving the parameter into the form.

As with a procedure, you need a Lparameter statement to declare the parameter. The declaration has to go into the Init method of the form and this is where confusion can arise because the Load event fires before Init and might seem to be the more natural place to declare the parameters.

Having declared the parameters in the right method, the next difficulty is that the parameters are local to this method and won't be available anywhere else in the form. Sometimes this doesn't matter. If the parameter is only being used to customise the appearance of the form by setting a label or caption then this can be done in the Init method - remember that the form's Init does not fire until all the controls on the form have been created so the label will exist at this point and we would be able to set its caption.

More often though we'll be wanting to use the parameters sometime later in the lifetime of the form so we need to store them somewhere where they will remain available. One way of doing this would be to create an invisible label on the form and to hold the value here. You might have to do this in Access but Fox allows us to create a custom property of the form and this can be used to hold the parameter's value for the lifetime of the form.

Create a custom property

Select New Property from the Form menu. This will open the New Property dialog as shown below. In this example I've set three attributes of the property:

Visual FoxPro Add Property dialog

  • nOrderNo as the name of the property. This is following the VFP convention that the names of numeric variables start with "n".
  • Zero as the initial value of the property. Without this, the property would be created with a default value of .F. and we might get a data type mismatch if we tried to use the property before it had been initialised.
  • "Order number as an integer" as the description. This field is optional but the description shows up in the Property Window of the Form Designer as shown below and is very useful when you come back to do maintenance work.

Click the Add button to add this property to the form then Close.

I've left the Access Method and Assign Method boxes unticked on the dialog. These would create methods that would run whenever the property was read or written to. In strict object-oriented terms the Access and Assign methods isolate the properties of the object and give us complete control over how these properties are available to other objects in the application. These methods aren't needed in this simple example.

VFP form property window The new property appears under the Other tab of the Visual FoxPro Properties window. Note that the property name is always shown in lower case regardless of how the name was originally entered. This is deliberate because properties are displayed in alphabetical order and a lower-case name forces the custom properties to the bottom of the list where they are easier to find.

If you need to change the property then right-click and select Edit Property/Method... from the shortcut menu or select Edit Property/Method... from the Form menu. Neither of these techniques will let you change the initial value of the property. Deleting and recreating the property seems to be the only way of doing this.

Storing the parameter on the form

Now that the property has been created, the parameter can be stored by a simple assignment in the Init method of the form:

Lparameters tnOrderNo
This.nOrderNo = tnOrderNo

Related Items

Alvechurch Data - Microsoft FoxPro and Access Database Development and Training

Alvechurch Data are based close to Birmingham and provide Microsoft Access and Visual FoxPro training, development and support.

Read More

Autocomplete in Visual Fox Pro

Autocomplete in Visual Fox Pro

The textbox class in Visual FoxPro 9 has a new Autocomplete property which shows the user the previous values that have been entered in that textbox.

Read More

Access and Visual FoxPro Training

Microsoft Access and Visual FoxPro training courses and workshops offered by Alvechurch Data in Birmingham.

Read More

Geoff Franklin CV

Geoff Franklin - resume of education and experience in Access and Visual FoxPro programming.

Read More

Visual FoxPro tips for database developers

Visual FoxPro help and advice for database developers

Read More