数据库这样设计
部件表(Id,Name)
零件表(Id,partId,Name)
公共模块
___________________________________
Private conn As New ADODB.Connection
Public Sub OpenConn()
'text.mdb改成你的Access数据库名
With conn
If .State = adStateClosed Then
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "/text.mdb;Persist Security Info=False"
.Open
End If
End With
End Sub
Public Function OpenRecordset(ByVal strSql As String) As ADODB.Recordset
Dim rs As New ADODB.Recordset
With rs
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.Open strSql, conn, , , adCmdText
End With
Set OpenRecordset = rs
End Function
Public Function RunTrans(ByVal tranSql As String)
With conn
.BeginTrans
.Execute tranSql
.CommitTrans
End With
End Function
窗体,trv是TreeView控件
_____________________
Dim root As Node
Private Sub Form_Load()
Dim strSql As String
Dim rs As ADODB.Recordset, prs As ADODB.Recordset
Dim cnode As Node, pnode As Node
OpenConn
Set root = trv.Nodes.Add(, , , "根节点")
strSql = "select * from 部件表"
Set rs = OpenRecordset(strSql)
Do While Not rs.EOF
Set cnode = trv.Nodes.Add(root, 4, , rs!Name)
cnode.Tag = rs!id
strSql = "select * from 零件表 where partid = " & rs!id
Set prs = OpenRecordset(strSql)
Do While Not prs.EOF
Set pnode = trv.Nodes.Add(cnode, 4, , prs!Name)
pnode.Tag = prs!id
prs.MoveNext
Loop
cnode.Expanded = True
rs.MoveNext
Loop
root.Expanded = True
End Sub