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

Notes
- I will be using Visual FoxPro's Project Manager for all of the examples. To review the example and code, load the project using the VFP File-Load and select the project file called Frmset.
- The examples and code will use basic VFP syntax, easy to understand and keep things very simple.
- To make things a little easier to follow I placed all the code in command buttons, rather than calling form methods for button action.
- To view the example use screen resolutions set at 800 X 600, as this was the design default.
- All examples that I write will usually be done using VFP 5.0 to ensure all VFP users can work with the example and I don't forget and use a VFP 7.0 command.

Reason for This Article
During the past 6 months that I have been visiting Foxite, I noticed that a lot of the "newbies" and yes even some of the "oldies", have never been exposed to formsets. Microsoft advised that formsets are in VFP for backward compatibility with older Fox 2.x versions. Well so much for Microsoft's option! Formsets are a very powerful tool that you can use, and are applicable to program design in a variety of apps. I hope through this article to expose you to formsets, how to utilize formsets in your applications, and where to use them.


What is a Formset?
A formset is a form that is designed in VFP form designer that consists of more that one form within the SCX. In fact, using a formset you can have many forms all contained within the one SCX. These multiple forms are all loaded when you call the form with the VFP command:

DO FORM Myfmset

All the multiple forms within the formset are loaded into memory, thus calling other forms within the formset results in quick response. As well, all the forms within the formset share a common data environment. All the forms have access to all tables that you add to the formset data environment. Any form within the same formset can reference objects easily on other forms in the formset. An advantage as well; when you load the formset in VFP's form designer, you can see all the forms and move between each form simply by clicking on the form you wish to work on. As an example, lets say you have two forms in the same formset. You currently have the second form in view, and the first form is hidden behind the current form. You want to utilize some value contained in a textbox on the form that is hidden behind your current form2. A simple reference like:

xform1val=Thisformset.Form1.mytextbox.Value

will obtain the value from form1's textbox into the memory variable xform1val. As well, lets say you wish to disable the user's ability to access form1 when form2 is called. A command like:

Thisformset.Form1.Enabled=.F.

will do this. I think you are starting to get the idea! Any form in the formset can easily reference any other form's object and obtain information, or change things.

Where to Use Formsets
Now that you have a brief explanation of what a formset is, lets look at when you might want to consider using them. On the forum over the past 6 months I have seen formsets referenced only a few times. In fact, performing a search of any reference to formsets turned up only one set of threads on the subject. The feeling I have is that the majority think that the tab form is the replacement for formsets. However, there is a big difference between using a tab form and a formset. Granted, both share a common data environment, but the visual aspect and user interaction is what I want to focus on. Many times you have a complicated input requirement and the situation of a Point of Entry Sales system comes to mind. You want to make the user input as simple as possible. You want to reduce what the user is looking at as you lead the user through the various input requirements. There are of course other ways to accomplish this, but formsets will really enable you to do this effectively. Any time you are faced with an application where you want to focus the user's input to minimum (they see less); or you want to lead the user through a series of input forms in a particular order, take a good hard look at formsets.

How to Call Forms within the Formset
Lets take an example where you have a formset with 3 forms in it. When the form is called with the "Do Form Myfrmset" you have form1 visible property set to .T. The other 2 forms within the formset have their visible property set to Visible=.F. so they are not seen. Now you want to see form2 and perform some input. Commands like:

Thisformset.Form1.Enabled=.F. && Disable form1
Thisformset.Form2.Visible=.T. && See form2
Thisformset.Form2.mytextbox.Setfocus && Default focus

will bring up the second form to the exact input box you wish the input to start at and will block the user from clicking on form1 since you disabled it. You may as well wish to make the forms modal. Keep in mind now with form2 up and active you can make use of validation routines to ensure the input is carried out correctly, just like you would do with any form.


Lets Review the Example Code
Now that you have a basic understanding of what formsets are and possibly places in your applications where they are applicable, lets take a tour of a working formset to see how it works.

Notes
- This example code is typical for a POS system where you wish to focus the end user's attention on the input task at hand. As well, you will notice the use of over sized command buttons used for touch screen technology selections.
- The example also demonstrates the use of parent -> child relationship in the main form called Invoice. You will see in the Database Designer the use of referential integrity to look after the clean up of children records if the parent is deleted via a cascade deletion trigger.

Load the Formset and Work with it
Lets move to the command window and run the form, by typing in at the command window:

DO FORM Myfrmset

The main form in the formset called Invoice will appear. Try adding a new Invoice via selecting the Command button named New Invoice.
You will immediately see a form appear that contains Customers in a Grid pick list. Select the customer you wish and select the Confirm button. The Customer pick list will disappear and you will be back to the Invoice screen with the Customer's information auto-filled. Now lets sell the customer some items out of our inventory (Items.dbf). Select the Add button on the bottom of the Invoice screen under the Invoice Details Grid. A pick list of Inventory items will appear. Select one of the items and choose the Confirm button. The Inventory pick list will disappear and you will find yourself back at the invoice screen again with your cursor sitting for you to enter in the quantity sold with the item auto-filled in the grid. Enter in some quantity, in this case enter 3 and press [Enter]. You will see the extended amount for the item sold is updated to the correct amount. Looks pretty easy to create an invoice.. ay !



Once you have some items sold to the customer on the invoice pick the Tender command button. A pick list screen will pop up with Pay Types on it. Select one and then choose the Confirm button. The Paytype screen will disappear and you will again be looking at the invoice screen. Spend some time playing around with this simply invoice program.

After using this example for a while you will start to see how easy it can be to lead the user through a series of forms for specific selections. Keep in mind this example is typical for a point and shoot POS system where the user just touches the options required, and as mentioned before, I have used over sized controls to make the touch selections easy.

Code Under the Hood
Exit the Invoice form and return to the VFP command window. Select the Project Manager Document tab and double clickin the form called myfrmset. The formset will load up in the form designer. You will see that there are 4 forms in this formset:
1. Main invoice form
2. Customer pickllist form
3. Inventory picklist form
4. Paytype picklist form

Reviewing the properties of each form you will note that only the Invoice form has it's visible property set to true, all other forms have the visible property set to false. This means when you call the formset only the main invoice form is seen. Take a look at the code on the invoice form for the command button New Invoice.

THISFORMSET.CUSTOMER.VISIBLE=.T. && See the customer form
THISFORMSET.CUSTOMER.GRID1.SETFOCUS && Where to place the cursor
THISFORM.ENABLED=.F. && Disable invoice form

I disable the invoice form so the user cannot move the customer pick list form out of the way, click on the invoice form and close the customer pick list without selecting a customer to invoice. The user has only two options, select a customer to invoice, or Abort creating a new invoice. Actually in touch screen POS systems I strive to keep user selection options down to point and shoot and try to conform to only two options for major decisions. Confirm, or Abort.

If you look at the code for the Confirm command buttons on the customer pick list form you will see how I reset things back to the main invoice screen.

* Confirm button's code:
SELECT INVOICE
APPEND BLANK
RESTORE FROM INVNO ADDITIVE
x_invno=x_invno + 1
REPLACE INVOICE.INV_NO WITH x_invno
SAVE ALL LIKE x_inv* TO INVNO.MEM
REPLACE INVOICE.CUST_ID WITH CUSTOMER.CUST_ID
REPLACE INVOICE.CUST_NAME WITH CUSTOMER.CUST_NAME
=TABLEUPDATE()
THISFORMSET.INVOICE.ENABLED=.T.
THISFORMSET.INVOICE.REFRESH
THISFORMSET.INVOICE.cmdadd.SETFOCUS
THISFORM.VISIBLE=.F.

Note: I have used a memory file to store the last invoice number and increment it. This is done just to keep this apps simply. In a real POS system I would use another way to auto-increment the invoice number. As well, a full blown POS system would have forms to maintain the customer, inventory(items), and other supporting files. You will also note that I do not add a record to the Invoice table until a Customer has been selected and confirmed. I try never to add a record to any table until after all validations, selections have been made, and the user finally selects the Confirm option.

* Abort button's code:
SELECT INVOICE
THISFORMSET.INVOICE.ENABLED=.T.
THISFORMSET.INVOICE.REFRESH
THISFORMSET.INVOICE.cmdadd.SETFOCUS
THISFORM.VISIBLE=.F.

You will notice that the code behind these two command buttons on the customer picklist form are very simple. Nothing fancy here, just basic VFP code. If you investigate the code that calls the inventory picklist and the tender picklist you will see code that is almost the same.

Changing Background and Foreground Colors
You will notice there are two command buttons on the main Invoice form that calls the Getcolor() function. One sets the forecolor and one sets the backcolor. Using formsets I can easily globally change all the colors on all the forms within the formset via the Setall command.

newcolor=Getcolor()
Thisformset.SetAll("Backcolor",newcolor,"Form")


How to Create Formsets
This is real easy. Just create your first form, and then select the VFP Form-Create Formset option. Once this is accomplished if you view your objects in the form designer's property window, you will see an object at the very top named formset1. You can set the overall formset properties as well. Once this is completed, to add a new form, select the VFP menu option Form-Add a New Form and a new form will appear in the form designer. You then can individually tailor each form in the formset to suit your needs. Remember to set each form's visible property to false if you do not want to see it. There may also be times you want a couple of forms in view at all times as well, and if this is the case leave the forms visible property to true. In order to position the forms in the correct location on the screen for the user when you have more that one form visible, take a look at the Move( ) method for forms. Move the forms to the required screen positions in the formset's Init event.

Conclusions
You will have to decide for yourself if you wish to utilize formsets within your custom applications. I hope from this example you can see the benefits to specifically lead a user through a series of input forms, greatly reducing what the users has to be focused on for input selections. I have made heavy use of formsets in all my POS system for this reason. This kind of thing is almost impossible to do using a tab form, and the only other way is to utilize separate forms. I find the formset approach greatly reduces my development time. This entire example application took only 1 hour to setup and test.

So you tell me, is Microsoft correct that formsets are only for backward compatibility with Fox 2.x versions?

Download code
You can download this article and the program sample here. The download is a zipfile. Its size is 30,511 bytes.

ABOUT THE AUTHOR: PETE SASS

Pete Sass Pete has worked in the computer world since the late 70's. He loves the outdoors and lives in a small town called Marathon, in the north of Canada.

FEEDBACK

Khurram Tahir @ 4/28/2007 1:20:59 PM
Thanks for this article on formsets. One thing which i am facing that formsets take more time to load as compare with a single form loading. I have 7 forms in my formset made for invoice input. I am taking care that only main form is visible on loading whose name is Form1

andien @ 5/18/2008 7:45:48 AM
hello..im andien from indonesia. can u help me... i need source code foxpro about buy transaction or cashier(like as supermarket shop) thnks verry much.
my email:andien_09@yahoo.com

Baby Thomas @ 7/10/2008 3:19:42 PM
This is what i was exactly looking for, thank you very much.

Romel Sipalay @ 1/6/2009 10:14:05 AM
how to create a program using Visual foxpro for Cashie program

SUMAN @ 1/9/2009 11:23:59 AM
I create a formset with form1, form2 & form3 .I want to show all form under form1.How do this work? Help me Please.My ID is sonu_dbg@sify.com.

Mehdi Hasan @ 5/4/2009 6:32:41 AM
Dear PETE SASS

Lot of thanks

K V Kesava Rao @ 9/24/2009 7:31:11 AM
Thanks a lot. I never considered the "form set" option before due to Microsoft statement that it is for backward compatibility!!

Elijah @ 10/16/2009 10:17:24 AM
Thanks. Lovely article. Just started using formsets.. and Im certain I will use them much now that you have given me the knowledge and confidence I need.

Barun Sengupta @ 11/6/2009 7:44:45 AM
thanks for formset. If I want to print inventory challan how do it with customer name and id. help me?

Maureen @ 6/14/2010 5:33:33 AM
Hi! Please help me create a hotel POS with inventory and accounting system using Fox Pro.

jayan @ 12/8/2010 7:25:16 AM
hi please help me create a sales and inventory system.

alceu pedro @ 2/13/2011 2:07:53 PM
I'm from Brazil, a programmer in VFP.
Made a Video
http://www.youtube.com/watch?v=iCbKmu2LZ4M&feature=player_embedded
through the code of FormSet Pete Sass,
Watch the video
My Blog
http://alceu11.blogspot.com/search/label/Videos%20Programacao%20VFP

mildred abanador @ 7/20/2011 4:49:19 PM
kindly help me to make a simple automated water billing system using VFP9.0... thanks a lot and god bless...
my email mildredabanador@yahoo.com

Ashraf Abusada @ 5/29/2013 8:03:13 PM
When using formsets sometime when closing the form in run time and trying to open it again as modify form an error message prompt (file in use) and you must quit VFP9 and start it again.
The solution to this is to Add (Clear Window) in the exit button on the main form of the form set.
Thanks

kkr donta @ 7/29/2013 2:38:43 AM
please help error download
thanks
from KKR2donta2011@hotmail.com

Pete @ 7/29/2013 11:51:09 AM
Contact Eric about this download problem.

Melvin Aznar @ 1/18/2014 6:11:03 PM
Dear Pete Sass
Please Help me create a simple library system for my thesis
thank you very much please do help me
my email is: melvin.aznar_c@yahoo.com
PLEASE HELP ME! THANK YOU VERY MUCH

RAJEEV KUMAR @ 2/12/2014 1:31:12 PM
Dear Mr. Pete Sass
Please help me. How we can save data using form with not allow duplicate data and command button in form in visual foxpro 9.0. i shall be very thankful to you. my email id is rajeeve2002us@yahoo.com

RAJEEV KUMAR @ 2/12/2014 1:31:40 PM
Dear Mr. Pete Sass
Please help me. How we can save data using form with not allow duplicate data and command button in form in visual foxpro 9.0. i shall be very thankful to you. my email id is rajeeve2002us@yahoo.com



Your Name: 
Your Feedback: 

Spam Protection:
Enter the code shown: