In Working up my Incremental search Antonio suggested to use On Change on a get to trap the values being typed into a field.
- Code: Select all Expand view
REDEFINE GET oLname var cLname ID 153 of oFld:aDialogs[1] ;
ON CHANGE ( _Isearch( oLname, cLname, oLbx, oRsCust )) UPDATE
I created a Function to pass and trap the value of the Get ( cLname ) in the function _Isearch().
- Code: Select all Expand view
//-------------
Static Func _Isearch( oLname, cLname, oBrw, oRsCust )
cLname := alltrim(cLname )
Msginfo( cLname) // rem this out and the function will fail
If len( cLname ) = 0
oRsCust:Filter := ""
oRsCust:Filter := "[Last Name] = 'bogus'"
oBrw:ReFresh()
oLname:SetFocus()
Return(.t.)
Endif
If len( cLname ) >= 3
oRsCust:Filter := ""
oRsCust:Filter := "[Last Name] like '"+cLname+"%'"
If oRsCust:eof
MsgInfo( "No Find" )
oLname:SetFocus()
Return(.t.)
Endif
oBrw:ReFresh()
oLname:SetFocus()
SysReFresh()
Return(.t.)
Endif
oLname:SetFocus()
Return(.T.)
I put a few MsgInfo() in to capture the value and lengths of cLname and I do get values and the above code works GREAT ! What is strange .. when you rem out the MsgInfo's .. the code fails to work and the value of cLname never seems to be evaluated.
I have included the complete code and resource .. Click on the button bar for Customer Info then start typing the the Last Name Lipkin and you will see the value of cLname and after len >= 3 .. the browse will fire and the results will be evaluated ..
Go Back and rem out the MsgInfo and you will notice cLname never seems to get evaluated.
Any advice on what I am missing would be appreciated!
Rick Lipkin
See the entire code here :
- Code: Select all Expand view
// Incremental search
#include "FiveWin.ch"
#Include "Xbrowse.ch"
STATIC oWnd,oBar,lOK,lOk1,oWndChild,oLbx
//-------------
Func Main()
Local catNewDB,xProvider,cFile,aDir,dExe,cDefa,mStart
Local oCn,cSql,oErr,oRsCust,cLOGIN,Saying,xLogin,cRights
Local cTitle,oButt1,oButt2,nYear,nScr1,nScr2,xMessage,cRdd
PUBLIC xCONNECT
lOK := .F.
//-- get timestamp on .exe //
cFILE := GetModuleFileName( GetInstance() )
aDIR := DIRECTORY( cFILE )
dEXE := aDIR[1] [3]
// where .exe started from is default directory //
mSTART := RAT( "\", cFILE )
cDEFA := SUBSTR(cFILE,1,mSTART-1)
aDIR := NIL
SET DEFA to ( cDEFA )
SET DELETED on
SET CENTURY on
SET 3DLOOK on
nYEAR := ( year( DATE() )-30 )
SET EPOCH to ( nYEAR )
REQUEST DBFCDX
rddsetdefault ( "DBFCDX" )
nSCR1 := GetSysMetrics(0)
nSCR2 := GetSysMetrics(1)
xPROVIDER := "Microsoft.Jet.OLEDB.4.0"
xSOURCE := cDEFA+"\Rick.mdb"
cRDD := xPROVIDER+" -- "+xSOURCE
// global connection string
xCONNECT := 'Provider='+xPROVIDER+';Data Source='+xSOURCE
If .not. File( cDefa+"\Rick.mdb" )
Ferase( cDefa+"\Rick.mdb" )
// create the adox object
Try
catNewDB := CreateObject("ADOX.Catalog")
Catch
MsgInfo( "Could not Create ADOX object")
Return(.f.)
End try
// create the table Rick.mdb
Try
catNewDB:Create('Provider='+xProvider+';Data Source='+xSource+';Jet OLEDB:Engine Type=5' )
Catch
MsgInfo( "Could not create the table "+xSource )
Return(.f.)
End Try
Try
oCn := CREATEOBJECT( "ADODB.Connection" )
Catch
MsgInfo( "Could not create the ADO object for connection")
End Try
TRY
oCn:Open( xCONNECT )
CATCH oErr
MsgInfo( "Could not open a Connection to Database "+xSource )
RETURN(.F.)
END TRY
cSQL := "CREATE TABLE CUSTOMER"
cSQL += "( "
cSQL += "[CUSTOMEREID] char(18) NOT NULL, "
cSQL += "[LAST NAME] char(30) NULL, "
cSQL += "[FIRST NAME] char(30) NULL, "
cSQL += "[MID INIT] char(30) NULL, "
cSQL += "[ADDRESS1] char(30) NULL, "
cSQL += "[CITY] char(30) NULL, "
cSQL += "[STATE] char(30) NULL, "
cSQL += "CONSTRAINT PK_USERINFO PRIMARY KEY ( CUSTOMEREID )"
cSQL += " )"
Try
oCn:Execute( cSQL )
Catch
MsgInfo( "Table CUSTOMER Failed" )
Return(.f.)
End try
oCn:Close()
oCn := nil
Endif
xLOGIN := UPPER( WNetGetuser() )+space(8) // fivewin
xLOGIN := SUBSTR(xLOGIN,1,8)
oRsCust := TOleAuto():New( "ADODB.Recordset" )
oRsCust:CursorType := 1 // opendkeyset
oRsCust:CursorLocation := 3 // local cache
oRsCust:LockType := 3 // lockoportunistic
// check for very first user
cSQL := "SELECT * FROM CUSTOMER"
TRY
oRsCust:Open( cSQL, xCONNECT )
CATCH oErr
MsgInfo( "Error in Opening CUSTOMER table here" )
RETURN(.F.)
END TRY
If oRsCust:eof
oRsCust:AddNew()
oRsCust:Fields("CustomerEid"):Value := "011111111111111111"
oRsCust:Fields("Last Name"):Value := "Lipkin"
oRsCust:Fields("First Name"):Value := "Richard"
oRsCust:Fields("Mid Init"):Value := "M"
oRsCust:Fields("Address1"):Value := "123 Anywhere"
oRsCust:Fields("City"):Value := "Columbia"
oRsCust:Fields("State"):Value := "SC"
oRsCust:Update()
oRsCust:AddNew()
oRsCust:Fields("CustomerEid"):Value := "011111111111111112"
oRsCust:Fields("Last Name"):Value := "Lipinsky"
oRsCust:Fields("First Name"):Value := "Jason"
oRsCust:Fields("Mid Init"):Value := "S"
oRsCust:Fields("Address1"):Value := "123 Arborgate"
oRsCust:Fields("City"):Value := "Columbia"
oRsCust:Fields("State"):Value := "SC"
oRsCust:Update()
oRsCust:AddNew()
oRsCust:Fields("CustomerEid"):Value := "011111111111111113"
oRsCust:Fields("Last Name"):Value := "Lipkin"
oRsCust:Fields("First Name"):Value := "Beth"
oRsCust:Fields("Mid Init"):Value := " "
oRsCust:Fields("Address1"):Value := "123 Lake Murray Blvd"
oRsCust:Fields("City"):Value := "Lexington"
oRsCust:Fields("State"):Value := "SC"
oRsCust:Update()
oRsCust:AddNew()
oRsCust:Fields("CustomerEid"):Value := "011111111111111114"
oRsCust:Fields("Last Name"):Value := "Lizzarous"
oRsCust:Fields("First Name"):Value := "Tim"
oRsCust:Fields("Mid Init"):Value := "J"
oRsCust:Fields("Address1"):Value := "456 Broad River"
oRsCust:Fields("City"):Value := "Irmo"
oRsCust:Fields("State"):Value := "SC"
oRsCust:Update()
Endif
cRIGHTS := "(RWS)"
xMESSAGE := "User "+xLOGIN+" Rights "+cRIGHTS+ ;
" Default= "+cDEFA+" Rdd= "+cRDD+ ;
" Revision "+DTOC(dEXE)+;
" -r"+str(nSCR1,4)+" x "+STR(nSCR2,4)
cTitle := "Test Incremental Search with ON CHANGE"
DEFINE WINDOW oWnd ;
TITLE cTITLE ;
MENU BuildMenu();
MDI
DEFINE BUTTONBAR oBar OF oWnd SIZE 65,70 3DLOOK 2007
oBar:SetColor(0)
DEFINE BUTTON oButt1 OF oBar FileName (cDefa+"\Icons\REQUEST.bmp") , ;
(cDefa+"\Icons\DREQUEST.bmp"), ;
(cDefa+"\Icons\DREQUEST.bmp") ;
MESSAGE "Customer Information" ;
ACTION _Custview( "A",oWnd,oButt1,oButt2 ) ;
PROMPT "Customer Info"
DEFINE BUTTON oButt2 OF oBar FileName (cDefa+"\Icons\CLOSE.bmp"), ;
(cDefa+"\Icons\DCLOSE.bmp"),;
(cDefa+"\Icons\DCLOSE.bmp") ;
MESSAGE "Close Application" ;
ACTION ( oWnd:End() ) ;
PROMPT "Quit"
SET MESSAGE OF oWnd ;
to xMESSAGE CLOCK 2007
ACTIVATE WINDOW oWnd MAXIMIZED ;
VALID ( IIF( !lOK, ExitPgm(.T.), .F. ))
RETURN( NIL )
//---------------------------
Static FUNCTION BuildMenu()
LOCAL oMENU, cDEFA
cDEFA := SET(7)
MENU oMenu 2007
menuitem "Login..." ;
MENUITEM "&About..."
MENUITEM "&Quit" ;
MESSAGE "Close this program";
ACTION oWND:END()
ENDMENU
RETURN( oMenu )
//-----------------------
Static FUNCTION ExitPgm( lCLEAN )
LOCAL lOK3
lOK3 := .F.
IF lCLEAN = .T.
lOK3 := .T.
lOK := .T.
SET RESOURCES to
ENDIF
RETURN( lOK3 )
//-------------------------------
Static FUNC _Custview( cMODE,oWnd,oBtn1,oBtn2 )
LOCAL SAYING,cDEFA
LOCAL cTITLE
LOCAL oIco,oFld,oCust
LOCAL oRsCust,cSql
lOK1 := .F.
cSQL := "SELECT * from CUSTOMER order by [Last Name]"
oRsCust := TOleAuto():New( "ADODB.Recordset" )
oRsCust:CursorType := 1 // opendkeyset
oRsCust:CursorLocation := 3 // local cache
oRsCust:LockType := 3 // lockoportunistic
TRY
oRsCust:Open( cSQL,xCONNECT )
CATCH oErr
MsgInfo( "Error in Opening CUSTOMER table" )
RETURN(.F.)
END TRY
SysReFresh()
cTITLE := "Customer Maintenance"
DO CASE
CASE cMODE = "E"
cTITLE := "Customer Maintenance EDIT"
CASE cMODE = "A"
cTITLE := "Customer Maintenance ADD"
CASE cMODE = "V"
cTITLE := "Customer Maintenance VIEW"
ENDCASE
oBtn1:Disable()
oBtn2:Disable()
DEFINE WINDOW oWndChild ;
MDICHILD ;
FROM 0,0 to 32,100 ;
OF oWnd ;
TITLE cTITLE
DEFINE DIALOG oCust RESOURCE "CUSTOMER" of oWndChild
REDEFINE FOLDEREX oFld ID 109 of oCust PROMPT "Billing Information", "Service Address";
DIALOGS "CUSTVIEW", "SERVVIEW"
Folder_1( cMode, oWnd, oRsCust, oFld ) // Custview folder
ACTIVATE DIALOG oCust NOWAIT ;
ON INIT ( oCust:Move( 0, 0 ));
VALID(!GETKEYSTATE( 27 ))
ACTIVATE WINDOW oWndChild ;
ON INIT oWndChild:SetSize( oCust:nWidth, oCust:nHeight, .T. );
VALID ( IIF( !lOK1, ExitPgm1(.T.,oWndChild,oRsCust,oBtn1,oBtn2), .F. ))
RETURN( NIL )
// ---------- FOLDER-PAGE 1
Static FUNC FOLDER_1( cMode, oWnd, oRsCust, oFld )
Local oLname,cLname
REDEFINE GET oLname var cLname ID 153 of oFld:aDialogs[1] ;
ON CHANGE ( _Isearch( oLname, cLname, oLbx, oRsCust )) UPDATE
REDEFINE xBROWSE oLBX ;
RECORDSET oRsCust ;
COLUMNS "CUSTOMEREID" ;
COLSIZES 50 ;
HEADERS "Cust Id" ;
ID 172 of oFld:aDialogs[1] ;
AUTOCOLS LINES
ADD oCol TO oLbx AT 1 DATA {|x| x := _ChkName(oRsCust:Fields("Last Name"):Value,;
oRsCust:Fields("First Name"):Value,;
oRsCust:Fields("Mid Init"):Value) };
HEADER "Last Name or Company" size 190
ADD oCol TO oLbx AT 2 DATA {|x| x := _ChkAdd(oRsCust:Fields("Address1"):Value,;
oRsCust:Fields("City"):Value,;
oRsCust:Fields("State"):Value) };
HEADER "Address" size 200
RETURN( nil )
//----------------------------
Static Func _ChkName( cLast,cFirst,cMiddle )
Local cName
cName := substr("Unk"+space(45),1,45)
If cMiddle = " "
If cFirst = " " .or. empty( cFirst )
cName = substr( alltrim( cLast)+space(45),1,45)
Else
cName := substr(alltrim(cLast)+", "+alltrim(cFirst)+space(45),1,45)
Endif
Else
If cFirst = " " .or. empty( cFirst )
cName = substr( alltrim( cLast)+space(45),1,45)
Else
cName := substr(alltrim(cLast)+", "+alltrim(cFirst)+" "+;
alltrim(cMiddle)+space(45),1,45)
Endif
Endif
Return( cName )
//-------------------------------
Static Func _ChkAdd( cAddress, cCity, cState )
LOCAL cName
cName := if( cAddress = " " .or. empty( cAddress ), " ", alltrim( cAddress ))+" "+;
if( cCity = " " .or. empty( cCity), " ", alltrim(cCity))+" "+;
if( cState = " " .or. empty( cState), " ", alltrim(cState))
cName := substr( alltrim( cName ) +space(45),1,45)
Return( cName )
//-------------
Static Func _Isearch( oLname, cLname, oBrw, oRsCust )
cLname := alltrim(cLname )
Msginfo( cLname) // rem this out and this function does not work
If len( cLname ) = 0
oRsCust:Filter := ""
oRsCust:Filter := "[Last Name] = 'bogus'"
oBrw:ReFresh()
oLname:SetFocus()
Return(.t.)
Endif
If len( cLname ) >= 3
Msginfo( cLname )
oRsCust:Filter := ""
oRsCust:Filter := "[Last Name] like '"+cLname+"%'"
If oRsCust:eof
MsgInfo( "No Find" )
oLname:SetFocus()
Return(.t.)
Endif
oBrw:ReFresh()
oLname:SetFocus()
SysReFresh()
Return(.t.)
Endif
oLname:SetFocus()
Return(.T.)
//-------------------------------
Static FUNCTION ExitPgm1( lCLEAN,oWndchild,oRsCust,oBtn1,oBtn2 )
LOCAL cDEFA, lOK3
cDEFA := SET(7)
IF lCLEAN = .T.
lOK1 := .T.
oRsCust:CLose()
oWndChild:End()
oBtn1:Enable()
oBtn2:Enable()
ENDIF
RETURN( lOK1 )
// -- end
- Code: Select all Expand view
CUSTOMER DIALOG 3, 13, 538, 356
STYLE WS_CHILD
FONT 8, "Arial"
{
CONTROL "", 109, "TFolderex", 0 | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 3, 4, 524, 308
}
CUSTVIEW DIALOG 12, 11, 513, 311
STYLE WS_CHILD
FONT 6, "MS Sans Serif"
{
GROUPBOX "", 196, 2, -2, 213, 304, BS_GROUPBOX
LTEXT "Company or Last Name", 112, 7, 39, 105, 10, SS_NOPREFIX | WS_GROUP
EDITTEXT 153, 7, 50, 105, 12, ES_AUTOHSCROLL | WS_BORDER | WS_TABSTOP
CONTROL "Customer Info", 172, "TXBrowse", 0 | WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_TABSTOP, 221, 1, 290, 215
}
SERVVIEW DIALOG 12, 11, 436, 311
STYLE WS_CHILD
FONT 6, "MS Sans Serif"
{
LTEXT "Company or Last Name", -1, 24, 58, 105, 10, SS_NOPREFIX | WS_GROUP
EDITTEXT 120, 24, 70, 105, 12, ES_AUTOHSCROLL | WS_BORDER | WS_TABSTOP
}