Solved

How to check if local sql server exist or any other sql server exist by passing the name?

Posted on 2006-06-27
2
208 Views
Last Modified: 2010-04-07
Need VB code to check whether a sql server with certain name exist or not. Please help!
0
Comment
Question by:Skaugen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Expert Comment

by:jkaios
ID: 16997873
Have you tried the Microsoft SQL DMO object library.
It has dozens of exposed properties and methods including functions to
list all available SQL Servers on your network.

If you install SQL Server (just the client tools) on your machine then it will be
available in the References list.
0
 
LVL 1

Accepted Solution

by:
maonas earned 500 total points
ID: 17000566
Here is the code; where strMachineName is the name of the SQL Server that you want to check.

Dim objWMIService As Object
Dim colItems As Object
Dim objItem As Object
Dim strMachineName As String

strMachineName = "localhost"

On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\" & strMachineName & "\root\cimv2")
If Err.Number Then
    ' Unable to connect to server
End If

Dim strQuery As String
strQuery = "SELECT * FROM Win32_Service WHERE Name = 'MSSQLServer'"
Set colItems = objWMIService.ExecQuery(strQuery, , wbemFlagForwardOnly Or wbemFlagReturnImmediately)
If Err.Number Then
    ' Unable to get SQL Server status
Else
    If colItems.Count Then
        For Each objItem In colItems
            Debug.Print "Status: " & objItem.Status
            Debug.Print "State: " & objItem.State
        Next
    Else
       'MSSQLServer service not running on strMachineName
    End If
End If
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question