Why the Connection Fails
The error on line 9 usually indicates that the provider name or connection string syntax is incorrect. VBScript expects a valid OLE DB provider and properly quoted values. Missing or mis‑typed parameters cause the ADODB.Connection.Open call to throw an exception.
Constructing a Valid Connection String
Use the OraOLEDB provider, separate each parameter with a semicolon, and avoid unnecessary quotes around the provider name. The Data Source should be the TNS alias or EZCONNECT string, and Persist Security Info can be omitted if not needed.
Provider=OraOLEDB.Oracle;User ID=user;Password=pwd;Data Source=source;Persist Security Info=False;
Simplified VBScript Example
This script opens the connection, runs a query, and displays the result in a message box. It includes error handling to catch connection issues.
Dim conn, rs, sql, msg
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = "Provider=OraOLEDB.Oracle;User ID=user;Password=pwd;Data Source=source;Persist Security Info=False;"
On Error Resume Next
conn.Open
If Err.Number <> 0 Then
MsgBox "Connection failed: " & Err.Description
WScript.Quit
End If
Set rs = CreateObject("ADODB.Recordset")
sql = "SELECT COUNT(DISTINCT party_id) FROM apps.per_all_people_f"
rs.Open sql, conn
If Not rs.EOF Then
msg = rs.Fields(0).Value
MsgBox msg
End If
rs.Close
conn.Close
Handling Recordset Properly
Always check for EOF before accessing fields. Use rs.EOF and rs.BOF to ensure the recordset contains data. Closing the recordset and connection in a Finally block guarantees resources are freed even if an error occurs.
Running the Script
Save the code as .vbs and execute from the command line: `cscript script.vbs`. The script will pop up a message box with the count. For headless environments, replace MsgBox with `WScript.Echo`.
Takeaway: A correctly formatted connection string and proper error handling turn a failing VBScript into a reliable Oracle data extractor.
People also ask
What if I need to use a different Oracle provider?
Replace `OraOLEDB.Oracle` with the provider name, e.g., `MSDAORA` or `OraClient11g`. Ensure the provider is installed on the machine.
How can I avoid hard‑coding credentials?
Use Windows authentication with `Integrated Security=SSPI` or read credentials from a secure file or environment variable.
Why does my script hang after opening the connection?
A missing or wrong Data Source can cause a timeout. Verify the TNS alias or EZCONNECT string in tnsnames.ora or the Oracle listener configuration.
Inspired by a public discussion on Stack Overflow. This article is an original explanation for learners.