Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 585489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:00:30+00:00 2026-05-13T15:00:30+00:00

I have an MS Access program in use in multiple locations. It connects to

  • 0

I have an MS Access program in use in multiple locations. It connects to MS SQL Server tables, but the server name is different in each location. I am looking for the fastest way to test for the existence of a server. The code I am currently using looks like this:

ShellWait "sc \\" & ServerName & " qdescription MSSQLSERVER > " & Qt(fn)
FNum = FreeFile()
Open fn For Input As #FNum
Line Input #FNum, Result
Close #FNum
Kill fn

If InStr(Result, "SUCCESS") Then ...

ShellWait: executes a shell command and waits for it to finish
Qt: wraps a string in double quotes
fn: temporary filename variable

I run the above code against a list of server names (of which only one is normally available). The code takes about one second if the server is available and takes about 8 seconds for each server that is unavailable. I’d like to get both of these lower, if possible, but especially the fail case as this one happens most often.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:00:30+00:00Added an answer on May 13, 2026 at 3:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 373k
  • Answers 373k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If your example should have said 70 in the first… May 14, 2026 at 7:30 pm
  • Editorial Team
    Editorial Team added an answer Here's one approach I've used that may seem inefficient but… May 14, 2026 at 7:30 pm
  • Editorial Team
    Editorial Team added an answer The simple way to run two queries and combine the… May 14, 2026 at 7:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.