Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to change Cursor Source property - Data Environment.

Status
Not open for further replies.

JWilsonny

Programmer
May 8, 2001
46
US
Please excuse my ignorance. I have a form (Aform) with the Data Environment set to a specific path and file name (cursorsource property = d:/path/filename). I want this to be a different path and set to a variable (f_namepath) so I can change the file name and path programatically. Property is greyed out when i modify the form.

1. When does the form grab the Data Environment during program execution?

2. When and how and where should I change it? Init or Load event for the form?

Thanks - JW
 
JW,

This is not as easy as you might think. The problem is that the property in question is read-only. Also, by the time you get to the form's Load or Init, the DE will already have opened the tables.

However, the situation isn't that bad. When you run the app on the user's system, the DE will look for the table in the specified location. If it can't find it, it will then look in the default directory, and in every directory on the search path. So provided the paths have been set correctly (using SET DEFAULT and SET PATH), all will be well.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
Thanks Mike .. would it be acceptable to delete the data environment table from the form and just open in in the [init event] ie Select and Use. Would I still have buffering avalable? JW
 
This is not as easy as you might think. The problem is that the property in question is read-only. Also, by the time you get to the form's Load or Init, the DE will already have opened the tables.

Mike I disagree with the fact that changing the path of the cursors in dataenvironment is not changable. I use this technique all the time. But I put the code in the load of the form rather than the init, which doesn't seem to be too late. Here is a small portion of it.
Code:
IF AMEMBERS(laCursor,ThisForm.DataEnvironment,2)>0
   FOR lnCompteur=1 TO ALEN(laCursor)
      lcExec='ThisForm.DataEnvironment.'+laCursor[lnCompteur]
      =AMEMBERS(laCursor2,&lcExec)
      IF laCursor2[1]='ALIAS'
         IF NOT ThisForm.FixPath(ALLTRIM(laCursor[lnCompteur]))
            RETURN .F.
         ENDIF
      ENDIF
   NEXT
ENDIF




Would I still have buffering avalable?

Yes, but you also need to set it in the init of the form.
Code:
Use mytable1 shared agin in 0
cursorsetprop("buffering",5,"mytable1")
Use mytable2 shared agin in 0
cursorsetprop("buffering",5,"mytable2")
Use mytable3 shared agin in 0
cursorsetprop("buffering",5,"mytable3")[/quote]

P.S. And since the dataenvironment is not going to close those tables, do forget to close them yourself in the queryunload of the form. But as I said to Mike you can switch database (provided they are of the same structure), at the start of an application, even though the paths are hard coded. Posting the code I use might not neccessary at this point, but just so you know it is possible.





[b][i]Mike Gagnon [/i][/b]

[i]If you want to get the best response to a question, please check out FAQ184-2483 first.[/I]
 
Mike,

But I put the code in the load of the form rather than the init, which doesn't seem to be too late

Well, I might be wrong, but I always thought that the Load fires after the tables are opened. If you need to do anything before opening the tables, I thought that the place to do it was in the BeforeOpenTables in the DE. Is that right?

Also, what exactly does your code do? What is FixPath()? Presumably, it iterates the cursors, changing the CursorSource setting, but I don't see how you can do that if CursorSource is read-only.

JW,

would it be acceptable to delete the data environment table from the form and just open in in the [init event] ie Select and Use.

Yes, but it would probably be better if you do it in the Load rather than the Init. That way, the tables would be open before you initialise the controls on the form.

But you don't need to delete the DE. Keep it in place, but set the AutoOpenTables and AutoCloseTables properties to .F.

Would I still have buffering avalable?

As Mike indicated, you can switch on buffering regardless of whether the tables are in the DE.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
Also, what exactly does your code do? What is FixPath()? Presumably, it iterates the cursors, changing the CursorSource setting, but I don't see how you can do that if CursorSource is read-only.

This is the fixpath() function. It allows me to work on both the development environment and runtime (depending on which path and which database the ini file is pointing to)



Code:
* Use to fix the path for a table
* expC1 Name of the cursor we have to modify
* expC2 Alias (if passed will override the default cursor alias)

LPARAMETER tcCursor,tcAlias

LOCAL lcObject

lcObject="ThisForm.DataEnvironment."+tcCursor

WITH &lcObject
   SELECT TABLE

   * We will locate for record in TABLE.DBF
   IF NOT EMPTY(tcAlias) AND TYPE('tcAlias')='C'
      * We need to replace the Alias and the CursorSource properties
      * because we will override the cursor
      SEEK UPPER(ALLTRIM(tcAlias)) ORDER ALIAS
      STORE tcAlias      TO .Alias
      STORE ALLTRIM(NOM) TO .CursorSource
      ELSE

      * If this is a view
      IF UPPER(.CursorSource)='VIEW'
         LOCAL lcDatabase
         lcDatabase=SUBSTR(.Database,RAT('\',.Database)+1)
         STORE oApp.cData+'\'+lcDatabase TO .Database
         RETURN
      ENDIF

      LOCAL lcAlias,lnAt
      lcAlias=SUBSTR(.CursorSource,RAT('\',.CursorSource)+1)
      lnAt=AT('.',lcAlias)
      IF lnAt>0
         lcAlias=SUBSTR(lcAlias,1,AT('.',lcAlias)-1)
      ENDIF
      =SEEK(UPPER(lcAlias),'TABLE','ALIAS')
   ENDIF

   * We will replace the database property for those that are in a DBC
   IF NOT EMPTY(DBC)

      IF oApp.lMainIni
         STORE oApp.cData+'\'+ALLTRIM(DBC)+'.dbc' TO .Database
         ELSE
         STORE ALLTRIM(DBC)+'.dbc' TO .Database
      ENDIF

      ELSE      

      * We will replace the CursorSource property with the full path
      * for those not part of a DBC
      STORE ALLTRIM(CHEMIN)+'\'+ALLTRIM(NOM)+'.dbf' TO .CursorSource

      * If we are in a pick list or in a grid admin
      * Because of a bug in Visual FoxPro 5, we are not allowed
      * anymore to set the property of Database to blank
      * This means we will use a technique to add a cursor here
      * in order to have it blank at first
      IF ThisForm.Name='recherche'
         ThisForm.DataEnvironment.RemoveObject('Cursor1')
         ThisForm.DataEnvironment.AddObject('Cursor2','Cursor')
         ThisForm.DataEnvironment.Cursor2.CursorSource=ALLTRIM(CHEMIN)+'\'+ALLTRIM(NOM)+'.dbf'
         ThisForm.DataEnvironment.Cursor2.Alias=ALLTRIM(NOM)
         ThisForm.DataEnvironment.Cursor2.BufferModeOverride=2
         ThisForm.DataEnvironment.InitialSelectedAlias=ALLTRIM(NOM)
      ENDIF

      * We need to remove the database
*     .ResetToDefault('Database')

   ENDIF
   
   * If we are in adding mode, we have to change the buffering property to optimistic
   * to have the possibility to create more than one record at a time for this form
   IF TYPE('oApp.nRecnoToProcess')='N' AND oApp.nRecnoToProcess=-1
      STORE 5 TO .BufferModeOverride
   ENDIF

ENDWITH


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Thanks Mike and Mike.

I was able to avoid the whole issue by setting the path in my main prg, but I may use Mike(s) ideas also. Good info on the DE! Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top