troubleshooting Question

Determine whether I have a valid connection to a SQL database

Avatar of Dale Fye
Dale FyeFlag for United States of America asked on
Microsoft AccessMicrosoft SQL Server
13 Comments2 Solutions143 ViewsLast Modified:
I have the following code which attempts to create a query and recordset to determine whether the parameters in a form for ServerName and dbName are valid:

strDriver = "SQL Server Native Client 11.0"
cnn = "ODBC;DRIVER=" & strDriver & ";SERVER=" & ServerName _
    & ";Trusted_Connection=Yes;APP=Microsoft Office 2003;WSID=" & ServerName _
    & ";DATABASE=" & DBName & "; "
Set db = CurrentDb
Set qdf = db.CreateQueryDef("")
qdf.Connect = cnn
qdf.ReturnsRecords = True
qdf.SQL = "SELECT top 1 dbo.SomeTableName. * from dbo.SomeTableName"
Set rs = qdf.OpenRecordset()
This code bombs out on the line that starts: SET rs =

With an error that reads:

 3151         ODBC--connection to 'SQL Server Native Client 11.0ServerName' failed.

But if I copy the connection string generated above:
ODBC;DRIVER=SQL Server Native Client 11.0;SERVER=ServerName;Trusted_Connection=Yes;APP=Microsoft Office 2003;WSID=ServerName;DATABASE=dbName;
and create a new pass-through query with identical SQL string, it returns the 1 record I'm looking for, so the connection is valid, but I keep getting this error message.

Any ideas what I might be doing wrong?
Éric Moreau
Senior .Net Consultant
Join our community to see this answer!
Unlock 2 Answers and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros