How to Add the Data Environment to a Form Programmatically (142885)



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 Q142885

SUMMARY

When a form is created using the Form Designer, a DataEnvironment object is automatically added to the form. To simulate this behavior in a programmatically created form, a DataEnvironment property must be declared for the form. Then a pointer to a DataEnvironment object may be stored in the form's DataEnvironment property.

MORE INFORMATION

  1. Create a new program and type the following code:
       oform1=CREATEOBJECT("Myform")  && Instantiate the form
       oform1.SHOW(0)
       oform1.refresh
       READ EVENTS
       RETURN
    
       DEFINE CLASS Data1 AS dataenvironment   && DataEnvironment Class
         Name = "Dataenvironment"
         ADD OBJECT "Cursor1" AS MyCursor      && ADD OBJECT for each
       ENDDEFINE                               && cursor in dataenvironment
    
       DEFINE CLASS MyCursor AS cursor         && Cursor Class
         Alias = "customer"
         Database = "C:\vfp\samples\data\testdata.dbc"
         CursorSource = "customer"
         Name = "Cursor1"
       ENDDEFINE
    
       DEFINE CLASS Myform AS form
         Top = 0
         Left = 0
         Height = 386
         Width = 587
         DoCreate = .T.
         Caption = "Form1"
         Name = "FORM1"
         DataEnvironment=""                && DataEnvironment is a property
                                           && of the form.
         PROCEDURE Destroy
           WAIT WINDOW PROGRAM() TIMEOUT 2
           THISFORM.DATAENVIRONMENT.CLOSETABLES  && Close tables at the end
         ENDPROC                                 && of destroy procedure.
    
         PROCEDURE Unload
           CLEAR EVENTS
         ENDPROC
    
         PROCEDURE Load
           * The DataEnvironment is instantiated and the form property
           * DataEnvironment references the object. The DataEnvironment
           * is never actually added to the form. THISFORM.DATAENVIRONMENT
           * is just a pointer to the DataEnvironment object.
    
           THISFORM.DATAENVIRONMENT=CREATEOBJECT("Data1")
    
           * Open the tables at the beginning of the LOAD.
           THISFORM.DATAENVIRONMENT.OPENTABLES
    
           WAIT WINDOW PROGRAM() TIMEOUT 2
         ENDPROC
       ENDDEFINE
    							
  2. Open the Debug window.
  3. Type the following on the left side of the debug window
       _SCREEN.ACTIVEFORM.DATAENVIRONMENT.NAME
    							
  4. Run the program created in step 1.
  5. Note that the debug window correctly displays Dataenvironment as the value of _SCREEN.ACTIVEFORM.DATAENVIRONMENT.NAME

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