VB中用treeview已Access(含多个表)的表名按分类建立有层次的结构树

栏目:资讯发布:2023-10-10浏览:3收藏

VB中用treeview已Access(含多个表)的表名按分类建立有层次的结构树,第1张

数据库这样设计
部件表(Id,Name)
零件表(Id,partId,Name)

公共模块
___________________________________
Private conn As New ADODBConnection

Public Sub OpenConn()
'textmdb改成你的Access数据库名
With conn
If State = adStateClosed Then
ConnectionString = "Provider=MicrosoftJetOLEDB40;Data Source=" & AppPath & "/textmdb;Persist Security Info=False"
Open
End If
End With
End Sub

Public Function OpenRecordset(ByVal strSql As String) As ADODBRecordset
Dim rs As New ADODBRecordset
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 ADODBRecordset, prs As ADODBRecordset
Dim cnode As Node, pnode As Node

OpenConn
Set root = trvNodesAdd(, , , "根节点")

strSql = "select from 部件表"
Set rs = OpenRecordset(strSql)
Do While Not rsEOF
Set cnode = trvNodesAdd(root, 4, , rs!Name)
cnodeTag = rs!id

strSql = "select from 零件表 where partid = " & rs!id
Set prs = OpenRecordset(strSql)

Do While Not prsEOF

Set pnode = trvNodesAdd(cnode, 4, , prs!Name)
pnodeTag = prs!id

prsMoveNext

Loop

cnodeExpanded = True
rsMoveNext

Loop
rootExpanded = True
End Sub

VB中用treeview已Access(含多个表)的表名按分类建立有层次的结构树

数据库这样设计部件表(Id,Name)零件表(Id,partId,Name)公共模块___________________________________Private conn As New ADODBConnect...
点击下载
热门文章
    确认删除?
    回到顶部