I have a problem I am hoping someone might be able to help me with or point me in the right direction. I have been developing a program that simulates an irrigation pivot panel for my job and I am running into a problem when I close the compiled program. When I compile the program and then run it, it seems to run OK, but when I go to close it and close the COM port I am using I get the error in the subject line. From what I have been able to find, it might be related to my form_load code, but I am honestly not sure. Here is my form_load code:

[Private Sub Form_Load()

Dim dtmTest As Date
Dim Dt As Date
Dim TimeCnt As Long
Dim relA As Single
Dim mMseAngle As Single
Dim Dir As String
Dim Strtstp As String
Dim SISStat As String
Dim Syswd As String 'System Wet/Dry
Dim Aux1 As String 'Aux 1
Dim Aux2 As String 'Aux 2
Dim i As Integer
Dim EGon As String 'End Gun on Monitor Variable
Dim SafeFault As String 'Safety fault monitor variable

Dim PwrFlt As String
Dim SISFlt As String
Dim PressFlt As String
Dim CmdFlt As String
Dim SftFlt As String
Dim RlyFlt As String
Dim DlyOpsFlt As String
Dim TempFlt As String

'dtmTest = Time.Now
Label9.Caption = Time
Label11.Caption = Date
lblStrtStp.Caption = "Stopped"
lblDir.Caption = "Forward"
lblWatStat.Caption = "Water.Off"
lblSISSet.Caption = "120"
lblSISStat.Caption = "Off"
txtkeyenter.Text = "80" 'Percent value
txtAppDepth.Text = "1.25" 'Depth Value
txtPosAngle.Text = "195" 'Default Degree Angle
txtPSI.Text = "190" 'Default PSI Setting
LblAux1.Caption = "Off" 'Default Aux 1 setting
lblAux2.Caption = "Off" 'Default Aux 1 setting
txtEGone.Text = "0-0" 'Set first end gun position
txtEGTwo.Text = "0-0"
Dir = "1" 'Forward
Strtstp = "00" 'Stopped
SISStat = "0" 'SIS Off
Syswd = "1" 'Status bit for "Dry"
Aux1 = "0"
Aux2 = "0"
EGon = "0"
SafeFault = "0"
PwrFlt = "OK"
SISFlt = "OK"
PressFlt = "OK"
CmdFlt = "OK"
'SftFlt = "OK"
RlyFlt = "OK"
DlyOpsFlt = "OK"
TempFlt = "OK"


txtStatBt1.Text = Dir & SISStat & Strtstp 'Status Bit 1 Testing Hex conversion
txtStatbt2.Text = Aux1 & "00" & Syswd
txtStatbt3.Text = Aux2 & "000"
txtStatbt4.Text = "0" & SafeFault & "0" & EGon
txtStatbt5.Text = "0000"
txtStatbt6.Text = "0000"

cboComPort.AddItem "COM1"
cboComPort.AddItem "COM2"
cboComPort.AddItem "COM3"
cboComPort.AddItem "COM4"
cboComPort.AddItem "COM5"
cboComPort.AddItem "COM6"
cboComPort.AddItem "COM7"
cboComPort.AddItem "COM8"
cboComPort.AddItem "COM9"
cboComPort.AddItem "COM10"
cboComPort.ListIndex = Valleypanel.SI_COMM1.PortNumber
Valleypanel.SI_COMM1.BaudRate = Baud9600

'show the splash screen
frmSplash.Show

'wait for them to see the
' splash screen

TimeCnt = Timer + 2
Do
DoEvents

'incase of mid night.
If TimeCnt - Timer > 2 Then TimeCnt = Timer + 2

Loop While TimeCnt > Timer


'hide the splash screen
frmSplash.Hide

Call MainKypdVis
Call TENKeyON

lblCOMNote.Visible = True

Set MyNumber = New AnyNumBase 'To be used to convert the Status bytes]

I do have a timer that I start once the user selects a COMport, and within the timer I am evaluating variables for proper alignment, etc. This program also communicates with our product to change the defined variables within the program. Is there a way to clear memory to prevent this from happening? Am I declaring too many variables? Not quite sure how I should go about fixing this error. Thanks.

Okay, when you go to close the form, you need to some cleaning up of your objects that you have created/initialized. The timer for one would be good to disable with a timer1.enabled = false and you should close your com port in the proper way. You can do this cleanup in either query unload or in unload.

Good Luck

As per vb5prgrmr, you are probably receiving requests from your winsockets/ports. This is mainly because they are still open and the timer event is triggered while you are ending the app. Try the following:

You can close your winsockets by using something like -

Private Sub socket_Close(index As Integer)
    'close a particular connection and decrease the number of concurrent connections
    sServerMsg = "Connection closed: " & Socket(index).RemoteHostIP
    Socket(index).Close
    Unload Socket(index)
    iSockets = iSockets - 1
    lblConnections.Caption = iSockets
    
End Sub

Disable the timer to stop listening for new data/requests -

Timer1.Enabled = False
This article has been dead for over six months. Start a new discussion instead.