by Rick Lipkin » Wed Nov 21, 2007 12:20 am
Michel
Here is some specific code and listbox for MS Sql server .. note the connection string .. :
// public defined in Main.prg:
xPROVIDER := "SQLOLEDB"
xSOURCE := "MSSQL01"
xCATALOG := "PCAS"
xUSERID := "pcasuser"
xPASSWORD := "pcas"
// grpbrow.prg
//
#INCLUDE "FIVEWIN.CH"
STATIC oBROW,oGRP
STATIC lOK
//----------------------------
FUNCTION _GRPbrow( oWndMDI )
LOCAL SAYING, oRs,oErr, cSQL, cTITLE
lOK := .F.
IF xSUPER = 'Y' .or. xADMIN = 'Y'
ELSE
SAYING := "SORRY ... Supervisor or Admin Rights only"
MsgAlert( SAYING )
_CleanUp()
RETURN(.F.)
ENDIF
oRs := TOleAuto():New( "ADODB.Recordset" )
oRs:CursorType := 1 // opendkeyset
oRs:CursorLocation := 3 // local cache
oRs:LockType := 3 // lockoportunistic
IF xADMIN = 'Y'
cSQL := "SELECT * FROM GROUPS ORDER BY PROGID,CODE"
cTITLE := "Group Records Browse for ALL"
ELSE
cSQL := "SELECT * FROM GROUPS where progid = '"+xPROGID+"' ORDER BY CODE"
cTITLE := "Group Records Browse for Progid "+xPROGID
ENDIF
TRY
oRs:Open( cSQL,'Provider='+xPROVIDER+';Data Source='+xSOURCE+';Initial Catalog='+xCATALOG+';User Id='+xUSERID+';Password='+xPASSWORD )
CATCH oErr
MsgInfo( "Error in Opening GROUPS table" )
_CleanUp()
RETURN(.F.)
END TRY
IF oRs:EOF
oRs:Close()
SAYING := "Could not find any matching records for "+cSQL
MsgInfo( SAYING )
_CleanUp()
RETURN(.F.)
ENDIF
oRs:MoveFirst()
DEFINE WINDOW oGRP ;
FROM 2,2 to 30,70 ;
of oWndMDI ;
TITLE cTITLE ;
MENU BuildMenu( oRs ) ;
NOMINIMIZE ;
NOZOOM ;
MDICHILD
@ 0, 0 LISTBOX oBrow FIELDS ;
oRs:Fields("CODE"):Value, ;
oRs:Fields("DESC"):Value, ;
oRs:Fields("progid"):Value ;
SIZES 80,250,80 ;
HEADERS "Code", ;
"Description", ;
"ProgID" ;
of oGRP ;
ON DBLCLICK( _GrpView( "V", oRs )) ;
UPDATE
oBrow:bLogicLen := { || oRs:RecordCount }
oBrow:bGoTop := { || oRs:MoveFirst() }
oBrow:bGoBottom := { || oRs:MoveLast() }
oBrow:bSkip := { | nSkip | Skipper( oRs, nSkip ) }
oBrow:cAlias := "ARRAY"
oGrp:oClient := oBROW
oGrp:SetControl( oBrow )
ACTIVATE WINDOW oGRP ;
VALID ( IIF( !lOK, GrpCLose( .T. , oRs ), .F. ))
RETURN( NIL )
//-------------------------------
Static Func _Cleanup()
oBROW := NIL
oGRP := NIL
RETURN(.T.)
//------------------//
static FUNCTION BuildMenu( oRs )
LOCAL oMenu1
MENU oMenu1
MENUITEM "&Add ..." ;
ACTION ( _GRPVIEW( "A", oRs ), ;
oBROW:Refresh() ) ;
MESSAGE "Add a new record"
MENUITEM "&Edit ..." ;
ACTION ( _GRPVIEW( "E", oRs ), ;
oBROW:ReFresh() ) ;
MESSAGE "Edit this record"
MENUITEM "&Delete ..." ;
ACTION GRPDEL( oRs) ;
MESSAGE "Delete this record"
MENUITEM "&View ..." ;
ACTION _GRPVIEW( "V", oRs ) ;
MESSAGE "View this record"
MENUITEM "&Quit" ;
ACTION oGRP:END()
ENDMENU
RETURN( oMenu1 )
//------------------------
Static FUNCTION GRPCLOSE(lCLEAN, oRs )
IF lCLEAN = .T.
lOK := .T.
oBROW:cALIAS := nil
oRs:Close()
_CleanUp()
ENDIF
RETURN(lOK)
//--------------------
Static FUNCTION GRPDEL( oRs )
LOCAL SAYING
IF xSUPER = 'Y' .or. xADMIN = 'Y'
ELSE
SAYING := "Sorry ... Supervisor or Admin Rights Only"
MsgAlert( SAYING )
RETURN(NIL)
ENDIF
IF oRs:EOF
SAYING := "Sorry ... Before you can Delete a "
SAYING += "record, you have to Add one first"
MsgAlert( SAYING )
RETURN(NIL)
ENDIF
IF MsgYesNo( "Are you SURE you want to DELETE this?" )
oRs:Delete()
oRs:MoveNext()
IF oRs:eof .and. .not. oRs:bof
oRs:MoveFirst()
ENDIF
ENDIF
oBROW:Refresh(.T.)
SysReFresh()
RETURN(NIL)
//-------------------------------
STATIC FUNCTION SKIPPER( oRsx, nSkip )
LOCAL nRec := oRsx:AbsolutePosition
oRsx:Move( nSkip )
IF oRsx:EOF; oRsx:MoveLast(); ENDIF
IF oRsx:BOF; oRsx:MoveFirst(); ENDIF
RETURN( oRsx:AbsolutePosition - nRec )
// end GRPBROW.PRG