Ok guys, dont mean to keep hassling peeps. Just this has been wrecking my head for a while now. Ive got everything working that i want working except for when i try to close my program I get an error.
Im using a connection to Kepware Server using OPC driver. Not sure if anyone is familiar with it but the problem isnt directly related to it. I think its more of a coding error. Ive emailed Kepware about it but of course they havent replied. (2 weeks ago) I downloaded one of there demo Projects aswell and it halts on the same error.
Any help would be mucho appreciated. Theres a good bit of code, so i understand if you havent got the time to have a gander
Ill break it up so its easier to read
CODE
Option Base 1
Dim WithEvents ConnectedOPCServer As OPCServer
Dim ConnectedServerGroups As OPCGroups
Dim WithEvents ConnectedGroup As OPCGroup
Dim OPCItemCollection As OPCItems
Dim ItemCount As Long
Dim OPCItemIDs(6) As String
Dim ItemServerHandles() As Long
Dim ItemServerErrors() As Long
Dim ClientHandles(6) As Long
Private Sub Form_Load()
'connect to OPC Server
Set ConnectedOPCServer = New OPCServer
ConnectedOPCServer.Connect "Kepware.KepserverEx.V4"
Set ConnectedServerGroups = ConnectedOPCServer.OPCGroups
Set ConnectedGroup = ConnectedServerGroups.Add("Test")
ConnectedGroup.UpdateRate = 50
ConnectedGroup.IsSubscribed = True
Dim i As Integer
'load data into arrays
ItemCount = 6
For i = 1 To ItemCount
OPCItemIDs(i) = "Channel1.Device2.PROGRAM:MAINPROGRAM.TIMER" & i & ".ACC"
ClientHandles(i) = i
Next i
OPCItemIDs(3) = "Channel1.Device2.PROGRAM:MAINPROGRAM.Switch1"
OPCItemIDs(4) = "Channel1.Device2.PROGRAM:MAINPROGRAM.Switch2"
OPCItemIDs(5) = "Channel1.Device2.PROGRAM:MAINPROGRAM.Timer1.pre"
OPCItemIDs(6) = "Channel1.Device2.PROGRAM:MAINPROGRAM.Timer2.pre"
Set OPCItemCollection = ConnectedGroup.OPCItems
OPCItemCollection.DefaultIsActive = True
OPCItemCollection.AddItems ItemCount, OPCItemIDs, ClientHandles, ItemServerHandles, ItemServerErrors
End Sub
CODE
Sub ConnectedGroup_DataChange(ByVal TransactionID As Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() As Long, TimeStamps() As Date)
Dim x As Integer
Dim arrportfolio(1) As Long
'Place Values Taken from timer data into respective text boxes
For x = 1 To NumItems
MyText(ClientHandles(x)).Text = ItemValues(x)
Next x
'if boolean value of TIMER.EN display On else display off
If MyText(3) = "1" Then
Text3.Text = "ON"
Else
Text3.Text = "OFF"
End If
'if boolean value of TIMER.EN display On else display off
If MyText(4) = "1" Then
Text2.Text = "ON"
Else
Text2.Text = "OFF"
End If
End Sub
CODE
Sub Remove_Items()
Dim RemoveItemServerHandles(6) As Long
Dim RemoveItemServerErrors() As Long
'When you unlodad or close the form it is time to remove the Items, Group, and Server Connection
'Remove the items
RemoveItemServerHandles(6) = ItemServerHandles(6)
OPCItemCollection.Remove 6, RemoveItemServerHandles, RemoveItemServerErrors
'Release the item interface and all the resources to be freed
Set OPCItemCollection = Nothing
End Sub
Sub Remove_Group()
'Remove the group
ConnectedServerGroups.Remove ("Test")
' Release the group interface and allow the server to cleanup the resources used
Set ConnectedGroup = Nothing
Set ConnectedServerGroups = Nothing
End Sub
Sub Disconnect_Server()
'Remove/Disconnect the server connecton
ConnectedOPCServer.Disconnect
' Release the old instance of the OPC Server object and allow the resources to be freed
Set ConnectedOPCServer = Nothing
End Sub
CODE
Private Sub ExitExample_Click() 'When you unlodad or close the form it is time to remove the Items, Group, and Server Connection
Call Remove_Items
Call Remove_Group
Call Disconnect_Server
End Sub
Private Sub Form_Unload(Cancel As Integer) 'When you unlodad or close the form it is time to remove the Items, Group, and Server Connection
Call Remove_Items
Call Remove_Group
Call Disconnect_Server
End
End Sub
The program halts on this line "OPCItemCollection.Remove 6, RemoveItemServerHandles, RemoveItemServerErrors" in Remove items with runtime error '5' invalid procedure call or argument....
This post has been edited by davkav: 7 Apr, 2008 - 01:18 AM