Search on customer.dbf

Post Reply
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Search on customer.dbf

Post by Silvio.Falconi »

using customer.dbf, when inserting a record, I should check if the surname (First field) and the name (Last) exist
so you can check records for the same surnames (First)


Image


sample I have

Code: Select all

    @ 12, 10 SAY "First :" OF oDlg SIZE 33, 8 PIXEL
   @ 10, 49 GET aGet[1] VAR cFirst OF oDlg  UPDATE SIZE 100, 12 PIXEL ;
          VALID Custvalid( cFirst, aGet[1], nMode)
          
   @ 26, 10 SAY "Last :" OF oDlg SIZE 22, 8 PIXEL
   @ 24, 49 GET aGet[2] VAR cLast  OF oDlg UPDATE SIZE 100, 12 PIXEL ;
             VALID Custvalid( cLast, aGet[2], nMode)





function Custvalid( cCliente, oGet, nMode)
   local lReturn  := .f.
   local nRecno   := CU->( RecNo() )
   local nOrder   := CU->( OrdNumber() )
   local nArea    := Select()


     //if is empty
     if Empty( cCliente )
      if nMode == 4 .OR. nTag == 2
         RETURN .t.
      else
         MsgStop("this field cannot be empty.")
         RETURN .f.
      endif
   endif


 SELECT CL
   CL->( DbSetOrder( nTag ) )
   CL->( DbGoTop() )

   if CL->( DbSeek( UPPER( cCliente ) ) )
      DO CASE
         Case nMode == 1 .OR. nMode == 3
            lReturn := .f.
            MsgStop("Existing customer.")
         Case nMode == 2
            if CL->( Recno() ) == nRecno
               lReturn := .t.
            else
               lReturn := .f.
               MsgStop("Existing customer.")
            endif
         Case nMode == 4
            lReturn := .t.

      END CASE
   else
      MsgStop("customer not found")
            lReturn := .f.
         endif
      endif
   endif

   if lReturn == .f.
      oGet:cText(space(15)
   else
      oGet:cText( cCliente )
   endif

   CL->( DbSetOrder( nOrder ) )
   CL->( DbGoTo( nRecno ) )

   Select (nArea)


the function only runs well for one field, how can I also check the other one so that I can check the same surnames (but with different names)
example :

Falconi Silvio
Falconi Max
Falconi Paolo

I tried also to make an index on Upper(first)+Upper(last)
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
karinha
Posts: 4882
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil

Re: Search on customer.dbf

Post by karinha »

Please post the example complete.
João Santos - São Paulo - Brasil
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: Search on customer.dbf

Post by Silvio.Falconi »

Image

I 'm using oldest customer.dbf (without ID field)

I wish insert Alan (as first) and Joe( as Last) when I insert "Alan" it return me "Alan" exist and then I cannot insert the lastname

here the test sample

Code: Select all

 
#include 'fivewin.ch'
#include 'xbrowse.ch'



REQUEST DBFCDX
REQUEST DBFFPT
EXTERNAL ORDKEYNO,ORDKEYCOUNT,ORDCREATE,ORDKEYGOTO



function Main()

   local oDlg, oBrw, oFont
   local aData
   local oFilter, oClear
   local lSearch:= .f.
   local cDbf :="customer"
   local cAlias:="CL"

    rddSetDefault( "DBFCDX" )
   setHandleCount( 100 )

   USE CUSTOMER ALIAS CL
   INDEX ON upper(CL->FIRST) TAG CU1 TO CUSTOMER
   INDEX ON upper(CL->LAST) TAG CU2 TO CUSTOMER
   DbCloseAll()


   USE (  cDbf + ".dbf" ) ;
         INDEX (  cDbf + ".cdx" ) ;
         ALIAS ( cAlias ) ;
         NEW


   DEFINE FONT oFont NAME "TAHOMA" SIZE 0,-14
    DEFINE DIALOG oDlg SIZE 640,440 PIXEL

  @ 30, 10 XBROWSE oBrw OF oDlg SIZE 300, 200 PIXEL ;
      AUTOCOLS ALIAS 'CUST' NOBORDER

    oBrw:nMarqueeStyle    = MARQSTYLE_HIGHLWIN7
   oBrw:CreateFromCode()

   @ 10, 20 BTNBMP  PROMPT "add" SIZE 40,20 PIXEL OF oDlg FLAT ;
      ACTION edita( .t.)

   @ 10,100 BTNBMP  PROMPT "Mod" SIZE 40,20 PIXEL OF oDlg FLAT ;
      ACTION edita( .f.)

   ACTIVATE DIALOG oDlg CENTERED
    RELEASE FONT oFont

   return nil

//------------------------------------------------------------------------------------------------------------------//

FUNCTION edita( lAdd )

   LOCAL oDlg
   LOCAL lSave := .F.
   LOCAL oFont
   LOCAL oBtn
   LOCAL oGrp

   LOCAL cFirst
   LOCAL cLast
   LOCAL cStreet
   LOCAL cCity
   LOCAL cState
   LOCAL cZip
   LOCAL dHiredate
   LOCAL lMarried
   LOCAL nAge
   LOCAL nSalary
   LOCAL cNotes
   local aGet[11]
   local nMode
    local aTitle := { "Add a customer",;
                      "Modify a customer"}
   IF lAdd
      CL->(dbGoBottom())
      CL->(dbSkip())
   ENDIF

   cFirst    := CL->FIRST
   cLast     := CL->LAST
   cStreet   := CL->STREET
   cCity     := CL->CITY
   cState    := CL->STATE
   cZip      := CL->ZIP
   dHiredate := CL->HIREDATE
   lMarried  := CL->MARRIED
   nAge      := CL->AGE
   nSalary   := CL->SALARY
   cNotes    := CL->NOTES

   If ladd
      nMode:=1
   else
      nMode:=2
   endif

   DEFINE FONT oFont NAME "MS Sans Serif" SIZE 0, 8
   DEFINE DIALOG oDlg FROM 100, 100 TO 482,916;
                 TITLE aTitle[nMode] PIXEL FONT oFont

   @ 0, 4 GROUP oGrp TO 168, 404 OF oDlg PIXEL


   @ 12, 10 SAY "First:" OF oDlg SIZE 14, 8 PIXEL FONT oFont
   @ 10, 43 GET  aGet[1]  var cFirst    OF oDlg SIZE 105, 12 PIXEL FONT oFont  ;
                VALID Custvalid( cFirst, aGet[1], nMode, 1)

   @ 26, 10 SAY "Last:" OF oDlg SIZE 14, 8 PIXEL FONT oFont
   @ 24, 43 GET aGet[2]  var  cLast     OF oDlg SIZE 105, 12 PIXEL FONT oFont ;
   VALID Custvalid( cLast , aGet[2], nMode,2)

   @ 40, 10 SAY "Street:" OF oDlg SIZE 19, 8 PIXEL FONT oFont
   @ 38, 43 GET aGet[3]  var cStreet   OF oDlg SIZE 155, 12 PIXEL FONT oFont

   @ 54, 10 SAY "City:" OF oDlg SIZE 13, 8 PIXEL FONT oFont
   @ 52, 43 GET aGet[4]  var cCity     OF oDlg SIZE 155, 12 PIXEL FONT oFont

   @ 68, 10 SAY "State:" OF oDlg SIZE 17, 8 PIXEL FONT oFont
   @ 66, 43 GET aGet[5]  var cState    OF oDlg SIZE 15, 12 PIXEL FONT oFont

   @ 82, 10 SAY "Zip:" OF oDlg SIZE 11, 8 PIXEL FONT oFont
   @ 80, 43 GET aGet[6]  var cZip      OF oDlg SIZE 55, 12 PIXEL FONT oFont

   @ 96, 10 SAY "Hiredate:" OF oDlg SIZE 28, 8 PIXEL FONT oFont
   @ 94, 43 GET aGet[7]  var dHiredate OF oDlg SIZE 44, 12 PIXEL FONT oFont

   @ 108, 43 CHECKBOX aGet[8]  var lMarried  PROMPT "&Married:" OF oDlg SIZE 42, 12 PIXEL FONT oFont

   @ 124, 10 SAY "Age:" OF oDlg SIZE 14, 8 PIXEL FONT oFont
   @ 122, 43 GET aGet[9]  var nAge      OF oDlg SIZE 12, 12 PIXEL PICTURE  "99" FONT oFont

   @ 138, 10 SAY "Salary:" OF oDlg SIZE 21, 8 PIXEL FONT oFont
   @ 136, 43 GET aGet[10]  var nSalary   OF oDlg SIZE 40, 12 PIXEL PICTURE  "999999.99" FONT oFont

   @ 152, 10 SAY "Notes:" OF oDlg SIZE 20, 8 PIXEL FONT oFont
   @ 150, 43 GET aGet[11]  var cNotes    OF oDlg SIZE 355, 12 PIXEL FONT oFont

  // @ 172, 4 BUTTON oBtn PROMPT "&Help" OF oDlg SIZE 42, 14 PIXEL FONT oFont // ACTION ()
   @ 172, 318 BUTTON oBtn PROMPT "Conferma" OF oDlg SIZE 42, 14 PIXEL FONT oFont DEFAULT ACTION (oDlg:End(), lSave := .T.)
   @ 172, 362 BUTTON oBtn PROMPT "Annulla" OF oDlg SIZE 42, 14 PIXEL FONT oFont CANCEL ACTION (oDlg:End())

   ACTIVATE DIALOG oDlg CENTERED

   IF lSave
      IF lAdd
         CL->(dbAppend())
      ELSE
         CL->(RLock())
      ENDIF

      CL->FIRST    := cFirst
      CL->LAST     := cLast
      CL->STREET   := cStreet
      CL->CITY     := cCity
      CL->STATE    := cState
      CL->ZIP      := cZip
      CL->HIREDATE := dHiredate
      CL->MARRIED  := lMarried
      CL->AGE      := nAge
      CL->SALARY   := nSalary
      CL->NOTES    := cNotes

      CL->(dbUnlock())

   ENDIF

   RETURN NIL
//-----------------------------------------------------------------------------------------//

 function Custvalid( cCliente, oGet, nMode,ntag)

   local lReturn  := .f.
   local nRecno   := CL->( RecNo() )
   local nOrder   := CL->( OrdNumber() )
   local nArea    := Select()

   if Empty( cCliente )
      if nMode == 4 .OR. nTag == 2
         RETURN .t.
      else
         MsgStop("This field cannot be empty..")
         RETURN .f.
      endif
   endif

   SELECT CL
   CL->( DbSetOrder( nTag ) )
   CL->( DbGoTop() )

   if CL->( DbSeek( UPPER( cCliente ) ) )
      DO CASE
         Case nMode == 1 .OR. nMode == 3
            lReturn := .f.
            MsgStop("Existing customer")
         Case nMode == 2
            if CL->( Recno() ) == nRecno
               lReturn := .t.
            else
               lReturn := .f.
               MsgStop("Existing customer")
            endif
         Case nMode == 4
            lReturn := .t.

      END CASE
   else
      if nMode < 4
         lReturn := .t.
      else
         if MsgYesNo("The customer not exit. ¿ Do you wish add  now ? ")
          //  lReturn := edita( , 1, , , @cCliente )
         else
            lReturn := .f.
         endif
      endif
   endif

   if lReturn == .f.
      iif(nTag==1,oGet:cText(space(15)),oGet:cText(space(15)))
   else
      oGet:cText( cCliente )
   endif

   CL->( DbSetOrder( nOrder ) )
   CL->( DbGoTo( nRecno ) )

   Select (nArea)

return lReturn
//---------------------------------------------------------------------------------------------//















 
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
FranciscoA
Posts: 1964
Joined: Fri Jul 18, 2008 1:24 am
Location: Chinandega, Nicaragua, C.A.

Re: Search on customer.dbf

Post by FranciscoA »

Puedes Intentar adaptar el siguiente codigo de ejemplo?:

Code: Select all

USE CUSTOMER ALIAS CL
   INDEX ON upper(CL->FIRST) TAG CU1 TO CUSTOMER
   INDEX ON upper(CL->LAST) TAG CU2 TO CUSTOMER
   INDEX ON upper(CL->FIRST) + upper(CL->LAST) TAG CU3 TO CUSTOMER
   DbCloseAll()

   ...
   ...

   @ 12, 10 SAY "First:" OF oDlg SIZE 14, 8 PIXEL FONT oFont
   @ 10, 43 GET  aGet[1]  var cFirst    OF oDlg SIZE 105, 12 PIXEL FONT oFont 

   @ 26, 10 SAY "Last:" OF oDlg SIZE 14, 8 PIXEL FONT oFont
   @ 24, 43 GET aGet[2]  var  cLast     OF oDlg SIZE 105, 12 PIXEL FONT oFont 

   @ 172, 318 BUTTON oBtn PROMPT "Conferma" OF oDlg SIZE 42, 14 PIXEL FONT oFont DEFAULT ;
              ACTION if( Valida(cFirst+cLast, 3), ( oDlg:End(), lSave := .T. ), lSave := .f. )

  //------------------------------//  
  Function Valida(cCliente, nTag)
  local lRet := .t.
  local nOrder := CL->( OrdNumber() )

  CL->( DbSetOrder( nTag ) )

  if DbSeek(Upper(cCliente))
     lRet := .f.
  else
     lRet := .t.
  endif

  CL->( DbSetOrder( nOrder ) )

  Return lRet
 
Francisco J. Alegría P.
Chinandega, Nicaragua.

Fwxh1204-MySql-TMySql
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: Search on customer.dbf

Post by Silvio.Falconi »

I too had come to this deduction, but I would prefer to check the insertion first in the dialog
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: Search on customer.dbf

Post by Silvio.Falconi »

sorry I found the solution

I remove the valid because MY customer told me that there can be the same surnames and names
but then change the tax code
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
FranciscoA
Posts: 1964
Joined: Fri Jul 18, 2008 1:24 am
Location: Chinandega, Nicaragua, C.A.

Re: Search on customer.dbf

Post by FranciscoA »

Of course, it is best to organize by identifiers.
Francisco J. Alegría P.
Chinandega, Nicaragua.

Fwxh1204-MySql-TMySql
Post Reply