How to Add a Table to a Form's Data Environment at Run Time (147652)



The information in this article applies to:

  • Microsoft Visual FoxPro for Windows 3.0
  • Microsoft Visual FoxPro for Windows 3.0b
  • Microsoft Visual FoxPro for Windows 6.0

This article was previously published under Q147652

SUMMARY

A table or view may be added to a Visual FoxPro form at run time. Although you can open a table with the USE command, there are benefits to adding it to the Data Environment of the form. If a form is set to use a Private Data Session, the table will only be added to the Data Environment of the current Data Session.

MORE INFORMATION

Step-by-Step Example

  1. Create a new form by using the Form Designer.
  2. Set the DataSession property of the form to 2-Private Data Session.
  3. Add a command button to the form, and place the following code in its Click event:
       WITH THISFORM.DataEnvironment
         .ADDOBJECT("mycursor","cursor")
         .mycursor.Database= SYS(2004)+"samples\data\testdata.dbc"
         .mycursor.CursorSource="customer"
         .CloseTables()  && Closes all the tables and views associated
                         && with the data environment.
         .OpenTables()   && Opens all the tables and views associated
                         && with the data environment.
       ENDWITH
  4. Save the form. Then run two instances of the form.
  5. Click the command button on the first instance of the form, but don't click the command button on the second instance of the form.
  6. In the debug window, type:
       _SCREEN.ACTIVEFORM.DataEnvironment.mycursor.CursorSource
  7. As you switch between the forms, notice the value in the Debug Window changes according to which DataSession is the active one. The table is only in the Data Environment of the first instance of the form.

Modification Type:MajorLast Reviewed:10/15/2003
Keywords:kbcode KB147652