ADO SQL Server, "Method 'Open' of object '_Connection' failed" - vb6

(Tearing my hair out, been doing this a million times for decades and now it doesn't work!) I have a simple VB6 program, connecting to SQL 2008 Express locally on the host machine. I can connect to the database using the same credentials in SQL Server Management Studio Express. However, when I run this code, I get the following error:
Run-time error '3706':
Method 'Open' of object '_Connection' failed
Dim DBConn As ADODB.Connection
Set DBConn = New ADODB.Connection
Dim ConnString As String
txtServer.Text = "R19DEV\SQLEXPRESS"
txtCatalog.Text = "MyDatabase"
txtUser.Text = "MyUser"
txtPassword.Text = "MyPassword"
ConnString = "Provider=SQLOLEDB.1;Persist Security Info=False;Initial Catalog=" & _
txtCatalog.Text & ";Data Source=" & txtServer.Text & ";User ID=" & txtUser.Text & _
";Password=" & txtPassword.Text
Debug.Print ConnString
DBConn.Open ConnString
Here's the connection string:
Provider=SQLOLEDB.1;Persist Security Info=False;Initial Catalog=MyDatabase;Data Source=R19DEV\SQLEXPRESS;User ID=MyUser;Password=MyPassword

OK, I'm not sure why this worked. I changed the connection provider from SQLOLEDB.1 to SQLOLEDB, and that fixed it.

Related

How to solve intermittent login problems with Oracle and VBA's ADODB

My environment is Windows 10, Office 2019 - 64 bit an Oracle Client 19.3.
I've had Office Macro's access Oracle data for years. Recently the Oracle server was upgraded from release 11.g to 18.c. The Macro's work on most PC's, but on a few I have intermittent problems logging in to Oracle. I'm wondering if it's because Oracle now has the concept of a "Container". In Oracle's SQL Plus it's called a "Service Name".
If I execute the code below using the UserID of "System" I get logged in, but can't see the tables in the container I need (XEPDB1). If I login with a UserID and Password that was created in the XEPDB1 container, I get an error telling me that I have an invalid UserID/Password combination.
Is there a way to use ADODB and specify a Container / Service Name as I log in so that I can avoid login errors?
Here's a snippet of the code that I'm using to login.
Option Explicit
Sub ReadFromOracle()
'DECLARE VARIABLES
Dim objConnection As ADODB.Connection
Dim objRecordset As ADODB.Recordset
Dim strConnection As String
Dim strSQL As String
Dim lngRecordCount As Long
On Error GoTo ErrorHandler
Set objConnection = New ADODB.Connection
Set objRecordset = New ADODB.Recordset
strConnection = "Provider=MSDASQL.1;" & _
"Persist Security Info=False;" & _
"Data Source=MyServer1;" & _
"UID=My_XEPDB1_UserID;" & _
"Password=My_XEPDB1_Password"
strConnection = "Provider=MSDASQL.1;" & _
"Persist Security Info=False;" & _
"Data Source=MyServer1;" & _
"UID=system;" & _
"Password=My_System_Password"
objConnection.ConnectionString = strConnection
'Open the database connection
objConnection.Open
strSQL = "Select title from LIB_BOOK where book_pk = 1;"
'Close objRecordset if it was open.
If CBool(objRecordset.State And adStateOpen) = True Then
objRecordset.Close
End If
objRecordset.CursorLocation = adUseClient
objRecordset.Open strSQL, objConnection
lngRecordCount = objRecordset.RecordCount
Exunt:
'Set objects to nothing
'Close objRecordSet
If CBool(objRecordset.State And adStateOpen) = True Then objRecordset.Close
Set objRecordset = Nothing
Set objConnection = Nothing
Exit Sub
ErrorHandler:
'Display the error message
'lngUpdateReturn = Err.Number
Select Case Err.Number
Case Is = -2147217843
MsgBox "Detailed Error Message for -2147217843.", vbCritical
Case Is = -2147467259
MsgBox "Detailed Error Message for -2147467259.", vbCritical
Case Else
MsgBox "Error Number : " & Err.Number & vbNewLine & vbNewLine & _
"Error Source: " & Err.Source & vbNewLine & vbNewLine & _
"Error Message : " & Err.Description & vbNewLine & vbNewLine, _
vbCritical
End Select
GoTo Exunt
End Sub
A subset of the TNSNAMES.ORA file is:
# tnsnames.ora Network Configuration File: C:\app\product\18.0.0\dbhomeXE\NETWORK\ADMIN\tnsnames.ora
# Generated by Oracle configuration tools.
MyServer1 =
(DESCRIPTION =
(ADDRESS =
(PROTOCOL = TCP)
(HOST = MyHost.MyDomain.com)
(PORT = 1521)
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = XEPDB1)
)
)

VB6 application ado connection for TLS1.2

I have to support a VB6 application that is still in production (ugh). A customer is specifying our software needs to be PCI compliant which requires TLS 1.2.
Anyone know how to do this?
I am using SQL Server 2014. I'm patched to build 12.0.4502.0.
Public Function GetConnection() As ADODB.Connection
Dim con As ADODB.Connection
On Error Resume Next
Set con = New ADODB.Connection
con.ConnectionTimeout = 10
Dim connstring As String
'connstring = "Provider=SQLOLEDB;Server=" & gstrServer & ";Database=" & gstrDB & ";User Id=" & gstrUser & ";Password=" & gstrPwd
connstring = "Provider=MSDASQL;DRIVER=Sql Server;Server=" & gstrServer & ";Database=" & gstrDB & ";UID=" & gstrUser & ";PWD=" & gstrPwd
con.Open connstring
If Err Then Set con = Nothing
Set GetConnection = con
End Function
The project is referencing "Microsoft ADO Ext. 6.0 for DDL and Security" and "Microsoft ActiveX Data Objects 2.5 Library"
I have tried multiple connection string options.
Thanks!
I found the answer in Using ADO with SQL Server Native Client.
To enable the usage of SQL Server Native Client, ADO applications will
need to implement the following keywords in their connection strings:
Provider=SQLNCLI11
DataTypeCompatibility=80
The following is an example of establishing an ADO connection string that is fully enabled to work
with SQL Server Native Client, including the enabling of the MARS
feature:
Dim con As New ADODB.Connection
con.ConnectionString = "Provider=SQLNCLI11;" _
& "Server=(local);" _
& "Database=AdventureWorks;" _
& "Integrated Security=SSPI;" _
& "DataTypeCompatibility=80;" _
& "MARS Connection=True;"
con.Open
Changing the provider to SQLNCLI11 and adding DataTypeCompatibility=80 worked.

Oracle Can connect from client but not from script - invalid user/password

I'm trying to connect to a Oracle DB and i believe the version is 12c, I'm able to connect to the same DB using a client (SQL Developer), but having issues while trying to connect using VBscript which I would like to use as part of my application.
Here is the error message --
**Exception: OraOLEDB - ORA-01017: invalid username/password; logon denied**
Here is my script that I'm using to connect --
Option Explicit
On Error Resume Next
'Dim strSQLQuery: strSQLQuery = "SELECT sysdate FROM dual"
Dim strDBDesc: strDBDesc = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST = XXX)(PORT = XXX))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = XXXX)))"
Dim strUserID: strUserID = "user"
Dim strPassword: strPassword = "pass"
Dim ADODBConnection: Set ADODBConnection = CreateObject("ADODB.Connection")
Dim strConnection
strConnection = "Provider=OraOLEDB.Oracle;Data Source=" & strDBDesc & _
";User ID=" & strUserID & ";Password=" & strPassword & ";"
ADODBConnection.Open strConnection
If Err <> 0 Then
WScript.Echo "An error occurred in Opening Connection: " & Err.Number & " " & Err.Description & " " & Err.Source
End If
Set ADODBConnection = Nothing
Is the password simple or with special character? use an escape sequence or quotes if with special characters.
Based on below use "ADODBConnection.ConnectionString = strConnection" in the above snippet and then open the connection.
Place break point and see what is being passed as a connection string to the driver.
Refer: https://docs.oracle.com/cd/E47955_01/win.121/e18594/using.htm
All,
i was able to connect successfully after i have download and installed the latest copy of Oracle ODAC from their website.
here are additional details - hope this helps others having a similar issue
https://blogs.msdn.microsoft.com/dbrowne/2013/10/02/creating-a-linked-server-for-oracle-in-64bit-sql-server/

QTP connection string for Sybase 16 database?

I can't seem to get my connection string correct.
Dim conn, rs
Set conn= createobject("adodb.connection")
set rs = createobject("adodb.recordset")
conn.Open "Provider=Sybase.ASEOLEDBProvider;Server Name = xxx.xxx.xxx.xxx,yyyy;User Id=user;Password=pwd;Database=mydatabase;"
rs.open "Select * from blah", conn
I keep getting an error stating, "Provider cannot be found. It may not be properly installed."
I know the provider is installed because I use the same computer for coding up C# applications that connect to Sybase 16 successfully all the time. Does anyone know the correct connection string for QTP/UFT?
I figured it out.
Dim conn, rs
Set conn= createobject("adodb.connection")
set rs = createobject("adodb.recordset")
conn.Open "Driver={Adaptive Server Enterprise}; Server=xxx.xxx.xxx.xxx; port=yyyy; uid=user; pwd=pwd; db=mydatabase;"
rs.open "Select * from blah", conn
Using this you'll be able to use QTP/UFT and connect to a sybase 16 database - so long as you have have the Sybase Adaptive Server Enterprise drivers installed on your system. It's a proprietary database and to my knowledge you'll have to either purchase drivers directly from Sybase or from a thrid party. If you're running Sybase 16 at your company you more than likely have the ASE drivers - ask around.
There is way to connect if it is in windows 7 Function
ConnectionTest()
DB_CONNECT_STRING = "Provider=OraOLEDB.Oracle; Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=" & myHostName & ")(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=" & myServiceNameOrSID & "))); User ID=" & myUsername & ";Password=" & myPassword & ";"
Set myConn = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
myConn.Open DB_CONNECT_STRING
objRecordSet.Open myQuery, myConn
Wscript.Echo objRecordSet.fields.item(1) & " " & objRecordSet.fields.item(2)
myConn.Close
End function
Call ConnectionTest()
if there is problem in mapping adodb driver need to invoke the exe file before running in windows 64 bit

Error in connecting with ms access database with password using VB6

Private Sub Form_Load()
Dim conString As String
conString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=D:\Dheeraj\VB6_DH\db5.mdb" _
& "Jet OLEDB:Database Password=dheeraj;" _
'& "App.Path & Persist Security Info=False;"
Set CON = New ADODB.Connection
With CON
.ConnectionString = conString
.Open
End With
End Sub
Hi,
here is the code to connect ms access database with password protection. However it is giving an error 'Could not use ";file already in use' can you please tell me what could be the issue.
Try to do this.
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Masterfile.mdb;Jet OLEDB:Database Password=xxxxx;"
Missing ; between .mdb and Jet
conString = "Provider=Microsoft.Jet.OLEDB.4.0" _
& ";Data Source=D:\Dheeraj\VB6_DH\db5.mdb" _
& ";Jet OLEDB:Database Password=dheeraj;"
This is a connection string I just pulled from on of my (working) project:
"Provider='Microsoft.Jet.OLEDB.4.0';Data Source='%path%\%file%'; Jet OLEDB:Database Password=%pwd%;"
So I guess the database source should be enclosed in a pair of single-quotes.
EDIT: Cross that, it shouldn't make any difference (as with the single quotes around the provider name). I never read your question until the end.
Close any programs currently having the database open and then delete any *.ldb file left in the same directory as the database. If you cannot delete the *.ldb file, then it means there's still a process running that has that file open. Hunt it down and kill it, then retry deleting the file.
You need to add two ; before Jet OLEDB
And try this code for connection
Public Con as New ADODB.Connection
Private Sub Form_Load()
Dim conString As String
conString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=D:\Dheeraj\VB6_DH\db5.mdb" _
& ";;Jet OLEDB:Database Password=dheeraj;"
Con.Open conString
End Sub
Dim Con As ADODB.Connection
Dim DatabasePath As String
Dim DatabasePassword As String
DatabasePath = App.Path & "\Storage.mdb"
DatabasePassword = "mypc"
Set Con = New ADODB.Connection
With Con
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Password='';" & _
"User ID=Admin;Data Source=" & DatabasePath & ";" & _
"Jet OLEDB:Database Password='" & DatabasePassword & "'"
.Open
End With

Resources