相关需求及信息请点击这里查看。
用vb代码实现方法
引用:无,部件:无
设计:在form1中右下角加入一个commandbutton,名称默认为command1,窗体的autoredraw属性设为true
窗体文件一:form1
option explicit
private mbom as collection 这是入口的集合
private mbomreturn as collection 这是出口的集合,未经处理
private mbomreturnlast as collection 这是出口的集合,经过处理
private sub addbomrecord()
在这里往mbom加入数据库里面的原内容,为求简便,我不想连接数据库
直接往里面写入记录了,如果需要,你就直接连接数据库,分析一下里面的
代码,然后再往mbom里面写入记录
fg sa1 2.0000
fg sa2 3.0000
sa1 pt1 4.0000
sa1 pt2 5.0000
sa2 pt1 6.0000
sa2 pt3 7.0000
dim mbomvalue as cbomvalue
set mbomvalue = new cbomvalue
mbomvalue.assbom = “fg”
mbomvalue.bompoint = “sa1”
mbomvalue.quantity = 2
mbom.add mbomvalue
set mbomvalue = new cbomvalue
mbomvalue.assbom = “fg”
mbomvalue.bompoint = “sa2”
mbomvalue.quantity = 3
mbom.add mbomvalue
set mbomvalue = new cbomvalue
mbomvalue.assbom = “sa1”
mbomvalue.bompoint = “pt1”
mbomvalue.quantity = 4
mbom.add mbomvalue
set mbomvalue = new cbomvalue
mbomvalue.assbom = “sa1”
mbomvalue.bompoint = “pt2”
mbomvalue.quantity = 5
mbom.add mbomvalue
set mbomvalue = new cbomvalue
mbomvalue.assbom = “sa2”
mbomvalue.bompoint = “pt1”
mbomvalue.quantity = 6
mbom.add mbomvalue
set mbomvalue = new cbomvalue
mbomvalue.assbom = “sa2”
mbomvalue.bompoint = “pt3”
mbomvalue.quantity = 7
mbom.add mbomvalue
end sub
private sub command1_click()
dim i as integer
dim m as cbomvalue
进行计算
注意以下两个新建实例,必须放置于getbomlist前,该操作也有清空现有数据的作用,否则会造成错误
即第一次运行后保存了数据于该两个变量中,并未清除相关记录,而下一次运行则在现有的基础上再进行加操作,因此数据错误了。
set mbomreturn = new collection
set mbomreturnlast = new collection
call getbomlist
计算后,mbomreturnlast返回的就是最终结果
if mbomreturnlast.count < 0 then
msgbox “没有记录!”, vbinformation + vbokonly, “bom表计算”
exit sub
else
在窗体中打印出列表的内容
me.cls
print “assbom” & vbtab & “point” & vbtab & “quantity”
for i = 1 to mbomreturnlast.count
set m = mbomreturnlast.item(i)
print m.assbom & vbtab & m.bompoint & vbtab & m.quantity
next i
end if
end sub
private sub form_load()
窗体调用处新建实例,然后再装入数据
set mbom = new collection
addbomrecord
end sub
***************************************************************
*
* 以下为进行计算部分的代码,注意collection里面的处理
*
***************************************************************
private sub getbomlist()
dim mbomtop as collection 这里保存了顶级产成品
dim i as integer
dim j as integer
dim m as cbomreturnvalue
dim mlast as cbomvalue
dim bfind as boolean
set mbomtop = new collection
装入顶级产成品
loadbomtop mbomtop
对顶级产品进行下级的判断
for i = 1 to mbomtop.count
最后一个参数为1,表示一个单位的产成品
call calcnextbom(mbomtop.item(i), mbomtop.item(i), “1”)
next i
最终得以mbomreturn,这里面已初步形成了结果了
再进行表达式计算,得到的值返回到mbomreturnlast中,注:mbomreturnlast这个集合加入cbomvalue内容
for i = 1 to mbomreturn.count
处理一下最终结果,如果没有在collection里面发现相同的assbom及bompoint,则新增加一个,如果已发现,仅只是数量相加
set m = mbomreturn(i)
查找是否已加入
bfind = false
for j = 1 to mbomreturnlast.count
set mlast = mbomreturnlast(j)
if trim(mlast.assbom) = trim(m.assbom) and trim(mlast.bompoint) = trim(m.bompoint) then
如果发现有相同的,则加入相关数字
mlast.quantity = mlast.quantity + calcexpression(m.expression)
bfind = true
end if
next j
if bfind = false then
如果没有找到
set mlast = new cbomvalue
mlast.assbom = trim(m.assbom)
mlast.bompoint = trim(m.bompoint)
mlast.quantity = calcexpression(trim(m.expression))
mbomreturnlast.add mlast
end if
next i
所有操作完毕
end sub
private sub loadbomtop(byref bomtop as collection)
装入顶级产成品,并返回到bomtop中
即存储过程中getbomlist中的第一个游标的创建@bomtop
dim i as integer
dim j as integer
dim n as integer
dim bmark as boolean 这只是一个标识符,表明是否发现非顶级
dim bmarkadd as boolean 用于判断是否已加入到bomtop中的标识
判断方法,如果assbom不在bompoint中,那就是顶级了
dim sbomassbom as string
for i = 1 to mbom.count
sbomassbom = trim(mbom.item(i).assbom)
再进行循环
bmark = false
for j = 1 to mbom.count
if sbomassbom = trim(mbom.item(j).bompoint) then
bmark = true
end if
next j
if bmark = false then
如果没有发现有相同的,则bomtop加入
加入前需要进行判断是否已加入
for n = 1 to bomtop.count
if bomtop.item(n) = sbomassbom then
bmarkadd = true
end if
next n
if bmarkadd = false then
如果没有加入过,则加入
bomtop.add sbomassbom
end if
end if
next i
end sub
getbomtruelist的存储过程用vb来描述
private sub calcnextbom(sassbom as string, sasspoint as string, sexp as string)
dim dquan as double
dim sexpression as string
dim spoint as string
dim bomtop as string
创建point_cursor处的游标
dim mbompoint as collection
set mbompoint = new collection
装入相关的集合
call loadnextpoint(mbompoint, sasspoint)
装入完毕后,再进行判断是否为明细级半成品,如果不是,递归一次本函数,如果是,加入到mbomreturn里面去
dim i as integer
dim mbomreturnvalue as cbomreturnvalue
for i = 1 to mbompoint.count
判断是否为明细级
if isdetailpoint(trim(mbompoint.item(i).bompoint)) = true then
如果是明细级,则加入到cbomreturnvalue
set mbomreturnvalue = new cbomreturnvalue
mbomreturnvalue.assbom = trim(sassbom)
mbomreturnvalue.bompoint = trim(mbompoint.item(i).bompoint)
构建表达式
mbomreturnvalue.expression = sexp & “*” & trim(cstr(mbompoint.item(i).quantity))
mbomreturnvalue.quantity = mbompoint.item(i).quantity
加入
mbomreturn.add mbomreturnvalue
else
如果不是明细项,则再次递归,注意传入的第一个参数,总是顶级bom,仅作标识符用
call calcnextbom(sassbom, trim(mbompoint.item(i).bompoint), sexp & “*” & trim(cstr(mbompoint.item(i).quantity)))
end if
next i
end sub
private sub loadnextpoint(byref bompoint as collection, byval pointname as string)
相当于getbomtruelist中的游标中的select distinct point,sl from te where assbom = @pointname
dim i as integer
dim j as integer
dim bmark as boolean
dim mpointvalue as cpointvalue
for i = 1 to mbom.count
bmark = false
if trim(mbom.item(i).assbom) = trim(pointname) then
判断是否已加入
for j = 1 to bompoint.count
if trim(bompoint.item(j).bompoint) = trim(mbom.item(i).bompoint) and bompoint.item(j).quantity = mbom.item(i).quantity then
bmark = true
end if
next j
if bmark = false then
表示没有加入
set mpointvalue = new cpointvalue
mpointvalue.bompoint = trim(mbom.item(i).bompoint)
mpointvalue.quantity = mbom.item(i).quantity
bompoint.add mpointvalue
end if
end if
next i
end sub
private function isdetailpoint(byval pointname as string) as boolean
判断是否为底级半成品
只需要判断pointname不在mbom的assbom项中即可
dim i as integer
for i = 1 to mbom.count
if trim(mbom.item(i).assbom) = trim(pointname) then
如果找到了,直接返回false,并退出函数
isdetailpoint = false
exit function
end if
next i
如果到了这里还没有找到,那么就肯定是底级了
isdetailpoint = true
end function
public function calcexpression(strexp as string) as double
计算处理中的表达式,注意,只有乘法
dim sitemexp() as string
dim dreturnvalue as double
dim iindex as integer
sitemexp = split(trim(strexp), “*”)
if ubound(sitemexp) < 0 then
calcexpression = 0
else
dreturnvalue = 1
for iindex = 0 to ubound(sitemexp)
if trim(sitemexp(iindex)) = “” then
sitemexp(iindex) = 0
end if
dreturnvalue = dreturnvalue * cdbl(sitemexp(iindex))
next iindex
calcexpression = dreturnvalue
end if
end function
类模块一:类名:cbomreturnvalue
option explicit
保持属性值的局部变量
private mvarassbom as string 局部复制
private mvarbompoint as string 局部复制
private mvarquantity as double 局部复制
private mvarexpression as string 局部复制
public property let expression(byval vdata as string)
向属性指派值时使用,位于赋值语句的左边。
syntax: x.expression = 5
mvarexpression = vdata
end property
public property get expression() as string
检索属性值时使用,位于赋值语句的右边。
syntax: debug.print x.expression
expression = mvarexpression
end property
public property let quantity(byval vdata as double)
向属性指派值时使用,位于赋值语句的左边。
syntax: x.quantity = 5
mvarquantity = vdata
end property
public property get quantity() as double
检索属性值时使用,位于赋值语句的右边。
syntax: debug.print x.quantity
quantity = mvarquantity
end property
public property let bompoint(byval vdata as string)
向属性指派值时使用,位于赋值语句的左边。
syntax: x.bompoint = 5
mvarbompoint = vdata
end property
public property get bompoint() as string
检索属性值时使用,位于赋值语句的右边。
syntax: debug.print x.bompoint
bompoint = mvarbompoint
end property
public property let assbom(byval vdata as string)
向属性指派值时使用,位于赋值语句的左边。
syntax: x.assbom = 5
mvarassbom = vdata
end property
public property get assbom() as string
检索属性值时使用,位于赋值语句的右边。
syntax: debug.print x.assbom
assbom = mvarassbom
end property
类模块二:类名:cbomvalue
option explicit
保持属性值的局部变量
private mvarassbom as string 局部复制
private mvarbompoint as string 局部复制
private mvarquantity as double 局部复制
public property let quantity(byval vdata as double)
向属性指派值时使用,位于赋值语句的左边。
syntax: x.quantity = 5
mvarquantity = vdata
end property
public property get quantity() as double
检索属性值时使用,位于赋值语句的右边。
syntax: debug.print x.quantity
quantity = mvarquantity
end property
public property let bompoint(byval vdata as string)
向属性指派值时使用,位于赋值语句的左边。
syntax: x.bompoint = 5
mvarbompoint = vdata
end property
public property get bompoint() as string
检索属性值时使用,位于赋值语句的右边。
syntax: debug.print x.bompoint
bompoint = mvarbompoint
end property
public property let assbom(byval vdata as string)
向属性指派值时使用,位于赋值语句的左边。
syntax: x.assbom = 5
mvarassbom = vdata
end property
public property get assbom() as string
检索属性值时使用,位于赋值语句的右边。
syntax: debug.print x.assbom
assbom = mvarassbom
end property
类模块三:类名:cpointvalue
option explicit
保持属性值的局部变量
private mvarbompoint as string 局部复制
private mvarquantity as double 局部复制
public property let quantity(byval vdata as double)
向属性指派值时使用,位于赋值语句的左边。
syntax: x.quantity = 5
mvarquantity = vdata
end property
public property get quantity() as double
检索属性值时使用,位于赋值语句的右边。
syntax: debug.print x.quantity
quantity = mvarquantity
end property
public property let bompoint(byval vdata as string)
向属性指派值时使用,位于赋值语句的左边。
syntax: x.bompoint = 5
mvarbompoint = vdata
end property
public property get bompoint() as string
检索属性值时使用,位于赋值语句的右边。
syntax: debug.print x.bompoint
bompoint = mvarbompoint
end property
加入后可直接在窗体中print出列表。