The solution I eventually settled on was to use nslookup.exe as a precursor to my sc.exe command. If the SQL Server server does not exist, nslookup tells me so immediately. Making this change cut down the time it took to fail on a SQL Server lookup from about 8 seconds to well under 1 second. The success case is actually slightly longer, but not noticeable. For those who may be interested, here is my final solution (hopefully the purpose of my personal functions [ShellWait, Qt, PassThru, LogError] will be obvious):
UPDATE: I've updated the function to incorporate dmaruca's clsRunApp (my new favorite class module) and the issue raised by Philippe concerning working in disconnected mode. The result is much better than I originally posted and I'd like to thank both of them for their contributions. Here's the function as it stands now:
Function SQLServerDBExists(ComputerName As String, DbName As String) As Boolean
Const LocalHost = "127.0.0.1"
Dim Result As String, RunApp As New clsRunApp
On Error GoTo Err_SQLServerDBExists
If ComputerName <> LocalHost And _
ComputerName <> "." And _
ComputerName <> Environ("COMPUTERNAME") Then
'Check for existence of the server using Name Server Lookup'
Result = RunApp.RunAppWait_CaptureOutput("nslookup " & ComputerName)
If InStr(Result, "Non-existent domain") Or _
InStr(Result, "Default servers are not available") Then
SQLServerDBExists = False
GoTo Exit_SQLServerDBExists
End If
End If
Result = RunApp.RunAppWait_CaptureOutput("sc \\" & ComputerName & " qdescription MSSQLSERVER")
If InStr(Result, "SUCCESS") Then
With PassThru("SELECT Name FROM sysdatabases " & _
"WHERE Name='" & DbName & "'", "master", ComputerName)
SQLServerDBExists = (Not .EOF)
End With
End If
Exit_SQLServerDBExists:
Exit Function
Err_SQLServerDBExists:
LogError Err.Number, Err.Description, "SQLServerDBExists", "AttachToSQL"
Resume Exit_SQLServerDBExists
End Function
Note: I realize Environ("COMPUTERNAME") is not a 100% reliable way of determining the computer's name, so feel free to replace that with your own code if you want. I think the lazy approach is sufficient for its purpose here.