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)
)
)
Related
When a SQL batch returns more than one message from e.g. print statements, then I can only retrieve the first one using the ADO connection's Errors collection. How do I get the rest of the messages?
If I run this script:
Option Explicit
Dim conn
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "SQLOLEDB"
conn.ConnectionString = "Data Source=(local);Integrated Security=SSPI;Initial Catalog=Master"
conn.Open
conn.Execute("print 'Foo'" & vbCrLf & "print 'Bar'" & vbCrLf & "raiserror ('xyz', 10, 127)")
Dim error
For Each error in conn.Errors
MsgBox error.Description
Next
Then I only get "Foo" back, never "Bar" or "xyz".
Is there a way to get the remaining messages?
I figured it out on my own.
This works:
Option Explicit
Dim conn
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "SQLOLEDB"
conn.ConnectionString = "Data Source=(local);Integrated Security=SSPI;Initial Catalog=Master"
conn.Open
Dim rs
Set rs = conn.Execute("print 'Foo'" & vbCrLf & "print 'Bar'" & vbCrLf & "raiserror ('xyz', 10, 127)")
Dim error
While not (rs is nothing)
For Each error in conn.Errors
MsgBox error.Description
Next
Set rs = rs.NextRecordSet
Wend
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/
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
Having a problem getting the above Error Message.
Can anyone assist please ?
Dim cn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim RS As New ADODB.Recordset
Dim RS2 As New ADODB.Recordset
Set cn = New ADODB.Connection
Call cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & TheServer &
FileTypeOld & ";" & "Jet OLEDB:Database Password=12345678;")
If cn.State = 0 Then cn.Open
Set cmd.ActiveConnection = cn
cmd.CommandText = "SELECT * FROM mov"
cmd.CommandType = adCmdTable
Set RS = cmd.Execute
The problem is you are using an SQL statement with a Command type adCmdTable. When using that command type, ADO will generate the select query internally, you are expected in this case to just specify the name of the table.
Alternatively, just remove the cmd.CommandType = adCmdTable line all together and keep the select * syntax.
You need ton add this character _ which is underscore at the end of this line
Just replace with this:
Call cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & TheServer & _
FileTypeOld & ";" & "Jet OLEDB:Database Password=12345678;")
or you can merge both lines into one line without underscore, just like this
Call cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & TheServer & ileTypeOld & ";" & "Jet OLEDB:Database Password=12345678;")
I'm trying to write a vb script that prompts a user for a schema attribute which I'll call bID and checks that the person with that bID is in active directory. I really have no idea how to get started, there are plenty of examples on how to query active directory users but I havent found a good one regarding checking against specific attributes. Any help/suggestions are greatly appreciated!
UPDATE:
ok heres my code so far, doesnt error out and returns 0, but I dont get a wscript.echo of the distinguished name for some reason. I included a few debugging wscript.echo's and it seems to never get into the while loop. Any ideas?
Option Explicit
GetUsers "CN=users,DC=example,DC=example,DC=example,DC=com","123456"
Function GetUsers(domainNc, ID)
Dim cnxn
Set cnxn = WScript.CreateObject("ADODB.Connection")
cnxn.Provider = "ADsDSOObject"
cnxn.Open "Active Directory Provider"
Dim cmd
Set cmd = WScript.CreateObject("ADODB.Command")
cmd.ActiveConnection = cnxn
cmd.CommandText = "<LDAP://" & domainNc & ">;(&(objectCategory=user)(objectClass=user) (employeeNumber=" & ID & "));distinguishedName;subtree"
WScript.Echo cmd.CommandText
cmd.Properties("Page Size") = 100
cmd.Properties("Timeout") = 30
cmd.Properties("Cache Results") = False
WScript.Echo "setting cmd.properties"
Dim rs
Set rs = cmd.Execute
WScript.Echo "rs object set"
While Not rs.eof
On Error Resume Next
WScript.Echo "while loop start"
Wscript.Echo rs.fields("distinguishedName".Value)
rs.MoveNext
If (Err.Number <> 0) Then
WScript.Echo vbCrLf& "Error # "& CStr(Err.Number)& " "& Err.Description
Else
On Error GoTo 0
End If
Wend
WScript.Echo "while loop end"
rs.close
WScript.Echo "rs object closed"
cnxn.Close
Set rs = Nothing
Set cmd = Nothing
Set cnxn = Nothing
End Function
Here's some vbscript that will find all users with bID=FooVal and write their DN out
Function GetUsers(domainNc, bIdVal)
Dim cnxn
Set cnxn = WScript.CreateObject("ADODB.Connection")
cnxn.Provider = "ADsDSOObject"
cnxn.Open "Active Directory Provider"
Dim cmd
Set cmd = WScript.CreateObject("ADODB.Command")
cmd.ActiveConnection = cnxn
cmd.CommandText = "<LDAP://" & domainNc & ">;(&(objectCass=user)(objectCategory=person)(bid=" & bidVal & "));distinguishedName;subtree"
cmd.Properties("Page Size") = 100
cmd.Properties("Timeout") = 30
cmd.Properties("Cache Results") = False
Dim rs
Set rs = cmd.Execute
While Not rs.eof
Wscript.Echo rs.fields("distinguishedName").Value
rs.MoveNext
Wend
rs.close
cnxn.Close
Set rs = Nothing
Set cmd = Nothing
Set cnxn = Nothing
End Function