Passing a parameter into a custom property of a VFP form

It is very 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 or function:

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 at the other end of the process. Receiving the parameter into the form takes a little more care than in a procedure.

Declaring a parameter

As with a procedure or function, the form needs a Lparameter statement to declare the parameter. This declaration has to be the first line of a procedure and you'd expect it to be the first line of the form. Actually it has to go into the form's Init method and this is where the confusion can arise because Init isn't the first method of the form to run. The Load event fires before Init and might seem to be the more natural place to declare the parameters. Logic leads us astray here, the parameter must be declared in the Init.

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. The label and all the other controls will exist at this point and we would be able to use the parameter set its caption.

More often though we'll be wanting to use the parameter sometime later in the lifetime of the form. If so, we'll need to store them somewhere where they will remain available outside the Init method. Object Orientation allows us to create a custom property of the form and if we use this to hold the parameter's value then it will be available for the lifetime of the form.

Create a custom property

The first step is to 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:

Add Property dialog of a Visual FoxPro form

  • nOrderNo is the name that we've given to the new property. This is following the regular VFP naming convention that the names of numeric variables start with an "n".
  • Zero is the initial value of the property. This field is optional but without it the property would be created with a default value of .F. and we risk getting a data type mismatch if we tried to use the property before it had been properly initialised.
  • "Order number as an integer" is the description of the property. This field is also optional but will be very useful later on. Anything entered here will show up in the Property Window of the Form Designer as shown below and will act as a reminder to anyone who has to do any maintenance work.

Click the Add button to add this property to the form then click 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 techniques aren't needed in this simple example.

Using the new property

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

If you need to change the name of 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 new property has been created, the value received as the parameter can be stored by a simple assignment in the Init method of the form:

Lparameters tnOrderNo
*-- Store the order number so that we can use it in the Save method
This.nOrderNo = tnOrderNo

MS Access technical tips

Visual FoxPro technical tips

General Tips

 

More tips from Alvechurch Data

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 Frequently Asked Questions

Frequently asked questions for Visual FoxPro

Read More