Jan 16, 2012

Replace string function

Sample : f_replace(powerbuilder,r,s) will return powesbuildes


f_replace(string as_text,string as_before,string as_after)  returns string

integer li_Posisi
string ls_return

li_Posisi = pos(as_text,as_before)
do while li_Posisi > 0
    ls_return += mid(as_text,1,li_Posisi - 1) + as_after
    as_text = mid(as_text, li_Posisi + Len(as_before))
    li_Posisi = pos(as_text,as_before)
loop
ls_return += as_text

return ls_text

cheers..

Export Datawindow to Excel

Export Datawindow to excel files without dw2xls
First, create a global function

f_excel(datawindow idw)
int li_rtn
string xlsname,nama, ls_alamat
//Check for row(s) in datawindow
if idw.rowcount( ) <= 0 then
        messagebox(' Export Excel','No rows to export!')
        return
end if
//excel file save directory
li_rtn   =   GetFileSaveName( "Select   File",   &  
            xlsname,   nama,   "",   &  
            "Excel 97-2003 Workbook(*.xls) ,*.*,")
if idw.saveas( xlsname+'.xls' , htmltable!, TRUE ) = -1 then
    MessageBox(" Warning", "error!",Exclamation! )
    return ;
end if ;
oleobject excel ;
excel = create oleobject ;
if excel.connecttoobject(xlsname+ '.xls' ) = 0 then
   Excel.Application. DisplayAlerts = false ;
   Excel.Application. Workbooks( 1 ).Parent.Windows
   Excel.Application. workbooks( 1 ).Name ).Visible true ;
   Excel.Application. Workbooks( 1 ).SaveAs(xlsname+ '.xls', 39 ) ;
   Excel.Application. Workbooks( 1 ).close() ;
   messagebox(' Export Excel','datawindow exported succesfully!')
        run("c:\windows\ explorer. exe "+xlsname+'. xls')
end if ;
destroy excel

Sample usage : f_excel(dw_customer)

Cheers..

Jan 15, 2012

Keycode to string

With most objects, you can trap the key strokes using user events.
For example, on a datawindow object,
you can create a user event using Event Id = pbm_dwnkey.
In this event, you'll find a parameter of type keycode.
This is a predefined enumerated data type with values such as KeyA!, KeyB!, etc.
But if you want to convert this value to a string,
you'll find your choices are limited.
It looks something like this :

choose case a_key
       case keyA!
            ls_string = "A"
       case keyB!
            ls_string = "B"
       case keyC!
           ls_string = "C"
...
end choose




As simple as that!
Cheers..

Sending Key Press

You can make a control likely pressing (a) key(s). 
First declare the Win32 API modul on Declare > Local External Functions
Subroutine keybd_event( int bVk, int bScan, int dwFlags, int dwExtraInfo) Library "user32.dll"

//Next is an example to simulate pressing "A" key
integer li_vkey
li_vkey = 65 // Character A
sle_1.setfocus() // the desired control to view
keybd_event( li_vkey, 1, 0, 0)


//Another example to simulate "Backspace" key
integer li_vkey
integer li_pos
li_pos = len(sle_1.Text) + 1
sle_1.selectText(li_pos, 0)// Cursor position on last text
li_vkey = asc ("~b") // backspace
keybd_event( li_vkey, 1, 0, 0)


It 100% works.
Cheers..

Import excel into datawindow

Tested in Microsoft Ofice Excel 2003

oleobject excel
long ll_hasil,ll_return,ll_count
boolean lb_cek
excel = create oleobject;
//connect to office application
ll_return = excel.connecttonewobject("excel.application")
if ll_return <> 0 then
messagebox('Peringatan','Tidak dapat mengeksekusi MS.Excel!',exclamation!)
destroy excel
return 0
end if
//open file excel (you can make this string as variable)
excel.workbooks.open( "c:\xls\contoh file excel.xls" )
excel.application.visible = false
excel.windowstate = 2
//cek rows in excel sheet with return value copy
lb_cek = excel.worksheets(3).activate
excel.worksheets(3).range("A2:Z5000").Copy //column range in excel files
ll_count = dw_1.importclipboard()
if ll_count <= 0 then
messagebox('Informasi','Data tidak ditemukan!')
end if
excel.worksheets(3).range("A10000:A10000").Copy
excel.application.quit
excel.disconnectobject()
destroy excel;

How it works?
This script open excel file, read cell and copy to clipboard. And then insert into datawindow.
You also import multiple sheet or multiple excel file.
Cheers..

Are legacy 4GL applications keeping you from embracing modern technologies?

As the competitive advantages offered by cloud, mobile, and other new technologies become more apparent, the decision to migrate legacy 4GL ...