title: MS Excel SQL failed Connection to database error handle
body: <p>I have a problem with my project when there is no internet connection this message box appears  i tried the Error handle with special case number but my message box will appear after the below message which i don't like because it's contain my db information.
  <a href="https://i.stack.imgur.com/rklBf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rklBf.png" alt="enter image description here"></a></p>
  
  <pre class="lang-vb prettyprint-override"><code>Function GetTestConnectionString() As String
  
  '==================== ' Connection to SQl Server '==============
     GetTestConnectionString = OleDbConnectionString("servername", "db name", "user", "pass")
  '===============================================================
  
  End Function
  Function GetTestQuery() As String
  
  '==================== ' Get User table ' =======================
      GetTestQuery = "SELECT * FROM [dbname].dbo.Users"
      ' GetTestQuery = "EXEC dbo04.uspExcelTest"
  '===============================================================
  
  End Function
  '=====================================================
  Sub TestImportUsingQueryTable()
  
      Dim conString As String
      conString = GetTestConnectionString()
  
      Dim query As String
      query = GetTestQuery()
  
      Dim Target As Range
      Set Target = ThisWorkbook.Worksheets("AdminPanel2").Cells(10, 2)
      Select Case ImportSQLtoQueryTable(conString, query, Target)
          Case Else
      End Select
  End Sub
  '======================================================
  ' ===== QueryTable Functions =====
  Sub RefreshWorksheetQueryTables(ByVal ws As Worksheet)
  
      On Error Resume Next
  
      Dim qt As QueryTable
  
      For Each qt In ws.QueryTables
          qt.Refresh BackgroundQuery:=True
      Next
  
      Dim lo As ListObject
  
      For Each lo In ws.ListObjects
          lo.QueryTable.Refresh BackgroundQuery:=True
      Next
  
  End Sub
  '==================================================================================================================
  Function GetTopQueryTable(ByVal ws As Worksheet) As QueryTable
  
      On Error Resume Next
  
      Set GetTopQueryTable = Nothing
  
      Dim lastRow As Long
      lastRow = 0
  
      Dim qt As QueryTable
      For Each qt In ws.QueryTables
          If qt.ResultRange.row &gt; lastRow Then
              lastRow = qt.ResultRange.row
              Set GetTopQueryTable = qt
          End If
      Next
  
      Dim lo As ListObject
  
      For Each lo In ws.ListObjects
          If lo.SourceType = xlSrcQuery Then
              If lo.QueryTable.ResultRange.row &gt; lastRow Then
                  lastRow = lo.QueryTable.ResultRange.row
                  Set GetTopQueryTable = lo.QueryTable
              End If
          End If
      Next
  
  End Function
  '==================================================================================================================
  ' ===== Connection String Functions =====
  Function OleDbConnectionString(ByVal Server As String, ByVal Database As String, _
      ByVal username As String, ByVal Password As String) As String
  
      If username = "" Then
          OleDbConnectionString = "Provider=SQLOLEDB.1;Data Source=" &amp; Server _
              &amp; ";Initial Catalog=" &amp; Database _
              &amp; ";Integrated Security=SSPI;Persist Security Info=False;"
      Else
          OleDbConnectionString = "Provider=SQLOLEDB.1;Data Source=" &amp; Server _
              &amp; ";Initial Catalog=" &amp; Database _
              &amp; ";User ID=" &amp; username &amp; ";Password=" &amp; Password &amp; ";"
  
      End If
  
  End Function
  '==================================================================================================================
  Function OdbcConnectionString(ByVal Server As String, ByVal Database As String, _
      ByVal username As String, ByVal Password As String) As String
  
      If username = "" Then
          OdbcConnectionString = "Driver={SQL Server};Server=" &amp; Server _
              &amp; ";Trusted_Connection=Yes;Database=" &amp; Database
      Else
          OdbcConnectionString = "Driver={SQL Server};Server=" &amp; Server _
              &amp; ";UID=" &amp; username &amp; ";PWD=" &amp; Password &amp; ";Database=" &amp; Database
      End If
  
  End Function
  '==================================================================================================================
  Function StringToArray(Str As String) As Variant
  
      Const StrLen = 127
      Dim NumElems As Integer
      Dim Temp() As String
      Dim i As Integer
  
      NumElems = (Len(Str) / StrLen) + 1
      ReDim Temp(1 To NumElems) As String
  
      For i = 1 To NumElems
         Temp(i) = Mid(Str, ((i - 1) * StrLen) + 1, StrLen)
      Next i
  
      StringToArray = Temp
  
  End Function
  
  '==================================================================================================================
  ' ===== Import Using QueryTable =====
  Function ImportSQLtoQueryTable(ByVal conString As String, ByVal query As String, _
      ByVal Target As Range) As Integer
  
      On Error Resume Next
  
      Dim ws As Worksheet
      Set ws = Target.Worksheet
  
      Dim address As String
      address = Target.Cells(1, 1).address
  
      ' Procedure recreates ListObject or QueryTable
  
      If Not Target.ListObject Is Nothing Then     ' Created in Excel 2007 or higher
          Target.ListObject.Delete
      ElseIf Not Target.QueryTable Is Nothing Then ' Created in Excel 2003
          Target.QueryTable.ResultRange.Clear
          Target.QueryTable.Delete
      End If
  
      If Application.Version &gt;= "12.0" Then        ' Excel 2007 and higher
          With ws.ListObjects.Add(SourceType:=0, Source:=Array("OLEDB;" &amp; conString), _
              Destination:=Range(address))
  
              With .QueryTable
                  .CommandType = xlCmdSql
                  .CommandText = StringToArray(query)
                  .BackgroundQuery = True
                  .SavePassword = True
                  .Refresh BackgroundQuery:=False
              End With
          End With
      Else                                          ' Excel 2003
          With ws.QueryTables.Add(Connection:=Array("OLEDB;" &amp; conString), _
              Destination:=Range(address))
  
              .CommandType = xlCmdSql
              .CommandText = StringToArray(query)
              .BackgroundQuery = True
              .SavePassword = True
              .Refresh BackgroundQuery:=False
          End With
      End If
  
      ImportSQLtoQueryTable = 0
  
  
  End Function
  '==================================================================================================================
  '==================================================================================================================
  </code></pre>
  
  <p>This is the code i use in single module to retrieve the query table any idea where should i put the error handle if my internet was disconnect or disable the window of (SQL server login)?</p>