Access的BOM开发(3)BOM展开

hi,大家好呀,手机边亲爱的你还好吗!

今天我们接着来讲一下BOM。我们在经历了几周的等待后,今天终于要迎来大结局了。今天我们就来把BOM最后一个功能来讲解一下。

首先,我们先来讲解一下什么是BOM展开(注意我这里并不是官方解释,也不是权威解释,所以有讲的不正确的地方请指正),BOM的展开是从一个层级开始将所有的子项一一展开,比如我们BOM示例中用到的电脑的数据,我们电脑的主机箱展开就是,机箱、主板、硬盘、显卡、网卡、电源、内存,同时还要将主板的子项展开,那就是还有CPU、北桥、南桥、CMOS电池等等,一直要展开到组装电脑用到的螺丝。

从以上的举例我们可以知道,就是要把BOM中子项的子项全展开,展开到最小项为止,那这样其实就是用到了递归(大家非常期待的递归),最终BOM展开后我们将数据挂到树上呈现出来。那我们分几步来操作一下。

×

01、建窗体

如下图,我们需要新建一个窗体,在窗体上放一个文本框(txtProductCode),一个按钮(btnOK),一个树控件(Bom_Tree),一个标签控件(Label_Path),一个子窗体控件(frmChild)

子窗体为数据表窗体,窗体的数据源为(qryBOM):

SELECT tblBOM.ID, tblBOM.ProductID, tblBOM.ProductParentID, tblBOM.Qty, tblBOM.Unit, 
tblBOM.IsEnabled, tblBOM.Remark, tblProduct.ProductCode AS 子物料代码, tblProduct.ProductName AS 
子物料名称, tblProduct_1.ProductName AS 父物料名称, tblProduct_1.ProductCode AS 父物料代码
FROM (tblBOM LEFT JOIN tblProduct ON tblBOM.ProductID = tblProduct.ID) LEFT JOIN tblProduct AS 
tblProduct_1 ON tblBOM.ProductParentID = tblProduct_1.ID;

×

02、添加代码

Private Sub btnOK_Click()
    GetTree
End Sub

Private Sub Form_Load()
    Me.Bom_Tree.Nodes.Clear
    Me.frmChild.Form.RecordSource = "select * from qryBOM  where 1=2"
    Me.Label_Path.Caption = ""
End Sub
Private Sub GetTree()
    On Error GoTo ErrorHandler
    Dim objNode As Object    ' MSComctlLib.Node

    Me.Bom_Tree.Nodes.Clear


    Dim strID As Long
    Dim strName As String
    strID = Nz(DLookup("ID""tblProduct""ProductCode='" & Me.txtProductCode & "'"), 0)
    strName = Nz(DLookup("ProductName""tblProduct""ProductCode='" & Me.txtProductCode & "'"), 0)
    Set objNode = Me.Bom_Tree.Nodes.Add(, , "K", strName)   '添加树控件底层节点

    Call ShowBOM(strID)
    objNode.Expanded = True '
ExitHere:
    Exit Sub
ErrorHandler:
    MsgBox Err.Description, vbCritical
    Resume ExitHere
End Sub
Private Sub ShowBOM(strID As Long)
    On Error GoTo ErrorHandler

    Dim rst As Object
    Dim strSQL As String
    Dim strP As String
    strSQL = "select * from qryBOM where ProductParentID=" & Nz(strID, 0)
    Set rst = CurrentDb.OpenRecordset(strSQL)

    Do Until rst.EOF
        If DCount("*""qryBOM""ProductID=" & Nz(strID, 0)) = 0 Or Nz(DLookup("ID""tblProduct""ProductCode='" & Me.txtProductCode & "'"), 0) = strID Then
            strP = "K"
        Else
            strP = "K" & rst!ProductParentID
        End If
        Me.Bom_Tree.Nodes.Add strP, tvwChild, "K" & rst!ProductID, rst!子物料名称 '添加树控件节点
        If DCount("*""qryBOM""ProductParentID=" & Nz(rst!ProductID, 0)) > 0 Then
            Call ShowBOM(rst!ProductID)
        End If
        rst.MoveNext
    Loop
    rst.Close


ExitHere:
Set rst = Nothing
    Exit Sub
ErrorHandler:
    MsgBox Err.Description, vbCritical
    Resume ExitHere
End Sub

Private Sub Bom_Tree_LostFocus()
    Set Me.Bom_Tree.DropHighlight = Me.Bom_Tree.SelectedItem
End Sub

Private Sub Bom_Tree_GotFocus()
    Set Me.Bom_Tree.DropHighlight = Nothing
End Sub

Private Sub Bom_Tree_NodeClick(ByVal Node As Object)
    Dim strSQL As String

    Me.Label_Path.Caption = Node.FullPath
    Me.Bom_Tree.SetFocus
    Dim objNode  As Node

    Set objNode = Me.Bom_Tree.SelectedItem

    If objNode.Key <> "K" Then

        If DCount("*""qryBOM""ProductParentID=" & CLng(Mid(objNode.Key2))) > 1 Then
            strSQL = "select * from qryBOM where ProductParentID=" & CLng(Mid(objNode.Key2))

        Else
            strSQL = "select * from qryBOM where ProductID=" & CLng(Mid(objNode.Key2))
        End If
    Else
        strSQL = "select * from qryBOM where 父物料名称='" & objNode.Text & "'"
    End If
    Me.frmChild.Form.RecordSource = strSQL

End Sub

我们自己写了一个函数,在这里我们用到了递归,具体的我们在后面的视频中说明

×

03、运行测试

最后一步就是运行测试了

注意,展开需要输入完整的产品代码,不能用模糊查询。

我们这次分享的内容主要是用于学习树控件,我们只是整了一个简单的BOM系统 ,如果需要当作一个系统来使用的话,还需要把目前的功能完善,那这个任务就交给大家吧。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

易登软件Access开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值