Sometimes we need a list of all the SQL Servers present on the network. There are lot of code snippets on the internet to do just that, however I did not find anything in the VBF Codebank.

So here is a function that will return an array f all the SQL Servers present in the network. In order to run this function you will have to set reference to Microsoft SQLDMO Object Library (Goto Projects-->References and select Microsoft SQLDMO Object Library)
VB Code:
  1. 'Function : listSQLServers
  2. 'Input Parameter : None
  3. 'Return Value:  Returns an Array that consists of the
  4. '               names of the SQLServers available
  5. Public Function listSQLServers() As String()
  6.    
  7.     'set reference to Microsoft SQLDMO Object Library
  8.     'prior to using this code
  9.     Dim sqlApp As New SQLDMO.Application
  10.     Dim serverNameList As SQLDMO.NameList
  11.     'an empty array that will hold all the servernames
  12.     Dim serverNames() As String
  13.     Dim loopCounter As Long
  14.            
  15.     'get all the SQL Servers available
  16.     Set serverNameList = sqlApp.ListAvailableSQLServers
  17.     'if no servers exist then return to the calling function
  18.     If serverNameList.Count = 0 Then
  19.         Set sqlApp = Nothing
  20.         Set serverNameList = Nothing
  21.         Exit Function
  22.     End If
  23.     'Redim the array with the proper count
  24.     ReDim serverNames(serverNameList.Count)
  25.        
  26.     'loop through all the servers
  27.     For loopCounter = 0 To serverNameList.Count
  28.         'add to the array
  29.         serverNames(loopCounter) = serverNameList.Item(loopCounter)
  30.     Next
  31.        
  32.     listSQLServers = serverNames
  33.        
  34.     'release objects
  35.     Set sqlApp = Nothing
  36.     Set serverNameList = Nothing
  37.     Exit Function
  38.  
  39. End Function