Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 136,562 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,894 people online right now. Registration is fast and FREE... Join Now!




Problems with closing program...

 
Reply to this topicStart new topic

Problems with closing program...

davkav
7 Apr, 2008 - 01:17 AM
Post #1

New D.I.C Head
*

Joined: 11 Mar, 2008
Posts: 35



Thanked: 1 times
My Contributions
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
User is offlineProfile CardPM
+Quote Post

davkav
RE: Problems With Closing Program...
7 Apr, 2008 - 05:20 AM
Post #2

New D.I.C Head
*

Joined: 11 Mar, 2008
Posts: 35



Thanked: 1 times
My Contributions
If someone doesnt answer me quick im going to pull a hissy fit!! smile.gif
User is offlineProfile CardPM
+Quote Post

davkav
RE: Problems With Closing Program...
7 Apr, 2008 - 07:24 AM
Post #3

New D.I.C Head
*

Joined: 11 Mar, 2008
Posts: 35



Thanked: 1 times
My Contributions
Doesnt matter figured it out.

Bloody hell you guys never reply!!! smile.gif

(kidding)
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 11:40PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month