create a function with Tdatabase

Post Reply
User avatar
Silvio.Falconi
Posts: 7140
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 1 time

create a function with Tdatabase

Post by Silvio.Falconi »

I would like to have a function that opens an archive and sets a series of indexes
I tried to do this function but it doesn't turn me at all I don't understand where I'm wrong

sample of Usage

oCustomers:Open_Db( "customer", , {FIRST,LAST} )
oCustomers:SetOrder( "FIRST" )
oCustomers:GoTop()



Code: Select all | Expand


FUNCTION Open_Db( cArchivio, aIdx )
   LOCAL cAlias
   LOCAL oDbf
   LOCAL i
   STATIC _Select_
   DEFAULT _Select_ := 0

   cAlias := "TD" + PADL( ++_Select_, 3, "0" )

   DbUseArea( .T. ,, cArchivio, cAlias, .T. )
   IF VALTYPE( aIdx ) == "A"
      FOR i := 1 TO LEN( aIdx )
         DBSETINDEX( aIdx[ i ] )
      NEXT
   ENDIF

   oDbf := TDatabase():Open(,cAlias)

RETURN oDbf



return me a message "TD0001.dbf not avaible"


then I tried with

Code: Select all | Expand

FUNCTION Open_Db( cArchivio, aIdx )
   LOCAL cAlias
   LOCAL oDbf
   LOCAL i
   STATIC _Select_
   DEFAULT _Select_ := 0

   oDbf := TDatabase():Open(,cArchivio)

   IF VALTYPE( aIdx ) == "A"
      FOR i := 1 TO LEN( aIdx )
         DBSETINDEX( aIdx[ i ] )
      NEXT
   ENDIF
RETURN oDbf
 


but make error because

Error description: Error DBCMD/2001 Workarea not in use: ORDLISTADD

Stack Calls
===========
Called from: => ORDLISTADD( 0 )
Called from: ../../../rddord.prg => DBSETINDEX( 0 )

what do you recommend?
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Antonio Linares
Site Admin
Posts: 42537
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 31 times
Been thanked: 77 times
Contact:

Re: create a function with Tdatabase

Post by Antonio Linares »

Silvio,

Class TDataBase has a destructor method, so when the object gets out of scope then it gets called

change local oDbf into static oDbf (or keep it in an array) so the object is not destroyed
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Re: create a function with Tdatabase

Post by nageswaragunupudi »

oDbf := TDatabase():Open(,cAlias)


This syntax is wrong. The DBF is already open.

Correct:

Code: Select all | Expand

oDbf := TDatabase():New( SELECT( cAlias ) )
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
Posts: 10721
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 8 times
Contact:

Re: create a function with Tdatabase

Post by nageswaragunupudi »

Code: Select all | Expand

  oDbf := TDatabase():Open(,cArchivio)

   IF VALTYPE( aIdx ) == "A"
      FOR i := 1 TO LEN( aIdx )
         DBSETINDEX( aIdx[ i ] )
      NEXT
   ENDIF
 


Change this as:

Code: Select all | Expand

  oDbf := TDatabase():Open(,cArchivio)

   IF VALTYPE( aIdx ) == "A"
      oDbf:Exec( <||
         local i
         FOR i := 1 TO LEN( aIdx )
            DBSETINDEX( aIdx[ i ] )
         NEXT
         return nil
         > )
   ENDIF
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
Silvio.Falconi
Posts: 7140
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 1 time

Re: create a function with Tdatabase

Post by Silvio.Falconi »

thanks
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: create a function with Tdatabase

Post by James Bott »

Sivio,

Maybe you didn't know that CDXs have always had the option to open all the indexes automatically when the DBF is opened. And as I understand it, NTXs can now do that also.

REQUEST DBFCDX
rddsetdefault( "DBFCDX" )
SET EXCLUSIVE OFF
SET(_SET_AUTOPEN, .T. )
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
Silvio.Falconi
Posts: 7140
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 1 time

Re: create a function with Tdatabase

Post by Silvio.Falconi »

James Bott wrote:Sivio,

Maybe you didn't know that CDXs have always had the option to open all the indexes automatically when the DBF is opened. And as I understand it, NTXs can now do that also.

REQUEST DBFCDX
rddsetdefault( "DBFCDX" )
SET EXCLUSIVE OFF
SET(_SET_AUTOPEN, .T. )


yes I knew it but it happens that in my application there is a function that does not have to load the indexes or in one of the functions that I have seen that does not load them all
As you well know in my old applications made for my school I had problems because sometimes the indexes were missing,
so not checking them first when opening the application seems to me a stretch because I am always afraid of not finding them in that folder
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: create a function with Tdatabase

Post by James Bott »

yes I knew it but it happens that in my application there is a function that does not have to load the indexes or in one of the functions that I have seen that does not load them all


Hmm, I always open the indexes whether they are used or not. This simplifies coding doesn't have a downside that I can think of.

If you are using database objects, in order to not open the indexes sometimes, it would mean you would have to pass a parameter and when looking at the code you could never tell if the indexes were open or not.

Is there a reason you need them to NOT have the indexes open?
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Post Reply