I do not know which version of FWH you are using.
If you know how to code browse for DBF, you already know how to code for ADO. The code written for DBF works exactly for ADO table with the same structure. Only difference is that you provide RecordSet object as DATASOURCE instead of Alias Name.
The sample provided here works for both DBF and ADO and is a complete table maintenance code, including inline-edit, Add/Edit dialogs and Deletion of Records. For testing please substitute your names in the static variables section. If you are not using a very old version of FWH all these features should work for you.
#include "fivewin.ch"
#include "adodef.ch"
REQUEST DBFCDX
static cDbf := "c:\\fwh\\samples\\customer.dbf" //
// Substitute your
static cServer := "localhost" // values
static cDatabase := "FWH" //
static cUser := "root" //
static cPassword := "secret" //
static cTable := "CUSTOMER" //
//----------------------------------------------------------------------------//
function Main()
local uTable
SET DATE ITALIAN
SET CENTURY ON
SET DELETED ON
FWNumFormat( "E", .t. )
uTable := OpenTable()
BrowseTable( uTable )
CloseTable( uTable )
return nil
//----------------------------------------------------------------------------//
// This XBrowse Code is exactly the same for DBF, ADO, etc
// Works the same way for browse with all features and also
// Add/Edit Dialogs irrespective of the datasource ( DBF/ADO/etc )
//----------------------------------------------------------------------------//
static function BrowseTable( uTable )
local oWnd, oFont, oBrw
DEFINE FONT oFont NAME "TAHOMA" SIZE 0,-14
DEFINE WINDOW oWnd
oWnd:SetFont( oFont )
DEFINE BUTTONBAR oWnd:oBar SIZE 80,32 2007
DEFINE BUTTON OF oWnd:oBar PROMPT "Add" ACTION oBrw:EditSource( .t. )
DEFINE BUTTON OF oWnd:oBar PROMPT "Edit" ACTION oBrw:EditSource()
DEFINE BUTTON OF oWnd:oBar PROMPT "Delete" ACTION oBrw:Delete()
DEFINE BUTTON OF oWnd:oBar PROMPT "Report" ACTION oBrw:Report() GROUP
DEFINE BUTTON OF oWnd:oBar PROMPT "Excel" ACTION oBrw:ToExcel()
@ 0,0 XBROWSE oBrw OF oWnd DATASOURCE uTable AUTOCOLS ;
CELL LINES FOOTERS FASTEDIT AUTOSORT
WITH OBJECT oBrw
:SetChecks()
:nEditTypes := EDIT_GET
:CreateFromCode()
END
oWnd:oClient := oBrw
ACTIVATE WINDOW oWnd MAXIMIZED
RELEASE FONT oFont
return nil
//----------------------------------------------------------------------------//
// The above code is all that is required for a full table maintenance
//----------------------------------------------------------------------------//
static function OpenTable()
local oCn, uTable
local nDataSource := Alert( "Select DataSource", { "MYSQL", "DBF" } )
if nDataSource == 1 // MYSQL
ADOCONNECT oCn TO MYSQL SERVER ( cServer ) DATABASE ( cDatabase ) USER ( cUser ) PASSWORD ( cPassword )
if oCn != nil
uTable := FW_OpenRecordSet( oCn, cTable )
if uTable == nil
oCn:Close()
? "Table Open Error"
QUIT
endif
endif
else
USE ( cDbf ) SHARED NEW VIA "DBFCDX"
if USED()
uTable := Alias()
else
? "Table Open Error"
QUIT
endif
endif
return uTable
//----------------------------------------------------------------------------//
static function CloseTable( uTable )
if ValType( uTable ) == 'C'
CLOSE ( uTable )
else
uTable:Close()
uTable:ActiveConnection:Close()
endif
return nil
//----------------------------------------------------------------------------//
The operative portion of the browse, include add/edit dialogs is only 30 lines including blank lines but handles table management completely.
In the above sample, I used FWH functions to connect to server and open recordset. While we advise using these functions for very good reasons, it is not necessary and you can adopt your own preferred code to open connection and record-set object.
For the illustration, I used AUTOCOLS. You can specify the columns just like DBF columns, eg.
COLUMNS "First", "City", "State", "Age", "Salary", etc.
You can also use expressions in the place of column names the same way for DBF and ADO
Eg: COLUMNS "FIRST-', '+LAST", "CITY", etc.
XBrowse translates the expression "FIRST-', '+LAST" as
- ( oBrw:cAlias )->( FIRST - ', ' + LAST ) if we are browing DBF and
- oBrw:oRs:Fields( "FIRST" ):Value - ', ' + oBrw:oRs:Fields( "LAST" ):Value if we are browsing ADO
All this complex work is automatically handled by XBrowse.
As long as you follow our oft repeated advise to use command syntax to create xbrowse and not to use Alias or Recset object directly in your browse code, the same code works for both DBF and ADO.
In case you have a very old version which does not support the add/edit/delete features, please let us know you version and we would be glad to make appropriate suggestions. Even otherwise you may like to know more and please feel free to ask here.
Screenshot of Browse with Edit Dialog:
With addition of oneline code you can direct the add/edit functionality to use your own custom made dialogs.