Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss. print.

In this tutorial we will create a set of CA’s that will be used for lookups only. If your fields have a large number of tables then you need the excellent CABuilder created by Mark McCasland and found at: http://www.foxite.com/downloads/default.aspx?id=161.

Step 1
From the menu select File/New Class – Create a new class as shown below.

When the class opens in the class designer, Rt. Click it and select “Builder”. Enter a dummy name for the alias. We will change this later.

Select the data Source. Here we select ODBC to connect to a MSDE data source.

Either use the connection string or an existing DSN. Here we have used a Trusted Connection String. Click “Test Connection”.

All is well so far and we have connected. Click OK and “OK” in the builder to close it and save the changes.

Step 2

Now we need to make some generic changes to the baseclass. Add the following new properties to the base class:

cFilterClause – This is where you will add your WHERE clause
cOrderBy – This is the ORDER BY clause of our select
cSelectCmd – This is where the guts of the Select command will go
lNoDataOnLoad – The flag whether we want data to be loaded. Set this to .T.

Open the BeforeCursorFill method and insert the following code:

LPARAMETERS luseCursorSchema, lNoDataOnLoad, cSelectCmd

* set the no data on load from our property
lNoDataOnLoad = This.lnodataonload 

* set the select command
IF NOT EMPTY(This.cselectcmd)
     cSelectCmd = This.cselectcmd

* set the where clause/s
IF NOT EMPTY(This.cfilterclause)
     cSelectCmd = cSelectCmd + [ WHERE ]+ This.cfilterclause

* set the orderby clause
IF NOT EMPTY(This.corderby)
     cSelectCmd = cSelectCmd + [ ORDER BY ]+ This.corderby

Close and save the caBaseClass. We will subclass it for our other classes.

Step 3

Create a new class and base it on the caBaseclass created above. This is important so that the new class inherits our settings.

Click OK to open it in the Class Designer. Rt Click and select Builder. Give the cursor a name.

Click the DataAccess tab and then the Build button so that you can build fields with the correct syntax.

Copy the resultant code as displayed. We may not actually be able to save this because of the 255 chr restriction in a property. We just do this to build the correct select command. Also this is useful for generating our schema which can also be copied if required.

Hint: you can also get the correct syntax by using the VFP Query builder on the same source table and then checking the generated SQL. This is useful for joins etc. Delete the Select and the Generated Cursor Schema and click OK to save changes. Open the properties and change our custom properties as below.

Close and save this class. Let’s test this out.

Step 4: Testing the Class
Create a new form. Open the DE and RT click. Select Builder. Do NOT add any tables to the DE(click cancel). Select the Cursors Tab of the builder. Click Add and navigate to your class you just created – caCustomer. Rename it from Cursor1 to caCustomer. Click OK to close the CA builder and OK again to close the DE builder. crsCustomer has been added to the DE.

To see the form in action you need a grid on the form.
Note: If we have no Schema in the CA we have no fields. This can be got around like this. Create a new form, open its DE and add the CUSTOMER table to it from NATIVE DBC. Rename the cursor Alias as crsCustomer(same as our CA) . Drag and drop it on the form. The grid now points to Alias crsCustomer and has ALL the fields from the table. Copy the grid and paste on our CATest form. Add objects to the form as shown.

Add this code in the cmdGetData.Click()

cClient = ALLTRIM(ThisForm.txtclientno.Value)
WITH ThisForm.grdCrscall 
     .Recordsource = ""
     WITH ThisForm.dataenvironment.cacall
          .lNoDataOnLoad = .F. && tell it we WANT data 
          .cFilterclause = [Call.Clientno LIKE ']+cClient+[%']
     .Recordsource = "crscall"

Now run the form, enter an id or part of an id in the text box and click to get the data in the grid. This ends the tutorial. These CA’s are non updatable and may be used only for lookups. To make them updatable you will need to set additional properties in the Builder. This is beyond the scope of this tutorial. On the CoDe magazine site is an excellent article: Introducing the CursorAdapter Class. I have created this tutorial to aid the development of CursorAdapters after spending many frustrating hours trying to set them up. This is aimed squarely at the newbie to CursorAdapters.

Have fun and may the Fox live long.


Bernard Bout Bernard Bout is a long time member of the Foxite.COM Community and based in Brisbane, Australia. Bernard's weblog is here: http://weblogs.foxite.com/bernardbout/


Benny Thomas @ 12/3/2008 10:56:28 AM
Dear Mr. Bernard,

I am just trying to start learning CursorAdapters. In Step 1, you start with a class named caBaseClass. Thereafter, your image shows Class name "cacall". At what point, the name change from caBaseClass to caCall? (Should I make a new class?) I do not get this. I am using VFP9 and I have no SQL Server installed so I have to try this on a Native database. Thanks for your guidance.

Benny Thomas

johnmar carida @ 3/28/2013 7:35:24 AM
hello there,,i just want to know what is the codes for selecting multiple data. thank you hope you could help me.more power

Ulises Zamora @ 10/22/2015 3:48:06 AM
thank you very much for your contribution, it has been very clear. Hence it comes to me the following query.
I made a form that uses 8 tables in a database firebird, through CursorAdapter and joined the DataEnviroment the form. Despite having followed their directions in the form does not recognize me all the tables I need.

As you might use multiple tables of the database firebird on the same form?

Your Name: 
Your Feedback: 

Spam Protection:
Enter the code shown: