I need help with a SQL Connection Error in my VB6 app

Hi Experts,
Sometimes when users open up my VB6 application, they get the following error message whe the program loads:

The connection cannot be used to perform this operation. It is either closed or invalid in this context.

I have the users close and restart the application, and the problem seems to go away.  What May be causing this?  Thanks in advance for your help.

CODE THAT OPENS THE CONNECTION, AND GENERATES ERROR:

     Set clsSQL = New clsData 
    strUserView = clsSQL.ViewUser(User.UserID, 1)
    Call clsSQL.SQLDisconnect

Open in new window



DATA CLASS CODE, WITH FUNCTION THAT CAUSES ERROR
Public cn2 As New ADODB.Connection


Public Function SQLConn2(Optional strErrText As String) As ADODB.Connection
On Error GoTo SQLError

Dim strProv As String

cn2.Provider = "sqloledb"


strProv = "Server=ABCDATA\DEVDATA;Database=Sales;User Id=WebUser;Password=0U812USER"


cn2.Open strProv

Set SQLConnect2 = cn2

SQLExit:
    Exit Function
SQLError:
    strErrText = Trim$(strErrText & " " & Err.Description)
    Resume SQLExit
End Function


Public Function ViewUser(strID As String, intQuery As Integer) As String       '---  4QA - 08272013 - RR
On Error GoTo Initialize_Err
        Dim cn3 As ADODB.Connection
        Dim rs3 As ADODB.Recordset

        Set cn3 = SQLConnt2()
        Set rs3 = New ADODB.Recordset
        rs3.CursorLocation = adUseClient
        rs3.CursorType = adOpenStatic
        rs3.LockType = adLockBatchOptimistic
        rs3.ActiveConnection = cn3
        If intQuery = 1 Then
            rs3.Open ("SELECT status FROM tblUser WHERE UserID = '" & strID & "'")
        Else
            rs3.Open ("SELECT Name FROM tblUser WHERE UserID = '" & strID & "'")
        End If

        
         If Not rs3.BOF And Not rs3.EOF Then
            rs3.MoveFirst
            ViewUser = rs3.Fields(0).Value
        Else
            ViewUser = ""
            Set rs3 = Nothing
            Call SQLDisconnect
        End If
        
        Exit Function

Initialize_Err:
        MsgBox (Err.Description)
        Exit Function
End Function


Public Sub SQLDisconnect(Optional strErr As String)
On Error GoTo SQLError

If cn2.State = adStateOpen Then
    cn2.Close
    Set cn2 = Nothing
End If

SQLExit:
    Exit Sub
SQLError:
    strErr = Trim$(strErr & " " & Err.Description)
    Resume SQLExit
End Sub

Open in new window

mainrotorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
You may need to check if the connection is open before executing similar to what you do in:

If cn2.State = adStateOpen Then
    cn2.Close
    Set cn2 = Nothing
End If

but in reverse "If cn2.State <> adStateOpen Then cn2.Open End If"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.