Page 1 of 1

Help on CUSTOM INDEX

PostPosted: Mon Oct 14, 2013 3:45 pm
by MaxP
Hi,

I have a problem with building a custom index.
For example: I have a database with 4 character-fields (
Name1, Name2, Name3, Name4, ...) .and. I want to have every name in my
custom index.

Code: Select all  Expand view

#include "Fivewin.ch"

FUNCTION MAIN()
        LOCAL   aStr := { { "NAME1", "C", 10, 0 }, ;
                          { "NAME2", "C", 10, 0 }, ;
                          { "NAME3", "C", 10, 0 }, ;
                          { "NAME4", "C", 10, 0 } }

        FERASE( "TEST.DBF" )
        FERASE( "TEST.DBT" )
        FERASE( "TEST.NTX" )

        DBCREATE( "TEST.DBF", aStr )

        USE TEST

        INDEX ON NAME1 TO TEST CUSTOM        // Create a CUSTOM INDEX

        APPEND BLANK
        REPLACE NAME1 WITH "First"
        REPLACE NAME2 WITH "First2"
        REPLACE NAME3 WITH "First3"
        REPLACE NAME4 WITH "First4"

        APPEND BLANK
        REPLACE NAME1 WITH "Second"
        REPLACE NAME2 WITH "Second2"
        REPLACE NAME3 WITH "Second3"
        REPLACE NAME4 WITH "Second4"

        APPEND BLANK
        REPLACE NAME1 WITH "Third"
        REPLACE NAME2 WITH "Third2"
        REPLACE NAME3 WITH "Third3"
        REPLACE NAME4 WITH "Third4"

        USE

        USE TEST INDEX TEST

        GO 2
       
        ORDKEYADD()                          // Add default KEY field NAME1

        ORDKEYADD( ,, TEST->NAME2 )          // Add KEY for field NAME2


        IF DBSEEK( "Second    " )
                Msgstop( "Second: FIND" )
        ELSE
                Msgstop( "Second: NOT FIND" )
        ENDIF

        IF DBSEEK( "Second2   " )
                Msgstop( "Second2: FIND" )
        ELSE
                Msgstop( "Second2: NOT FIND" )
        ENDIF


        USE
RETURN NIL
 


The first ORDKEYADD() will work fine, but the following 3 ORDKEYADD()
only will add the Name1 to the index. I will never get Name2, Name3
oder Name4 in my custom index. Whats going wrong??

thanks
Massimo

Re: Help on CUSTOM INDEX

PostPosted: Mon Oct 14, 2013 4:15 pm
by MarcoBoschi
Because "Second2" does not exist in field name1 in any records

NAME1 NAME2 NAME3 NAME4
First First2 First3 First4
Second Second2 Second3 Second4
Third Third2 Third3 Third4

Is it correct?

Re: Help on CUSTOM INDEX

PostPosted: Mon Oct 14, 2013 4:22 pm
by MaxP
Yes,

but I want to add the value of the field Name2 on CUSTOM INDEX

Re: Help on CUSTOM INDEX

PostPosted: Mon Oct 14, 2013 10:17 pm
by Antonio Linares
Max,

Using DBFCDX and specifying the first param in OrdKeyAdd() seems to work fine:

Code: Select all  Expand view
#include "Fivewin.ch"

request DBFCDX

FUNCTION MAIN()
        LOCAL   aStr := { { "NAME1", "C", 10, 0 }, ;
                          { "NAME2", "C", 10, 0 }, ;
                          { "NAME3", "C", 10, 0 }, ;
                          { "NAME4", "C", 10, 0 } }
        FERASE( "TEST.DBF" )
        FERASE( "TEST.DBT" )
        FERASE( "TEST.NTX" )

        DBCREATE( "TEST.DBF", aStr )

        USE TEST VIA "DBFCDX"

        INDEX ON Field->NAME1 TO TEST CUSTOM        // Create a CUSTOM INDEX

        APPEND BLANK
        REPLACE NAME1 WITH "First"
        REPLACE NAME2 WITH "First2"
        REPLACE NAME3 WITH "First3"
        REPLACE NAME4 WITH "First4"

        APPEND BLANK
        REPLACE NAME1 WITH "Second"
        REPLACE NAME2 WITH "Second2"
        REPLACE NAME3 WITH "Second3"
        REPLACE NAME4 WITH "Second4"

        APPEND BLANK
        REPLACE NAME1 WITH "Third"
        REPLACE NAME2 WITH "Third2"
        REPLACE NAME3 WITH "Third3"
        REPLACE NAME4 WITH "Third4"

        USE

        USE TEST VIA "DBFCDX" // INDEX TEST

        GO 2
       
        MsgInfo( ORDKEYADD( 1 ) )                         // Add default KEY field NAME1

        MsgInfo( ORDKEYADD( 1 ,, TEST->NAME2 ) )          // Add KEY for field NAME2

        DbSetorder( 1 )
       
        IF DBSEEK( "Second    " )
                Msgstop( "Second: FIND" )
        ELSE
                Msgstop( "Second: NOT FIND" )
        ENDIF

        IF DBSEEK( "Second2" )
                Msgstop( "Second2: FIND" )
        ELSE
                Msgstop( "Second2: NOT FIND" )
        ENDIF

        USE
RETURN NIL

Re: Help on CUSTOM INDEX

PostPosted: Mon Oct 14, 2013 10:21 pm
by Antonio Linares

Re: Help on CUSTOM INDEX

PostPosted: Tue Oct 15, 2013 5:42 am
by MaxP
many thanks Antonio

Massimo