title: the connection cannt be used to perform this operation. It may closed or not valid in this context error in vb6
body: <p>I am trying to execute the query which stores recordset vales in sql db. when I am trying to execute that i am getting error like </p>
  
  <p>the connection cannt be used to perform this operation. It may closed or not valid in this context  error in vb6. Please help me to solve this issue.</p>
  
  <pre><code>' Write records to Database
  
      frmDNELoad.lblStatus.Caption = "Loading data into database......"
      Call FindServerConnection_NoMsg
  
      Dim lngRecCount As Long
      lngRecCount = 0
      rcdDNE.MoveFirst
  
      Set rcdReclamation = New ADODB.Recordset
      With rcdReclamation
          .ActiveConnection = objConn
          .Source = "insert into t_DATA_DneFrc (RTN, AccountNbr, FirstName, MiddleName, LastName, Amount) values ('" &amp; rcdDNE("RTN") &amp; "', '" &amp; rcdDNE("AccountNbr") &amp; "', '" &amp; rcdDNE("FirstName") &amp; "', '" &amp; rcdDNE("MiddleName") &amp; "', '" &amp; rcdDNE("LastName") &amp; "', '" &amp; rcdDNE("Amount") &amp; "')"
          .CursorType = adOpenDynamic
          .CursorLocation = adUseClient
          .LockType = adLockOptimistic
          .Open cmdCommand
      End With
  
      Do Until rcdDNE.EOF
          lngRecCount = lngRecCount + 1
          frmDNELoad.lblStatus.Caption = "Adding record " &amp; lngRecCount &amp; " of " &amp; rcdDNE.RecordCount &amp; " to database."
          frmDNELoad.Refresh
          DoEvents
          Call CommitNew
          rcdDNE.MoveNext
      Loop
  
      frmDNELoad.lblStatus.Caption = "DNE Processing Complete."
      frmDNELoad.Refresh
  
  End Function
  
  Sub CommitNew()
     ' Add records to DneFrc table
      With rcdReclamation
          .Requery
          .AddNew
          .Fields![RTN] = rcdDNE.Fields![RTN]
          .Fields![AccountNbr] = rcdDNE.Fields![AccountNbr]
          .Fields![FirstName] = rcdDNE.Fields![FirstName]
          .Fields![MiddleName] = rcdDNE.Fields![MiddleName]
          .Fields![LastName] = rcdDNE.Fields![LastName]
          .Fields![Amount] = rcdDNE.Fields![Amount]
          .Update
  
      End With
  
  End Sub
  </code></pre>
  
  <p>conection  code</p>
  
  <pre>
  Sub InstantiateCommand_SQLText()
      ' Creates a command object to be used when executing SQL statements.
      Set objCommSQLText = New ADODB.Command
      objCommSQLText.ActiveConnection = objConn
      objCommSQLText.CommandType = adCmdText
  End Sub
  
  Function FindServerConnection_NoMsg() As String
  
      Dim rcdClientPaths As ADODB.Recordset
      Dim strDBTemp As String
      Const CLIENT_UPDATE_DIR = "\\PSGSPHX02\NORS\Rs\ClientUpdate\"
  
      On Error Resume Next
      ' If persisted recordset is not there, try and copy one down from
      ' CLIENT_UPDATE_DIR.  If that can't be found, create a blank one
      ' and ask the user for the server name.
      Set rcdClientPaths = New ADODB.Recordset
      ' Does it already exist locally?
      If FileExists_FullPath(App.Path & "\" & "t_PCD_ServerConnectionList.xml") = False Then
          ' Can it be retrieved from CLIENT_UPDATE_DIR
          If Dir(CLIENT_UPDATE_DIR & "t_PCD_ServerConnectionList.xml")  "" Then
              FileCopy CLIENT_UPDATE_DIR & "t_PCD_ServerConnectionList.xml", App.Path & "\" & "t_PCD_ServerConnectionList.xml"
          Else
              ' Creat a blank one.
              With rcdClientPaths
                  .Fields.Append "ServerConnection", adVarChar, 250
                  .Fields.Append "Description", adVarChar, 50
                  .CursorType = adOpenDynamic
                  .LockType = adLockOptimistic
                  .CursorLocation = adUseClient
                  .Open
                  .Save App.Path & "\" & "t_PCD_ServerConnectionList.xml", adPersistXML
                  .Close
              End With
          End If
      End If
  
      ' Open the recordset
      With rcdClientPaths
          .CursorType = adOpenDynamic
          .LockType = adLockOptimistic
          .CursorLocation = adUseClient
          .Open App.Path & "\" & "t_PCD_ServerConnectionList.xml", , , , adCmdFile
      End With
  
      If rcdClientPaths.RecordCount  0 Then
          ' try each one listed
          rcdClientPaths.MoveFirst
          Do Until rcdClientPaths.EOF
              strDBTemp = TryConnection_NoMsg(rcdClientPaths.Fields![serverconnection])
              If strDBTemp  "" Then
                  FindServerConnection_NoMsg = strDBTemp
                  Exit Function
              End If
              rcdClientPaths.MoveNext
          Loop
          strDBTemp = ""
      End If
  
      Do While strDBTemp = ""
          If strDBTemp  "" Then
              strDBTemp = TryConnection_NoMsg(strDBTemp)
              If strDBTemp  "" Then
                  With rcdClientPaths
                      .AddNew
                      .Fields![serverconnection] = strDBTemp
                      .Update
                      .Save
                  End With
                  FindServerConnection_NoMsg = strDBTemp
                  Exit Function
              End If
          Else
              Exit Function
          End If
      Loop
  End Function
  
  Function TryConnection_NoMsg(ByVal SvName As String) As String
      On Error GoTo ErrHandle
      ' If a server was provided, try to open a connection to it.
      Screen.MousePointer = vbHourglass
      Set objConn = New ADODB.Connection
      With objConn
          .CommandTimeout = 30
          .ConnectionTimeout = 30
          .ConnectionString = "Provider=SQLOLEDB.1; Server=" & SvName & "; User ID=RS_Auth; Password=weLcomers_auth; Initial Catalog=NORS" ' Test
          .Open
          .Close
      End With
      Set objConn = Nothing
      TryConnection_NoMsg = SvName
      Screen.MousePointer = vbNormal
      Exit Function
  
  ErrHandle:
      TryConnection_NoMsg = ""
      Set objConn = Nothing
      Screen.MousePointer = vbNormal
      Exit Function
  
  End Function
  </pre>