VBScript test ODBC connection

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

VBScript test ODBC connection

Post by pgriffin » Wed Aug 02, 2006 9:38 pm

I need to take the ODBC name, table name from a text string and do nothing more than test that the ODBC connection is there and the table name is valid.

I'm searching EVERYWHERE. I am using MacroScript to call the VBScript and to take in the text string...

Anyone have some VBScript they are willing share? I may resort to opening the database (good or bad) and then trying to GetNextRecord....

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Aug 03, 2006 7:41 am

You could do this:

Code: Select all

VBSTART

Function TestODBC

On Error Resume Next

    TestODBC = 0
    Set DB = CreateObject("ADODB.Connection")
    DB.Open "Provider = MicroSoft.JET.OLEDB.4.0; Data Source=c:\DB\DB.mdb;"
    If Err then
        MsgBox "Error: " & Err.Number & " - " & Err.Description
        TestODBC = 1
    Else
        SQL = "Select * FROM MyTable"
        Set RS = DB.Execute(SQL)
        If Err then
            MsgBox "Error: " & Err.Number & " - " & Err.Description
            TestODBC = 2
        End If
    End if

End Function

VBEND

VBEval>TestODBC,r
You could be more specific by querying the value of Err.Number or Err.Description. You can probably find a list of error codes somewhere.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Thu Aug 03, 2006 1:34 pm

Thanks Marcus,

I will modify the code to send the odbc name and table name to the VBScript routine, then I'll repost in case anyone else is interested.

btw, I always use this link for finding error codes...

http://msdn.microsoft.com/library/defau ... 0-499_.asp

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Thu Aug 03, 2006 2:49 pm

Ok, here is my final version. If anyone wants to use this and have the function return the actual error number, just enable the "TestODBC = err.number" lines.

Of course, you could get very advanced here and select specific column names, etc...but my intention is only to tell the user that the connection information they entered works or does not work.

Code: Select all

VBSTART

Function TestODBC(ODBCname,TableName)
dim SQLString
dim DB
dim RS

On Error Resume Next

    TestODBC = 0
    Set DB = CreateObject("ADODB.Connection")
	on error resume next
    DB.Open "DSN=" & ODBCname
    If Err then
        TestODBC = 1
		'TestODBC = err.number
    Else
        SQLString = "Select * from " & TableName
		on error resume next
        Set RS = DB.Execute(SQLString)
        If Err then
            TestODBC = 2
			'TestODBC = err.number
        End If
    End if

End Function

VBEND
VBEval>TestODBC("%ODBCname%","%TableName%"),dbresult
label>end

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts