Hello,
I want too use a treeview in my userform.
In my program I connect too a OPC server the OPC server contains different channels, those channels contains devices and the devices contain items, those items could be a collection of items or individual items.
I have already used a listbox and listview in my userform, but i'm not particulary happy with the results. So i want too add the treeview because i think it would look and work better.
I'll add all of my code and i hope you can see what i mean. I have found some examples but they use data on a worksheet which is a bit different too how i want too do it. I have found out the .add that is used there doesn't work and i'm having trouble finding what i need. for some reason opening the help files is blocked on win xp.
Option Explicit
Dim WithEvents OPCMyserver As OPCServer 'OPC Server object
Dim WithEvents OPCMygroups As OPCGroups 'OPC Group object collection
Dim WithEvents OPCMygroup As OPCGroup 'OPC Group object
Dim OPCMyitems As OPCitems 'OPC Item object collection
Dim OPCMyitem As OPCItem 'OPC Item object
Dim OPCMyBrowser As OPCBrowser 'OPC Browser object
Dim ItemServerHandles() As Long 'OPC Item Handle
Dim ClientHandles(1) As Long 'OPC Client Handle
Dim OPCItemIDs(1) As String 'OPC Item ID
Dim Errors() As Long 'OPC Item Errors
'Dim NombreItem As Integer 'Ne sert a rien pour l'instant
Dim BrowseLevel As Integer 'Current level in the OPC Browser
Dim ConnectFlag As Boolean 'If connection established, ConnectFlag = True, Else = False
Dim Dummyserver As OPCServer
Dim AllOPCServers As Variant
Dim i As Integer
Private Sub Down_Click()
Dim vName As Variant
If Left$(Devicelist.Text, 1) = "+" Then 'If the device contains Items....
vName = Mid$(Devicelist.Text, 2)
Else
Exit Sub 'If not, we leave
End If
OPCMyBrowser.MoveDown (vName) '...we move down in the browser
Devicelist.Clear
OPCMyBrowser.ShowBranches 'We get the branches of the tree at the "vname" location
For Each vName In OPCMyBrowser
Devicelist.AddItem "+" + vName
Next vName
OPCMyBrowser.ShowLeafs 'We get the leaves of the tree at the "vname" location
For Each vName In OPCMyBrowser
Devicelist.AddItem vName
Next vName
BrowseLevel = BrowseLevel + 1 'Current level in the tree
End Sub
Private Sub Up_Click()
Dim vName As Variant
If BrowseLevel = 0 Then Exit Sub 'If tree's top,nothing to do
OPCMyBrowser.MoveUp 'Else, we move up in the browser
Devicelist.Clear
OPCMyBrowser.ShowBranches 'We get the branches of the tree at the "vname" location
For Each vName In OPCMyBrowser
Devicelist.AddItem "+" + vName
Next vName
OPCMyBrowser.ShowLeafs 'We get the leaves of the tree at the "vname" location
For Each vName In OPCMyBrowser
Devicelist.AddItem vName
Next vName
BrowseLevel = BrowseLevel - 1 'Current level in the tree
End Sub
Private Sub UserForm_Initialize()
With OPCitems 'Initializing of the ListView, 4 labelled collumns
.ColumnHeaders.Add , , "Item", .Width * 30 / 100
.ColumnHeaders.Add , , "Value", .Width * 20 / 100
.ColumnHeaders.Add , , "TimeStamp", .Width * 20 / 100
.ColumnHeaders.Add , , "Quality", .Width * 20 / 100
End With
With OPCtree
.LineStyle = tvwRootLines
End With
Serverzoeken.Clear
Set Dummyserver = New OPCServer
AllOPCServers = Dummyserver.GetOPCServers()
For i = LBound(AllOPCServers) To UBound(AllOPCServers) ' fill the combobox
Serverzoeken.AddItem AllOPCServers(i) '
Next i '
Serverzoeken.ListIndex = 0
If Showserver.Caption = "" Then 'If no OPC server found, we display an Error MsgBox
MsgBox "Impossible to find an active OPC server", vbOKOnly + vbExclamation
End If
Set Dummyserver = Nothing
End Sub
Private Sub Serverzoeken_Click()
Showserver.Caption = Serverzoeken.Value
End Sub
Private Sub Connect_Click()
Devicelist.Clear
OPCitems.ListItems.Clear
With OPCtree.Nodes
.Clear
End With
Dim vName As Variant
If ConnectFlag = False Then 'If we are not connected
On Error GoTo ConnectError
Set OPCMyserver = New OPCServer
OPCMyserver.Connect Showserver.Caption 'We try to connect to OPC server
Set OPCMygroups = OPCMyserver.OPCGroups 'We load the OPC Groups
Set OPCMygroup = OPCMygroups.Add("Group_1") 'We add a group, the name is not important
Set OPCMyitems = OPCMygroup.OPCitems 'We load the OPC Items located in the OPC Group
Set OPCMyBrowser = OPCMyserver.CreateBrowser 'For the navigation in the OPC server
OPCMyBrowser.ShowBranches
For Each vName In OPCMyBrowser 'All the Variant are displayed with "+"
Devicelist.AddItem "+" + vName
Next vName
positie.Text = Mid$(Devicelist.List(0), 2) 'We display the first device found
If positie.Text = "" Then 'If there is no device
OPCMyserver.Disconnect 'We dicsonnect, we clear objects, and we display a MsgBox
Set OPCMyserver = Nothing
Set OPCMyBrowser = Nothing
MsgBox "Can't find OPC device", vbOKOnly + vbExclamation
Exit Sub
End If
ConnectFlag = True 'We are now connected
Connect.Caption = "Disconnect"
BrowseLevel = 0 'Initializing of the position in the OPC server
OPCMygroup.IsActive = False
Else
On Error Resume Next
OPCMygroup.IsActive = False
OPCMygroups.Remove OPCMygroup.ServerHandle
Set OPCMyitems = Nothing 'Delete Item collection
Set OPCMyitem = Nothing 'Delete Item object
Set OPCMygroups = Nothing 'Delete Group collection
Set OPCMygroup = Nothing 'Delete Group object
Set OPCMyBrowser = Nothing
OPCMyserver.Disconnect 'Diconnection
Set OPCMyserver = Nothing 'clear the OPC server object
ConnectFlag = False 'We are not connected anymore
Connect.Caption = "Connect to OPC Server"
Devicelist.Clear
OPCitems.ListItems.Clear 'clear the ListView
End If
ConnectError:
MsgBox "Error Connecting", vbOKOnly + vbExclamation
End Sub
Private Sub Devicelist_Click()
Dim vName As Variant
On Error GoTo ErrorOccured
If Left$(Devicelist.Text, 1) = "+" Then
vName = Mid$(Devicelist.Text, 2)
Else
vName = Devicelist.Text
End If
positie.Text = OPCMyBrowser.GetItemID(vName)
ErrorOccured:
End Sub
I know it's a bit much, but i'd rather added too much code then you guys need too ask that i add some pieces.
I hope you can help me it would be appreciated.
greetings Dadio25
Bookmarks