FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour create a function with Tdatabase
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
create a function with Tdatabase
Posted: Sat Apr 24, 2021 09:29 AM
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 (fw): Select all Collapse
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 (fw): Select all Collapse
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 March-April 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
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: create a function with Tdatabase
Posted: Sat Apr 24, 2021 11:19 AM

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
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: create a function with Tdatabase
Posted: Sat Apr 24, 2021 11:49 AM
oDbf := TDatabase():Open(,cAlias)


This syntax is wrong. The DBF is already open.

Correct:
Code (fw): Select all Collapse
oDbf := TDatabase():New( SELECT( cAlias ) )
Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: create a function with Tdatabase
Posted: Sat Apr 24, 2021 11:54 AM
Code (fw): Select all Collapse
   oDbf := TDatabase():Open(,cArchivio)

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


Change this as:
Code (fw): Select all Collapse
   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
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: create a function with Tdatabase
Posted: Sun Apr 25, 2021 04:35 PM

thanks

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 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
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: create a function with Tdatabase
Posted: Wed Apr 28, 2021 05:57 PM

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
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: create a function with Tdatabase
Posted: Thu Apr 29, 2021 08:01 AM
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 March-April 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
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: create a function with Tdatabase
Posted: Thu Apr 29, 2021 07:50 PM
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

Continue the discussion