FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Mr Rao, Do you have a sample using xBrowse with ADO?
Posts: 3358
Joined: Fri Oct 07, 2005 08:20 PM
Mr Rao, Do you have a sample using xBrowse with ADO?
Posted: Fri Feb 27, 2015 03:58 PM

Good Morning Mr. Rao:

Dou you have, or where can I find, a sample using xBrowse with FASTEDIT (ADD, DELETE, CHANGE), ADO
and MySql?.

Thanks and Regards

SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Mr Rao, Do you have a sample using xBrowse with ADO?
Posted: Sat Feb 28, 2015 08:43 AM
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 3358
Joined: Fri Oct 07, 2005 08:20 PM
Re: Mr Rao, Do you have a sample using xBrowse with ADO?
Posted: Sat Feb 28, 2015 05:58 PM

Antonio:

Muchas gracias, voy a "echarles un ojo", si alguien más tiene
otro ejemplo pues bienvenido.

Saludos

SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Mr Rao, Do you have a sample using xBrowse with ADO?
Posted: Sun Mar 01, 2015 10:29 PM
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.
Code (fw): Select all Collapse
#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.
Regards



G. N. Rao.

Hyderabad, India
Posts: 3358
Joined: Fri Oct 07, 2005 08:20 PM
Re: Mr Rao, Do you have a sample using xBrowse with ADO?
Posted: Mon Mar 02, 2015 02:37 AM

Mr. Rao:

Thanks a lot for your sample, It's very clear, I'l try it, although I was thinking in FASTEDIT
xBrowse sample, however this example can help me.

My tools are, FWH1312, BCC582, Harbour/xHarbour and MySql 5.1

Best regards

SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Mr Rao, Do you have a sample using xBrowse with ADO?
Posted: Mon Mar 02, 2015 03:12 AM
Armando wrote:Mr. Rao:

Thanks a lot for your sample, It's very clear, I'l try it, although I was thinking in FASTEDIT
xBrowse sample, however this example can help me.

My tools are, FWH1312, BCC582, Harbour/xHarbour and MySql 5.1

Best regards

For 13.12, please comment out :SetChecks()
FASTEDIT is also included in the above sample. FastEdit works the same way as for DBF.
I am not sure if any changes are required for mysql for 13.12. I shall try to test with this version soon and get back to you.
Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Mr Rao, Do you have a sample using xBrowse with ADO?
Posted: Mon Mar 02, 2015 03:58 AM
I modified the program for 13.12.
I suggest you give your full working connection string in the static variable at the beginning of the program.
Code (fw): Select all Collapse
#include "fivewin.ch"
#include "adodef.ch"

REQUEST DBFCDX

static cDbf       := "c:\\fwh\\samples\\customer.dbf"  // substitute your dbf na,e

// Replace this with your connection string
static cConnStr   := "Driver={MySQL ODBC 5.3 ANSI Driver};Server=localhost;Database=FWH;User=root;Password=secret;Option=3;"
static cTable     := "CUSTOMER"                 // Substitute your table name

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

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
      AEval( :aCols, { |o| If( o:cDataType == 'L', o:SetCheck(), nil ) } )
      :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, cStr
   local nDataSource := Alert( "Select DataSource", { "MYSQL", "DBF" } )

   if nDataSource == 1 // MYSQL
      oCn   := FW_OpenadoConnection( cConnStr, .t. )
      if oCn != nil
         uTable    := FW_OpenRecordSet( oCn, cTable )
      endif
      if uTable == nil
         ? "Table Open Error"
         QUIT
      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

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

After you test this sample, you may ask how to handle more complex situations and we shall be glad to support you.
Regards



G. N. Rao.

Hyderabad, India
Posts: 3358
Joined: Fri Oct 07, 2005 08:20 PM
Re: Mr Rao, Do you have a sample using xBrowse with ADO?
Posted: Mon Mar 02, 2015 04:21 AM
Mr Rao.

nageswaragunupudi wrote:I modified the program for 13.12.
I suggest you give your full working connection string in the static variable at the beginning of the program.

After you test this sample, you may ask how to handle more complex situations and we shall be glad to support you.


I will, thanks for your excelent support.

Regards
SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero

Continue the discussion