POWER BUIDER VÀ CHƯƠNG TRÌNH TỔNG VỤ
(biên soạn Đào Công Thới)
PHẦN I: CÁC CÚ PHÁP POWERBUILDER
A )Cac kiểu khai báo biến thông dụng
Menu im_id //biến kiểu menu
menu im_popm //kiểu menu
int ii_nowrow //kiểu số nguyên lớn hơn kiểu interger(loại bỏ phần thập phân)
vd: - Int(3.2)
Int(3.8) trả lại giá trị 3.0
- Int(-3.2)
Int(-3.8) trả lại giá trị là -4
datawindow idw_focus //kiểu dayawindow
boolean ib_ins, ib_upd //kiểu đúng sai
integer lds_ds //kiểu số nguyên
ulong AW_HOR_POSITIVE =1
pipelinez guo_pipelinez //kiểu dữ liệu được truyền dẫn
string g_transport //kiểu chuổi
long ll_newrow //kiểu số dài
A.1) khai báo biến kiểu currsor(kiểu con trỏ)
-cú pháp:
· DECLARE cursor_name CURSOR FOR . . .
· OPEN cursor_name
· FETCH cursor_name INTO . . .
· CLOSE cursor_name
-ví dụ:(khi một giá trị biến mang dữ liệu gồm nhiều dòng thì ta dùng cursor)
DECLARE emp_curs CURSOR FOR //khai báo biến cursor
SELECT emp_name FROM EMPLOYEE
WHERE emp_state = :sle_1.text;
string emp_name_var
OPEN emp_curs;
FETCH emp_curs INTO :emp_name_var;
// vòng lặp đến khi nào không còn dữ liệu mới ngừng
DO WHILE SQLCA.sqlcode = 0
MessageBox("Found an employee!",emp_name_var)
FETCH emp_curs INTO :emp_name_var;
LOOP
CLOSE emp_curs;
B) cách thiết lập menu(thong qua ví dụ từ chương trình tổng vụ)
-khai báo hai dang menu
+dạng menu trên thanh tasbar menu im_id
+dang menu popup menu im_popm
-thiết lập form sử dụng menu nào(thông qua biến)
Trong sự kiện open trên form
im_id = this.menuid //sử dụng chính menu trên form này
im_popm = create m_sheet //sử dụng menu m_sheet(menu này dạng popup)
-đây là cách kiểm tra xem popup khác menu tasbar chổ nào
Ví dụ: 1-
//biến chỉ đến menu tasbar là im_id
//item trước là vị trí menu cha,item sau là vị trí menu con
im_id.item[1].item[1].Enabled = TRUE // save
im_id.item[1].item[2].Enabled = FALSE // print
im_id.item[1].item[5].Enabled = FALSE // query
im_id.item[4].item[1].Enabled = FALSE // new
im_id.item[4].item[2].Enabled = FALSE // modify
im_id.item[4].item[3].Enabled = FALSE // delete
im_id.item[4].item[4].Enabled = TRUE // cancel
// biến chỉ dến menu popup là im_popm
im_popm.item[4].item[1].Enabled = FALSE // new
im_popm.item[4].item[2].Enabled = FALSE // modify
im_popm.item[4].item[3].Enabled = FALSE // delete
im_popm.item[4].item[5].Enabled = FALSE // cancel
im_popm.item[4].item[7].Enabled = FALSE // save
-ví dụ 2
im_id.item[1].item[1].Enabled = FALSE // save
im_id.item[1].item[2].Enabled = FALSE // print
im_id.item[1].item[5].Enabled = FALSE // query
im_id.item[4].item[1].Enabled = FALSE // new
im_id.item[4].item[2].Enabled = FALSE // modify
im_id.item[4].item[3].Enabled = FALSE // delete
im_id.item[4].item[4].Enabled = FALSE // cancel
im_popm.item[4].item[1].Enabled = FALSE // new
im_popm.item[4].item[2].Enabled = FALSE // modify
im_popm.item[4].item[3].Enabled = FALSE // delete
im_popm.item[4].item[5].Enabled = TRUE // cancel
im_popm.item[4].item[7].Enabled = TRUE // save
sau hai ví dụ ta thấy menu frame sữ dụng cho thanh tasbar ma biến đại diện menu là im_id.
Còn menu m_sheet sử dụng cho menu popup biến đại diện là im_popm
Và đây là 2 hình của 2 menu này
Sau khi xem ví dụ cụ thể trên tôi tin rằng bạn dư sức thiết lập menu cho nó hoạt động như thế nào rồi đó
C) các lệnh hoạt động của menu tieu biểu
I) Lệnh tạo một dòng dữ liệu mới (ue_newrow, ue_newrow_d)
ll_newrow = This.InsertRow(0) cho biến này inser một giá trị rỗng(lệnh inser)
(this) mang giá trị của form hay datawindow đang thực thi gọi nó
This.ScrollToRow( ll_newrow ) và giá trị rỗng ấy được đặt ở hàng cuối cùng
Gia tri (this) bây gio đại diện cho dw
ii_nowrow = dw_1.GetRow(): đọc vô hàng hiện tại rồi gán biến ii_nowrow bằng giá trị hàng dữ liệu
dw_1.Setfocus() : uu tien cho dw_1(được đặt con trỏ hoặt rê chuột tác động)
II) lệnh lưu lại một dòng vừa inser vào
áp dụng ví dụ từ chương trình
If ii_status=11 or ii_status=12 then //neu chương trình kích hoạt inser or modify thì
If dw_1.AcceptText() = 1 then //kiểm tra xem có dòng dữ liệu chưa
dw_1.TriggerEvent("ue_save") // sau đó gọi đến sự kiện ue_save để thực hiện lệnh save
ue_save
long ll_del,ll_mod,ll_total //khai báo biến
ll_del = this.DeletedCount() //biến nhận giá trị số dòng bị xóa
ll_mod = this.ModifiedCount() //biến nhận giá trị số dòng bị sữa
ll_total = ll_del + ll_mod
//nếu tổng số dòng thêm mới và số dòng được sữa lớn hơn 0 và lựa chọn update thì lệnh save kích hoạt
IF ll_total > 0 AND this.Update(TRUE,FALSE) = 1 THEN
commit Using SQLCA;
this.ResetUpdate() //lệnh này lưu dòng dữ liệu lại vào cơ sở dữ liệu
this.Object.Datawindow.ReadOnly = "YES"
ib_save = TRUE //truyền ra ngoài biến ib_save=true
ELSE
rollback Using SQLCA; //nếu lệnh save không thành công thì trả ra ib_save= faise
ib_save = FALSE
END IF
kết thúc ue_save
IF dw_1.ib_save = true THEN
this.TriggerEvent ( 'ue_lostctrl_m' )//sau khi lưu lại lập tức set lại menu
dw_1.Object.DataWindow.ReadOnly = true
ii_status = 0 //trỡ về ban đầu
ELSEIF trim(dw_1.GetItemString(dw_1.GetRow(),1)) <> "" THEN
IF dw_1.DeletedCount() + dw_1.ModifiedCount() = 0 THEN
this.TriggerEvent ( 'ue_lostctrl_m' )//nếu save không thành công lập tức set lại menu
dw_1.Object.DataWindow.ReadOnly = true
ii_status = 0 //trỡ lại ban đầu
dw_1.ib_save = TRUE
END IF
END IF
End if
End if
//sau khi xem ví dụ này chốt lại lệnh save dữ liệu là this.ResetUpdate()
III) lệnh sữa một dòng dữ liệu(ue_modify, ue_modify_d)
if dw_1.GetRow() > 0 then // nếu xác định có dòng dữ liệu
if Not IsNull(dw_1.GetItemString( dw_1.GetRow(), 1 )) then
this.TriggerEvent ( 'ue_control_m' )//menu được set lại
dw_1.Object.DataWindow.ReadOnly="No"
ii_status = 12
ii_nowrow = dw_1.GetRow() //dòng hiện tại được phép sữa
else
Messagebox ( "訊訊", "訊訊訊訊訊訊 !!" )// nếu không có dữ liệu để sữa thì thông báo
end if
else
Messagebox ( "訊訊", "訊訊訊訊訊訊 !!" ) // nếu không có dữ liệu để sữa thì thông báo
end if
//sau đó chỉ cần gọi sự kiện save thì dữ liệu được sữa se lưu lại vào cơ sở dữ liệu
-Ví dụ:
if idw_focus.GetRow () > 0 then //nếu hàng hiện thời >0 thì
this.TriggerEvent ( 'ue_control_d' )
idw_focus.Object.DataWindow.ReadOnly="No"
ii_status = 2
idw_focus.Triggerevent('ue_modify_d')
ii_nowrow = idw_focus.GetRow()//biến bằng giá trị hiện thời(modify)
else
Messagebox ( "訊訊", "訊訊訊訊訊訊訊訊 !!" )
end if
IV) lệnh xóa một dòng dữ liệu(ue_deleterow, ue_deleterow_d)
if dw_1.GetRow() > 0 then
if Not IsNull(dw_1.GetItemString( dw_1.GetRow(), 1 )) then
if Messagebox ( "訊訊", "訊訊訊訊訊訊訊訊訊 ??", Question!, OkCancel! ) = 2 then
Return //hỏi xem có xóa dữ liệu không
end if
ii_status = 13
dw_1.Triggerevent( 'ue_deleterow' ) //gọi đến sự kiện xóa dòng
dw_1.TriggerEvent ( 'ue_save' ) //sau đó lưu lại
else
Messagebox ( "訊訊", "訊訊訊訊訊訊 !!" ) //nếu không có dữ liệu thì thông báo
end if
else
Messagebox ( "訊訊", "訊訊訊訊訊訊 !!" )
end if
ue_daleterow
long ll_currentrow
ll_currentrow = This.GetRow( )
This.deleterow(ll_currentrow) //xóa dòng dữ liệu
ket thúc
V) lệnh cancel lệnh vừa kich hoạt
ví dụ 1 (ue_cancel) lệnh cancel khi sử dụng menu tasbar
if ii_status = 11 then //nếu mà thêm mới thì
dw_1.DeleteRow(dw_1.RowCount()) //xóa dòng hiện thời vừa thêm mới
IF dw_1.RowCount() < 1 THEN //kiểm tra xem dòng hiện tại nếu chưa có thì
dw_1.InsertRow(0) // thể hiện khung datawindow
END IF
Else // nếu mà sữa dữ liệu thì
dw_1.object.data.current[dw_1.getrow()] = dw_1.object.data.original[dw_1.getrow()]
dw_1.setitemstatus(dw_1.getrow(),0,primary!,NotModified!)
//thiết lập tình trạng không thay đổi dữ liệu ở dw_1
end if
ii_status = 0 //trả về như lúc ban đầu
dw_1.ScrollToRow(dw_1.RowCount()) // cuộn tới hàng kế tiếp
dw_1.Object.DataWindow.ReadOnly="Yes"
dw_1.TriggerEvent("ue_cancel") //goi đến ue_cancel
this.TriggerEvent ( 'ue_lostctrl_m' ) //set lai memu
ue_cancel
IF ii_status = 11 THEN
this.DeleteRow(this.RowCount())
IF this.RowCount() < 1 THEN
this.InsertRow(0)
END IF
ELSEIF ii_status = 12 THEN //sữa lại một dòng dữ liệu
this.Object.Data.Current[this.GetRow()] = this.Object.Data.Original[this.GetRow()]
this.SetItemStatus(this.GetRow(),0,Primary!,NotModified!)
//set cho tình trạng không thay đổi dữ liệu
END IF
this.Modify("Datawindow.ReadOnly = YES")
ii_status = 0 //trỡ lại tình trạng chưa có sự kiện nào kích hoạt
kết thúc ue_cancel
Ví dụ 2(ue_cancel_d) lệnh cancel khi sử dụng menu popup
if Mod(ii_status, 10) = 1 then //biến ii_status chia cho 10 mà phần dư=1 thì
ii_status = 0 //trở lại tình trạng nguyên thủy
idw_focus.RowsDiscard ( ii_nowrow, idw_focus.RowCount(), Primary! )
else //nếu sữa dữ liệu
idw_focus.object.data.current[idw_focus.getrow()]
=idw_focus.object.data.original[idw_focus.getrow()]
idw_focus.setitemstatus(idw_focus.getrow(),0,primary!,NotModified!)
end if
ii_status = 0
idw_focus.Object.DataWindow.ReadOnly="Yes"
this.TriggerEvent ( 'ue_lostctrl_d' ) //set lai menu
(vậy sau khi xét ví dụ trên ta thấy lệnh cancel là lệnh quay lại ban đầu và không thực hiện lệnh
Thay đổi dữ liệu)
VI ) các lệnh di chuyển trên dữ liệu( tới,lui,đầu,cuối)
1 ) ue_nexrow(tới dòng kế tiếp)
integer li_count,li_currow
li_count = This.RowCount()
li_currow = This.getrow()
if li_currow = li_count then
IF IsValid(m_frame) THEN
m_frame.m_edit.m_lastrow.Enabled = FALSE
m_frame.m_edit.m_nextrow.Enabled = FALSE
m_frame.m_edit.m_firstrow.Enabled = TRUE
m_frame.m_edit.m_priorrow.Enabled = TRUE
END IF
else
This.ScrollNextRow( ) // chạy đến dòng kế tiếp
IF IsValid(m_frame) THEN
m_frame.m_edit.m_firstrow.Enabled = TRUE
m_frame.m_edit.m_priorrow.Enabled = TRUE
END IF
end if
setfocus(This)
2) ue_lastrow(trở về dòng trước)
integer li_rowcount
li_rowcount = This.RowCount()
This.ScrollToRow(li_rowcount) //trở về hàng trước
setfocus(This)
IF IsValid(m_frame) THEN
m_frame.m_edit.m_lastrow.Enabled = FALSE
m_frame.m_edit.m_nextrow.Enabled = FALSE
m_frame.m_edit.m_firstrow.Enabled = TRUE
m_frame.m_edit.m_priorrow.Enabled = TRUE
END IF
3 ) ue_firstrow(về dòng đầu tiên)
This.ScrollToRow(0) //về dòng đầu tiên
setfocus(This)
IF IsValid(m_frame) THEN
m_frame.m_edit.m_firstrow.Enabled = FALSE
m_frame.m_edit.m_priorrow.Enabled = FALSE
m_frame.m_edit.m_lastrow.Enabled = TRUE
m_frame.m_edit.m_nextrow.Enabled = TRUE
END IF
4) ue_priorrow (về dòng cuối cùng)
integer li_count,li_currow
li_currow = This.getrow()
if li_currow = 1 then
IF IsValid(m_frame) THEN
m_frame.m_edit.m_firstrow.Enabled = FALSE
m_frame.m_edit.m_priorrow.Enabled = FALSE
m_frame.m_edit.m_lastrow.Enabled = TRUE
m_frame.m_edit.m_nextrow.Enabled = TRUE
END IF
else
This.ScrollPriorRow() //về dòng cuối cùng
IF IsValid(m_frame) THEN
m_frame.m_edit.m_lastrow.Enabled = TRUE
m_frame.m_edit.m_nextrow.Enabled = TRUE
END IF
end if
setfocus(This)
VII ) các lệnh di chuyển trên trang báo biểu
1 ) ue_nextpage
This.scrollnextpage() //lệnh di chuyển đến trang kế tiếp
2) ue_priorpage
This.scrollpriorpage() //di chuyển đến trang cuối cùng
D ) các giá trị biến được gán với sự kiện
ii_status = 11: sự kiện ue_newrow(thêm mới một dòng dữ liệu)
ii_status = 0: chưa có sự kiện nào kích hoạt
ii_status = 12: sự kiện ue_modify(sữa chữa lại một dòng cũ)
ii_status = 22: sự kiện ue_modify_d (sữa chữa trên dòng dữ liệu)
ii_status = 13: sự kiện ue_deleterow
ii_status = 23: sự kiện ue_deleterow_d(xóa trên dòng dữ liệu)
ii_startus=1 : sự kiện ue_newrow
ii_status=21 : sự kiện thêm mới
* dw_1.SetTransObject ( SQLCA ) //cho phép thể hiện dw_1
* dw_1.Object.DataWindow.ReadOnly="No" //datawindow này được phép thêm xóa hoặc sữa
* dw_1.Object.DataWindow.ReadOnly="yes” // datawindow này chỉ đọc không làm gì hết
Tương tự:
* dw_1.Object.DataWindow.ReadOnly=false //.datawindow này được phép thêm xóa hoặc sữa
* dw_1.Object.DataWindow.ReadOnly= true //datawindow này chỉ đọc không làm gì hết
E) truyền giá trị từ cột,giá trị đệm vào biến
1) getitem
-diễn giải
Truyền dữ liệu từ cột và hàng xác dịnh vào biến,hoac giá trị lưu trữ ở bộ đệm vào biến
-cú pháp
returnvalue dwcontrol.GetItem (number row, number column )
returnvalue dwcontrol.GetItem (number row, string column )
-ví dụ
var LName = dw_employee.GetItem(3, "emp_name");
đặt giá trị cột emp_name hàng số 3 vào biến lname có kiểu var
2)getitemdate
- diễn giải:
Lấy giá trị có sẵn ở cột và hàng xác định,hoặc từ bộ đệm(kiểu ngày đưa vào biến)
-cú pháp
date dwcontrol.GetItemDate ( long row, string column {, DWBuffer dwbuffer , boolean
originalvalue } )
date dwcontrol.GetItemDate ( long row, integer column {, DWBuffer dwbuffer, boolean
originalvalue } )
-ví dụ
Date hiredate
hiredate = dw_employee.GetItemDate(3, "first_day")
//lấy giá trị kiểu ngày từ cột first_date hàng số 3 vào biến hiredate
3)getitemnumber
-diễn giải
Lấy giá trị kiểu số từ cột và hàng xác định,hoặt từ bộ đệm đưa vào biến
-Cú pháp
numeric dwcontrol.GetItemNumber( long row, string column{, DWBuffer dwbuffer, boolean originalvalue } )
numeric dwcontrol.GetItemNumber( long row, integer column{,DWBuffer dwbuffer, boolean
originalvalue } )
-ví dụ:
integer EmpNbr
EmpNbr = dw_employee.GetItemNumber(4, "emp_nbr")
// lấy giá trị kiểu số từ hàng 4 cột emp_npr đưa vào biến EmpNprte
4)Getitemstring
-Ví dụ thực tế
ls_kind = this.getitemstring(row,’as_kind’):
//truyền dữ liệu as_kind vào biến ls_kind
ls_pnl_no = dw_1.getitemstring(dw_1.getrow(),’pnl_no’):
// truyền dữ liệu từ cột pnl_no vào biến ls_pnl_no
cú pháp:
string dwcontrol.GetItemString (number row, number column {, string dwbuffer, boolean originalvalue } )
ví dụ:
String LName
LName = dw_employee.GetItemString(3, "emp_name")
//Biến LName nhận giá trị hàng 3 trong cột có tên emp_name trong DataWindow dw_employee
5)getitemtime
-Ví dụ:
Time Start
Start = dw_employee.GetItemTime(3, "title")
-diễn giải
Dữ liệu hàng 3 cột title được chuyển vào biến start dưới dạng giờ
F )truyền giá trị từ biến vào cột
1) setitem
-diễn giải
Truyền một giá trị biến vào cột và hàng xác định
-cú pháp
integer dwcontrol.SetItem ( long row, integer column, any value )
-ví dụ
dw_order.SetItem(3, "hire_date", 2003-06-07)
-giải thích ví dụ
Gán giá tri là ‘2003-06-07 ‘vào cột hire_date dòng 3 của dw_oder
2) setitemdate
- diễn giải:
Truyền một giá trị ngày tháng vào cột và hàng xác định
-cú pháp:
number dwcontrol.SetItemDate ( number row, string column, Date value )
number dwcontrol.SetItemDate ( number row, number column, Date value )
3)setitemdatetime
-cú pháp:
number dwcontrol.SetItemDateTime ( number row, string column, Date value )
number dwcontrol.SetItemDateTime ( number row, number column, Date value )
4)setitemnumber
-diễn giải:
truyền giá trị số vào hàng cột xác định
-cú pháp:
short dwcontrol.SetItemNumber ( long row, string column, double value )
short dwcontrol.SetItemNumberByColNum ( long row, short column,double value )
5)setitemstatus(thiết lập tình trạng cho ô dữ liệu)
-diễn giải:
Ô dữ liệu sẽ được thiết lập một tình trạng,ví như không được sữa
-cú pháp:
integer dwcontrol.SetItemStatus ( long row, integer column, dwbuffer dwbuffer, dwitemstatus status )
integer dwcontrol.SetItemStatus ( long row, string column, dwbuffer dwbuffer, dwitemstatus status )
+giải thích riêng về dwbutfer
.Primary! Số 0 mặc định giá tri khi lấy ra không xóa hay lọc ra
.delete! số 1 xóa trong datawindow nhưng không xóa trong csdl
. Filter! Số 2 dữ liệu này đã được lọc ra
+giải thích rieng về dwitemstatus
. NotModified! Số 0 không được thay đổi
. DataModified! Số 1 được thay đổi
.new số 2 thêm một hàng mới
. NewModified! Số 3 hàng được tạo mới hay sữa mới
-ví dụ
+ dw_history.SetItemStatus(5, "Salary", &Primary!, NotModified!)
thiết lập cột tiền lương mang giá trị nguyên thủy không thể sữa được
+ dw_new_hire.SetItemStatus(5, "emp_status", & Primary!, DataModified!)
Thiet lập cột dòng 5 cột em_status nguyen thủy là sữa dữ liệu
+ dwItemStatus l_status
l_status = dw_rpt.GetItemStatus(5, 0, Primary!)
IF l_status = NewModified! THEN
dw_rpt.SetItemStatus(5, 0, Primary!, DataModified!)
END IF
// Nếu tình trang hiện thời của hàng 5 này thêm mới thì cho phép sữa giá trị mới thêm mới đó
6)setitemstring
-cú pháp:
short dwcontrol.SetItemString ( long row, string column, string value )
short dwcontrol.SetItemStringByColNum ( long row, short column, string value )
-diễn giải:
Truyền giá trị chuổi ký tự vào dòng và cột xác định
-ví dụ:
this.setitem(this.getrow(),’matm_mat_no’,bien):
lấy dữ liệu của biến vào cột(mat_no của bảng matm)
-lấy dữ liệu từ file đuôi ini
*cú pháp:
ProfileString ( filename, section, key, default )
Ví dụ thực tế:
ls_password = ProfileString (is_ini_file, "database", "LogPassword", "")
ví dụ:
ProfileString("C:\PROFILE.INI", "Employee", "Name", "None")
-chuyển dữ liệu vào file đuôi ini
*cú pháp:
SetProfileString ( filename, section, key, value )
Ví dụ thực tế:
setprofilestring(is_ini_file,'database','logid',sle_1.text)
setprofilestring(is_ini_file,'database','logpassword',ls_password)
setprofilestring(is_ini_file,'database','servername',sle_3.text)
ví dụ :
SetProfileString("C:\PROFILE.INI", "Position", "Title", "MGR")
- is_ini_file = message.stringparm //biến nhận giá trị kiểu chuổi
7) GetFileOpenName
Cú pháp:
GetFileOpenName ( title, pathname, filename {, extension {, filter { , initdir { , aFlag } } } } )
GetFileOpenName ( title, pathname, filename[ ] {, extension {, filter { , initdir { , aFlag } } } } )
Chức năng:
thể hiện lên hộp thoại cho phép ta chọn file
ví dụ:
ll_value = GetFileOpenName("Select File To Upload Data", is_filename, ls_file, "JPG", "JPG Files
(*.jpg),*.jpg," +"BMP Files (*.bmp), *.bmp," + & "GIF Files (*.gif), *.gif,"+ "XML Files (*.xml),*.xml,"
+"PDF Files (*.pdf), *.pdf," + & "TIF Files (*.tif), *.tif," + "*.*, *.*")
ll_value =1 có nghĩa là đã chọn file
ll_value =0 không chọn hoặc cancel
G) CÁC HÀM TRONG POWERBUILDER
1)hàm mid(cắt kí tự xác định)
cú pháp:
Mid ( string, start {, length } )
ví dụ:
trả lại giá trị RUTH:
Mid("BABE RUTH", 5, 5)
trả lại giá trị "":
Mid("BABE RUTH", 40, 5)
trả lại giá trị BE RUTH:
Mid("BABE RUTH", 3)
2)hàm trim(cắt bõ khoảng trắng)
Cú pháp:
Trim ( string )
Ví dụ:
Trả lại giá trị BABE RUTH:
Trim(" BABE RUTH ")
string emp_fname
emp_fname = Trim(sle_emp_fname.Text)
3)hàm asc(lấy số trong mã ansi)
Cú pháp:
Asc ( string )
Ví dụ:
//Trả lại giá trị chữ A là 65 A:
Asc("A")
//Kiểm tra xem biến ls_name có phải là chuổi không
String ls_name
IF Asc(ls_name) > 64 and Asc(ls_name) < 91 THEN
4) hàm Mod (chia lấy phần dư)
Cú pháp:
Mod ( x, y )
Ví dụ:
Trả lại giá trị 2:
Mod(20, 6)
Trả lại giá trị 1.5:
Mod(25.5, 4)
Trả lại giá trị 2.5:
Mod(25, 4.5)
5)hàm round(làm tròn số thập phân được xác định)
Cú pháp:
Round ( x, n )
Ví dụ:
//trả lại giá trị 9.62:
Round(9.624, 2)
// trả lại giá trị 9.63:
Round(9.625, 2)
// trả lại giá trị 9.600:
Round(9.6, 3)
// trả lại giá trị -9.63:
Round(-9.625, 2)
//trả lại giá trị 0:
Round(-9.625, -1)
6) hàm truncate (cắt lấy số thập phân tới số xác định)
Cú pháp: Truncate ( x, n )
Ví dụ:
Trả lại giá trị 9.2:
Truncate(9.22, 1)
Trả lại giá trị 9.2:
Truncate(9.28, 1)
Trả lại giá trị 9:
Truncate(9.9, 0)
Trả lại giá trị -9.2:
Truncate(-9.29, 1)
7)hàm ceiling (cho số nguyên nhỏ nhất lớn hơn hoặc bằng n )
Cú pháp: Ceiling ( n )
Ví dụ: đặt giá trị biến num tới số 5:
decimal dec, num
dec = 4.8
num = Ceiling(dec)
đặt giá trị biến num là số -4
decimal num
num = Ceiling(-4.2)
num = Ceiling(-4.8)
8)hàm sqrt (Cho căn bậc 2 của n, n>=0)
Cú pháp: Sqrt ( n )
Ví dụ:
Giá trị trả về 1.414213562373095:
Sqrt(2)
Bị lỗi khi thực hiện lệnh này
Sqrt(-2)
9) hàm abs (cho giá trị tuyệt đối)
Cú pháp: Abs ( n )
Ví dụ:
Trả lại giá trị 4.2:
Abs(-4.2)
10) hàm lower (chuyển chuổi kí tự trỡ thành chữ thường) và hàm upper(ngược lại)
Cú pháp:
Lower ( string )
Ví dụ:
Trả lại chuỗi là babe ruth:
Lower("Babe Ruth")
11) hàm lefttrim (loại bỏ những giá trị rỗng đứng trước chuổi)
Cú pháp:
LeftTrim ( string )
Ví dụ:
Giá trị trả về là RUTH:
LeftTrim(" RUTH")
12) hàm righttrim (loại bỏ những giá trị rỗng cuối cùng của chuổi)
Cú pháp:
rightTrim ( string )
Ví dụ:
Giá trị trả về là RUTH:
RightTrim("RUTH ")
13)hàm left (cắt lấy vị trí xác định bên trái của chuổi)
Cú pháp:
Left ( string, n )
Ví dụ:
Trả về giá trị BABE:
Left("BABE RUTH", 4)
Trả về giá trị BABE RUTH:
Left("BABE RUTH", 40)
//biến em_address trả về giá trị là 40 kí tự đầu tiên của chuổi sle_address
string emp_address
emp_address = Left(sle_address.Text, 40)
14) hàm right (cắt lấy vị trí xác định của chuổi kí tự từ phía bên phải)
Cú pháp:
Right ( string, n )
Ví dụ:
Trả về giá trị RUTH:
Right("BABE RUTH", 4)
Trả về giá trị BABE RUTH:
Right("BABE RUTH", 75)
15) hàm replace (thay thế 1 kí tự bởi 1 kí tự khác)
Cú pháp:
Replace ( string1, start, n, string2 )
Ví dụ:
//Tên davis được thay đổi thành dave
string Name
Name = "Davis"
Name = Replace(Name, 4, 2, "e")
//Chữ BABE RUTH được thay đổi thành BABY RUTH:
Replace("BABE RUTH", 1, 4, "BABY")
//Chuổi kí tự ‘close for vacation’ được thay đổi thành ‘Closed for the Winter:’
Replace("Closed for Vacation", 12, 8, "the Winter")
//Chuổi kí tự ABCDEF được thay bằng ABZZZZ:
Replace("ABCDEF", 3, 2, "ZZZZ")
//Chuổi kí tự ABCDEF được thay bằng ABZZZZ:
Replace("ABCDEF", 3, 50, "ZZZZ")
//Chuổi kí tự ABCDEF được thay bằng ABCDEFZZZZ:
Replace("ABCDEF", 50, 3, "ZZZZ")
16) hàm length (xác định chiều dài byte kí tự của chuổi)
Cú pháp :
olestream.Length ( sizevar )
ví dụ:
17)hàm month (xác định tháng của một giá trị thời gian)
Cú pháp:
Month ( date )
Ví dụ:
//Giá trị trả về là tháng 1:
Month(2004-01-31)
//Biến start_month nhận giá trị tháng của chuổi kiểu ngày đối tượng sle_start_date
integer start_month
start_month = Month(date(sle_start_date.Text))
18) hàm date (chuyển chuổi thành dạng ngày)
Cú phap: Date ( string )
Ví dụ:
//trả về giá trị năm 2004 tháng 7 ngày 04 (kiểu ngày)
Date("2004/07/04")
Date("2004 July 4")
Date("July 4, 2004")
19) các toán tử quan hệ
Toán tử Ý nghĩa Ví dụ
= Bằng nhau Price = 100
> Lớn hơn Price > 100
< Nhỏ hơn Price < 100
<> Không bằng nhau Price <> 100
>= Lớn hơn hoặc bằng nhau Price >= 100
<= Nhỏ hơn hoặc bằng nhau Price <= 100
NOT = Không bằng Price NOT= 100
LIKE Giống Emp_lname LIKE 'C%' OR
Emp_lname LIKE 'G%'
IN Nằm trong giá trị Dept_id IN (100, 200, 500)
BETWEEN Nằm trong khoản giá trị cả giá trị đầu và
giá trị cuối
Price BETWEEN 1000 AND 3000
NOT LIKE Không giống mẫu xác định Emp_lname NOT LIKE 'C%' AND
Emp_lname NOT LIKE 'G%'
NOT IN Không nằm trong giá trị Dept_id NOT IN (100, 200, 500)
NOT BETWEEN không nằm trong khoản xác định cả giá
trị đầu và giá trị cuối
Price NOT BETWEEN 1000 AND
2000
20) hàm today (lấy ngày giờ hệ thống)
Cú pháp:
Today ( )
Ví dụ:
//Lấy ngày tháng hiện tại của hệ thống
Today()
//Nếu ngày tháng hệ thống trước 2003/04/15 thì
IF Today() < 2003-04-15 THEN
//st_date.text bằng tháng/ngày/ năm (dạng chuổi)
st_date.Text = String(Today(), "m/d/yy")
//st_date.text bằng tháng/ ngày/năm/giờ/phút (dạng chuổi)
st_date.Text = String(Today(), "m/d/yy hh:mm")
20.1)hàm cộng trừ ngày
Cú pháp:
RelativeDate ( date, n )
Ví dụ:
Trả lại giá trị ngày 2000-02-10:
RelativeDate(2000-01-31, 10)
Trả lại giá trị ngày 2000-01-21:
RelativeDate(2000-01-31, - 10)
21) hàm string
Cú pháp 1;
String ( data, { format } )
Ví dụ 1 :
-//Trả lại giá trị kiểu chuổi ‘Jan 31, 2002’:
String(2002-01-31, "mmm dd, yyyy")
-//biến date1 trả lại giá trị kiểu chuổi là ‘6-11-2002’
Date order_date = 2002-06-11
string date1
date1 = String(order_date,"m-d-yy")
- //trả lại giá trị chuổi là ‘Jan 31, 2001 6 hrs and 8 min’
String(DateTime(2001-01-31, 06:08:00), 'mmm dd, yyyy h "hrs and" m "min"')
22) hàm number (chuyển giá trị chuổi thành số)
Cú pháp:
Number ( string )
Ví dụ:
//trả lại chuổi ‘24’ thành số24
Number("24")
23) hàm year (xác định năm của chuổi ngày tháng)
Cú pháp:
Year ( date )
Ví dụ:
Trả lại năm 2005:
Year(2005-01-31)
24 ) hàm min và hàm max
Cú pháp:
Min ( x, y )
Max ( x, y )
25)hàm DayNumber
Cú pháp:
DayNumber ( date )
Công dụng:
Trả lại số ngày của tuần
Ví dụ:
integer day_nbr
day_nbr = DayNumber(2000-01-31) // giá trị day_nbr là 4 (là ngày thứ tư của tuần)
26)hàm today
Cú pháp:
Today ( )
Công dụng :
Trả lai giá trị ngày tháng hiện tai
Ví dụ
This.title ='exam'+' '+'today:'+string( today(),'yyyy/mm/dd')+' '+dayname(today())
27)hàm dayname
Công dụng; trả lai thu cua tuan trong ngay
28) hàm upper
Cú pháp: Upper ( string )
Công dụng : Chuyển đổi tất cả các đặc tính trong một chuỗi Tới Chữ hoa.
Ví dụ:
Kết quả trả về BABE RUTH khi thực hiện lệnh phía dưới
Upper("Babe Ruth")
29) hàm filelength, fileopen, fileread
a) hàm filelength
cú pháp: FileLength ( filename )
chức năng: trả lại kích thước của file mà không quá 2 GB
nếu muốn tìm kiểu dữ liệu lớn hơn 2 GB thì dùng lệnh filelength64
ví dụ:
long LengthA
integer li_FileNum
LengthA = FileLength("C:\EAST\EMP.TXT")
li_FileNum = FileOpen("C:\EAST\EMP.TXT", &TextMode!, Read!, LockReadWrite!)
b) hàm fileopen
ví dụ1:
nguyên bản:
This example uses the default arguments and opens the file EMPLOYEE.DAT for reading. The default
settings are LineMode!, Read!, LockReadWrite!, and EncodingANSI!. FileReadEx reads the file line by line
and no other user is able to access the file until it is closed:
Tạm dịch:
Ví dụ này sử dụng những mặt định của chương trình khi mở file emloyee.dat để đọc.
Mặt định là LineMode!, read!, LockReadWrite!, và EncodingANSI!.,và đọc cho đến khi đóng lại
integer li_FileNum
li_FileNum = FileOpen("EMPLOYEE.DAT")
ví dụ 2:
This example opens the file EMPLOYEE.DAT in the DEPT directory in stream mode (StreamMode!) for
write only access (Write!). Existing data is overwritten (Replace!). No other users can write to the file
(LockWrite!)(chưa hiểu)
integer li_FileNum
li_FileNum = FileOpen("C:\DEPT\EMPLOYEE.DAT", & StreamMode!, Write!, LockWrite!, Replace!)
ví dụ 3:
This example creates a new file that uses UTF8 encoding. The file is called new.txt and is in the D:\temp
directory. It is opened in text mode with write-only access, and no other user can read or write to the file:
(chưa hiểu)
integer li_ret
string ls_file
ls_file = "D:\temp\new.txt"
li_ret = FileOpen(ls_file, TextMode!, Write!, & LockReadWrite!, Replace!, EncodingUTF8!)
c)hàm fileread
vậy là 3 hàm nay lien quan mật thiết với nhau
bây gio ta xem xét 3 lệnh trên với một đoạn lệnh thực tế sẽ hiểu hơn về 3 lệnh này và một số lệnh liên quan
String ls_comp_no, ls_fact_no, ls_item, ls_pur_no //khai báo biến kiểu string
String ls_file, ls_filetype
long ll_value, ll_rowcount, ll_flen, ll_loop,ll_count, ll_read //khai báo biến kiểu long
blob lb_tot, lb_get , lb_tot1 //khai báo biến kiểu hình ành
int li_FileNum, li_count, li_count_trans, li_count1 //khai báo biến kiểu interger
If This.AcceptText() < 0 then return //kiểm tra xem có dòng dữ liệu chưa,nếu chưa có dòng nào thì thoát
//bốn biến bên dưới nhận dữ liệu từ datawindow
ls_comp_no = This.GetItemString(This.GetRow(),'comp_no')
ls_fact_no = This.GetItemString(This.GetRow(),'fact_no')
ls_item = This.GetItemString(This.GetRow(),'item')
ls_pur_no = dw_1.GetItemString(dw_1.GetRow(),'pur_no')
select count(*) into :li_count1
from flow_vouchkd@adt b
where comp_no = :ls_comp_no
and fact_no = :ls_fact_no
and vou_no =:ls_pur_no;
if li_count1 > 0 then
select count(*) into :li_count_trans
from flow_vouchkd@adt b,
(select max(id) id
from flow_vouchkd@adt
where comp_no= :ls_comp_no and
fact_no =:ls_fact_no and
vou_no =:ls_pur_no and
votype='AD01') a
where a.id = b.id
and chk_mk IN ( 'E', 'L' );
if li_count_trans = 0 then
messagebox('Caution!','This purm tranfered ADT !!')
return
End if
end if
ll_value = GetFileOpenName("Select File To Upload Data", is_filename, ls_file, "JPG", "JPG Files
(*.jpg),*.jpg," +"BMP Files (*.bmp), *.bmp," + &
"GIF Files (*.gif), *.gif,"+ "XML Files (*.xml),*.xml," +"PDF
Files (*.pdf), *.pdf," + &
"TIF Files (*.tif), *.tif," + "*.*, *.*")
ls_filetype = Upper(Right(Trim(ls_file),3))
IF ll_value < 1 THEN RETURN
ll_flen = FileLength(is_filename) // kiểm tra dung lượng file hình
li_FileNum = FileOpen(is_filename, StreamMode!, Read!, LockRead!)//mở file hình ra
//nếu file hình lớn hơn 32765 byte thì chia file hình ra thành nhiều phần
IF ll_flen > 32765 THEN
IF Mod(ll_flen, 32765) = 0 THEN //nếu chia không dư thì vòng lặp gép hình vừa đủ
ll_loop = ll_flen/32765
ELSE
ll_loop = (ll_flen/32765) + 1//nếu chia hình bị dư ra thì vòng lặp phải cộng phần hình dư
END IF
ELSE
ll_loop = 1//nếu hình nhỏ hơn 32765 byte thì không phải chia hình ra
END IF
FOR ll_count = 1 to ll_loop//bắt đầu lặp và đưa hình vào biến bằng lệnh fileread
ll_read = FileRead(li_FileNum, lb_get)//lb_get là biến nhận hình khi truyền vào
lb_tot = lb_tot + lb_get //nếu hình bị chia nhỏ ra thì bây giờ gộp lai thành lb_tot
NEXT
FileClose(li_FileNum) //sau khi lấy xong thì đóng file lại
SetPointer(Arrow!)//đặt dấu hiệu lên dòng datawindow
IF isNull(lb_tot) THEN
Messagebox("訊訊訊訊","訊訊訊訊訊訊訊訊訊訊訊訊訊 !!")
Return
END IF
This.Object.imag_name[row] = ls_file //lấy tên file đưa vào cột imag_name
This.Object.imag_type[row] = ls_filetype//lấy loại file đưa vào cột imag_type
UPDATE pur_pic
SET imag_name = :ls_file ,
imag_type = :ls_filetype
WHERE comp_no = :ls_comp_no and
fact_no = :ls_fact_no and
pur_no = :ls_pur_no and
item = :ls_item;
IF SQLCA.SQLCODE = -1 THEN
Messagebox("Information2",SQLCA.SQLErrtext)
ROLLBACK USING SQLCA;
RETURN
END IF
COMMIT USING SQLCA;
IF isNull(lb_tot) THEN
Messagebox("訊訊訊訊","訊訊訊訊訊訊訊訊訊訊訊訊訊 !!")
Return
END IF
ib_tot = lb_tot
UPDATEBLOB pur_pic //update hình vào cơ sở dữ liệu
SET PI_IMAG = :lb_tot
WHERE comp_no = :ls_comp_no and
fact_no = :ls_fact_no and
pur_no = :ls_pur_no and
item = :ls_item;
IF SQLCA.SQLCODE = -1 THEN
Messagebox("Information3",SQLCA.SQLErrtext)
ROLLBACK USING SQLCA;
RETURN
END IF
SELECT count(*) into :li_count
FROM pur_pic
WHERE comp_no = :ls_comp_no and
fact_no = :ls_fact_no and
pur_no = :ls_pur_no and
item = :ls_item AND
PI_IMAG IS NULL ;
IF li_count > 0 then
Messagebox('Caution!!!','file dinh kiem khong duoc null!!!')
return
end if
COMMIT USING SQLCA;
30) hàm GetFileSaveName
Cú pháp:
GetFileSaveName ( title, pathname, filename {, extension {, filter { , initdir { , aFlag } } } } )
GetFileSaveName ( title, pathname, filename [ ] {, extension {, filter { , initdir { , aFlag } } } } )
Chức năng:
Hiển thị hộp thoại để chọn đường dẫn nhằm mục đích lưu file vào đường đó
Chức năng lưu file vào đường dẫn
Ví dụ 1:
string ls_path, ls_file
int li_rc
ls_path = sle_1.Text
li_rc = GetFileSaveName ( "Select File", &
ls_path, ls_file, "DOC", &
"All Files (*.*),*.*" , "C:\My Documents", &
32770)
IF li_rc = 1 Then
sle_1.Text = ls_path
End If
//mở ra hộp thoại để lựa chọn đường dẫn,mặt định là file doc và file mặt định la thư mục c:\my ducument,và
phần mở rộng là tất cả các file
Ví dụ 2:
li_rc = GetFileSaveName("SAVE FILE", is_path,is_filename,"", "All Files (*.*),*.*" )
L ) CÁC MỆNH ĐỀ TRONG POWER BUILDER
1) mệnh đề if…then
Cú pháp 1:
IF condition THEN action1 {ELSE action2}
Ví dụ 1 (3 dạng khác nhau)
-IF Num = 1 THEN Open(w_first) ELSE Open(w_rest)
-IF sle_State.text="TX" THEN MessageBox("Hello","Tex")
-IF w_first.X > w_second.X THEN
w_first.X = 0
END IF
Cú pháp 2:
IF condition1 THEN
action1
{ ELSEIF condition2 THEN
action2
. . . }
{ ELSE
action3 }
END IF
2) vòng lặp do… loop (có bốn dạng khác nhau)
2.1 ) do until
Cú pháp:
DO UNTIL điều kiện
khối phát biểu
LOOP
Ví dụ:
// thực hiện vòng lặp trừ khi A>15
integer A = 1, B = 1
DO UNTIL A > 15
Beep(A)
A = (A + 1) * B
LOOP
2.2) do while
Cú pháp:
DO WHILE điều kiện
Khối phát biểu
LOOP
Ví dụ:
//vòng lặp được thực hiện khia<=15
integer A = 1, B = 1
DO WHILE A <= 15
Beep(A)
A = (A + 1) * B
LOOP
2.3 ) loop until
Cú pháp:
DO
khối phát biểu
LOOP UNTIL điều kiện
Ví dụ:
//lặp trừ khi A>15
integer A = 1, B = 1
DO
Beep(A)
A = (A + 1) * B
LOOP UNTIL A > 15
2.4 ) loop while
Cú pháp:
DO
khối phát biểu
LOOP WHILE điều kiện
Ví dụ:
// thực hiện lệnh khi A<=15
integer A = 1, B = 1
DO
Beep(A)
A = (A + 1) * B
LOOP WHILE A <= 15
3) vòng lặp for to…next (lặp đến số xác định)
Cú pháp:
FOR varname = start TO end {STEP increment}
khối phát biểu
NEXT
Ví dụ 1:
//bắt đầu thực hiện lặp từ 5 tới hết 25
FOR n = 5 to 25
A = A+10
NEXT
Ví dụ 2:
//bắt đầu lặp ở vị trí thứ 5 và mỗi lần lặp nhảy 5 bật(tổng cộng 5 vòng lặp)
FOR N = 5 TO 25 STEP 5
A = A+10
NEXT
Ví dụ 3: //vòng lặp lồng nhau cho ra ma trận
Int Matrix[100,50,200]
FOR i = 1 to 100
FOR j = 1 to 50
FOR k = 1 to 200
Matrix[i,j,k]=1
NEXT
NEXT
NEXT
4)phát biểu choose case
Cú pháp:
CHOOSE CASE biến hoặt sự kiện để đưa ra biểu thức
CASE biểu thức
khối phát biểu
{ CASE biểu thức
khối phát biểu
CASE biểu thức
khối phát biểu }
CASE ELSE
khối phát biểu}
END CHOOSE
ví dụ 1
CHOOSE CASE Weight
CASE IS<16
Postage=Weight*0.30
Method="USPS"
CASE 16 to 48
Postage=4.50
Method="UPS"
CASE ELSE
Postage=25.00
Method="FedEx"
END CHOOSE
Ví dụ thực tế:
String ls_base_no,ls_voutype
select base_no into :ls_base_no from dbbase;
choose case trim(ls_base_no)
case 'BC'
if as_stk_no = 'K002' THEN
ls_voutype = 'W101'