BOM表查询的VB实现方法
用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