Log in

View Full Version : vba code to test odbc connection



gdmvet
02-16-2008, 04:27 AM
How can i test an ODBC connection in VBA code.
I use DAO recordsets.
I know this question is already been raised on this forum, but it's never been answered.
I'd like to write a function like TestConnection = true or false or something
The connection string is like :
"ODBC;DATABASE=namedatabase;DESCRIPTION=x;DSN=x;OPTION=2346;PWD=x;PORT=0;SER VER=x;UID=x"

Any help would be appreciated alot

stanl
02-16-2008, 07:54 AM
DAO probably has an Errors collection, but IMHO ADO is probably a better route; and I would think you would want to emply DNS-less Connection Strings. Anyway, check out the errors collection here.


http://www.w3schools.com/ado/ado_ref_connection.asp

.02 Stan

gdmvet
02-16-2008, 09:51 AM
Hi Stan,

Thanx for the reply. I've used your ADO-way. This is the code that
works for me.

Dim con
Set con = CreateObject("ADODB.Connection")
con.Open CONNSTRINGS
con.Close
Set con = Nothing

Where CONNSTRINGS is my connection string.

If the server is not online, my error trap gives the "no connection" message.

Thanks alot.

I'm new to ODBC connection. I'm gonna check out the differences between DSN and DSNless connections, because that's all a bit of Chinese for me right now ;)