I just created my FIRST One to Many Form with HMG.
It is a very basic sale invoice entry form, with 2 combo boxes (one for customer another for Item in grid). It can auto calculate sale invoice amount.
I shall incomporate AddNew, Delete, Save, Print etc later.
Please check and tell me how to improve. I need your advice very much to learn HMG
I couldn't write this program without your active support.
Code: Select all
#include "minigui.ch"
static aGrid := {}, aCustSource := {}, aCustDisp := {}, ;
aItemSource := {}, aItemDisp := {}, aItemRate := {}, lChanged := .f.
Function Main
local i, gb
gb := {|| CalcAmt()}
CreateTable()
CreateLookup()
DEFINE WINDOW Form_1 ;
AT 0,0 ;
WIDTH 640 ;
HEIGHT 460 ;
TITLE "One To Many Form" ;
MAIN ;
on init RefreshWin()
@ 10, 10 label lblInvno value "Invoice No.:"
@ 10, 100 textbox txtInvno ;
readonly
@ 10, 300 label lblInvdt value "Invoice Dt.:"
@ 10, 400 datepicker dpkInvdt
@ 40, 10 label lblCustcd value "Customer:"
@ 40, 100 COMBOBOX cboCustcd ;
items aCustDisp ;
width 200
@ 70,10 GRID grdSaledtl ;
WIDTH 500 ;
HEIGHT 210 ;
HEADERS {'Item', 'Rate', 'Qty', 'Amt'} ;
WIDTHS {200,100,100,100} ;
COLUMNCONTROLS { {'COMBOBOX', aItemDisp}, ;
{'TEXTBOX', 'NUMERIC', '999999.99'}, ;
{'TEXTBOX', 'NUMERIC', '999999'}, ;
{'TEXTBOX', 'NUMERIC', '99999999.99'}} ;
edit ;
on lostfocus calcamt()
@ 330, 300 label lblTotamt value "Total:"
@ 330, 400 textbox txtTotamt ;
readonly ;
numeric ;
inputmask "9999999.99"
@ 360, 10 button cmdPrev caption "&Previous" ;
action MovePrev()
@ 360, 120 button cmdNext caption "&Next" ;
action MoveNext()
END WINDOW
CENTER WINDOW Form_1
ACTIVATE WINDOW Form_1
Return
FUNCTION CreateLookup()
local i
aItemSource := {}
aItemDisp := {}
aItemRate := {}
aCustSource := {}
aCustDisp := {}
select item
do while !eof()
aadd(aItemSource, item->itemcd)
aadd(aItemDisp, item->itemnm)
aadd(aItemRate, item->rate)
skip
enddo
select cust
do while !eof()
aadd(aCustSource, cust->custcd)
aadd(aCustDisp, cust->custnm)
skip
enddo
return nil
FUNCTION RefreshWin()
form_1.txtInvno.value := sale->invno
form_1.dpkInvdt.value := sale->invdt
form_1.cboCustcd.value := ascan(aCustSource, {|x| x == sale->custcd})
form_1.txtTotamt.value := sale->totamt
aGrid := {}
select saledtl
locate for invno = sale->invno
do while !eof()
aadd(aGrid, {ascan(aItemSource, {|x| x == saledtl->itemcd}), ;
saledtl->rate, saledtl->qty, saledtl->rate*saledtl->qty})
continue
enddo
form_1.grdSaledtl.DeleteAllItems()
for i = 1 to len(aGrid)
form_1.grdSaledtl.additem(aGrid[i])
next
return nil
PROCEDURE CalcAmt()
local i, nIctr, aTemp := {}, mTotAmt := 0.00
// lChange := .t.
i := form_1.grdSaledtl.value
nIctr := form_1.grdSaledtl.cell(i, 1)
aTemp := form_1.grdSaledtl.item(i)
if i < 1
return
endif
aTemp[2] := iif(nIctr>0 .and. nIctr<=len(aItemRate), aItemRate[nIctr], 0.00)
aTemp[4] := aTemp[2]*aTemp[3]
form_1.grdSaledtl.item(i) := aTemp
for i = 1 to form_1.grdSaledtl.Itemcount
mTotAmt += form_1.grdSaledtl.cell(i, 4)
next
form_1.txtTotamt.value := mTotAmt
return
PROCEDURE MovePrev()
select sale
if !bof()
skip -1
endif
if bof()
go top
endif
RefreshWin()
PROCEDURE MoveNext()
select sale
if !eof()
skip
endif
if eof()
go bottom
endif
RefreshWin()
FUNCTION CreateTable()
local aDbf := {}
if !file("item.dbf")
aDbf := {}
aadd(adbf, {"itemcd", "c", 10, 0})
aadd(adbf, {"itemnm", "c", 20, 0})
aadd(adbf, {"rate", "n", 8, 2})
dbcreate("item", adbf)
use item new
select item
append blank
replace itemcd with "COMP"
replace itemnm with "Computer"
replace rate with 20000.00
append blank
replace itemcd with "CD"
replace itemnm with "Compact Disk"
replace rate with 10.00
append blank
replace itemcd with "OPM"
replace itemnm with "Optical Mouse"
replace rate with 400.00
use
endif
if !file("cust.dbf")
aDbf := {}
aadd(adbf, {"custcd", "c", 10, 0})
aadd(adbf, {"custnm", "c", 20, 0})
dbcreate("cust", adbf)
use cust new
select cust
append blank
cust->custcd := "CUST1"
cust->custnm := "Customer One"
append blank
cust->custcd := "CUST2"
cust->custnm := "Customer Two"
append blank
cust->custcd := "CUST3"
cust->custnm := "Customer Three"
use
endif
if !file("sale.dbf")
aDbf := {}
aadd(adbf, {"invno", "c", 10, 0})
aadd(adbf, {"invdt", "d", 8, 0})
aadd(adbf, {"custcd", "c", 10, 0})
aadd(adbf, {"totamt", "n", 10, 2})
dbcreate("sale", adbf)
use sale new
select sale
append blank
sale->invno := str(1, 10)
sale->invdt := date()
sale->custcd := "CUST1"
sale->totamt := 40200.00
append blank
sale->invno := str(2, 10)
sale->invdt := date()
sale->custcd := "CUST2"
sale->totamt := 420.00
use
endif
if !file("saledtl.dbf")
aDbf := {}
aadd(adbf, {"invno", "c", 10, 0})
aadd(adbf, {"itemcd", "c", 10, 0})
aadd(adbf, {"rate", "n", 8, 2})
aadd(adbf, {"qty", "n", 6, 0})
dbcreate("saledtl", adbf)
use saledtl new
select saledtl
append blank
saledtl->invno := str(1, 10)
saledtl->itemcd := "COMP"
saledtl->rate := 20000.00
saledtl->qty := 2
append blank
saledtl->invno := str(1, 10)
saledtl->itemcd := "CD"
saledtl->rate := 10.00
saledtl->qty := 20
append blank
saledtl->invno := str(2, 10)
saledtl->itemcd := "CD"
saledtl->rate := 10.00
saledtl->qty := 5
append blank
saledtl->invno := str(2, 10)
saledtl->itemcd := "OPM"
saledtl->rate := 400.00
saledtl->qty := 1
use
endif
close databases
use item new
use cust new
use sale new
use saledtl new
return nil
1. Please copy the codes into a .prg file (e.g., demo.prg) in a folder.
2. Go to command prompt and change directory to that folder.
3. Run C:\HMG\BATCH\COMPILE demo
This will create an exe file. Please run the exe file (it will automatically create required tables)
With best regards.
Sudip