How to connect Excel to Oracle database? - vba

I am trying to connect Excel to my Oracle database using VBA.
Sub dbConnect()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
strCon = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(description=(address=(protocol=tcp)(host=mydb.domain.com)(port=1522))(connect_data=(sid=mydb))); uid=user; pwd=pw;"
con.Open (strCon)
End Sub
I get an error.
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
I know from other questions and sources that the problem is most likely using the wrong version of the DSN.
However, I don't understand what I need to do to fix it.
My Windows is 64-bit, when I open the ODBC Data Source Administrator I see the following (among others):
Name: Excel Files, Platform: 64-bit, Driver: Microsoft Excel Driver
User DSN
Name: mydb, Platform: 32-bit, Driver: oracle in ORA121020_x86
under System DSN
What can I do to fix it? How do the connection string and the DNSs relate to each other? Should I change the version of one of the DNS and if yes how do I do that?
Edit:
The connection string is copied from the db connection in Oracel SQL Developer where I can access the db.

Ok I managed to fix it. My excel is 64 bit so in order to connect to the db, I had to create a System DNS for my connection that is also 64 bit. In order to do that, I had to go to the /Windows/system32 folder, choose the file odbcad32 and under system DNS add a new DNS with a 64 bit driver I had to download. Lets say I named that DNS abc
I then also changed the connection string that you can see in the above code to
strCon = "Data Source=abc;User=user;Password=pw"
According to this the connection string I used in the post above does not need a DNS, so I don't know why it didn't work, however after creating a new DNS as just described I switched to the new connection string that specifies a DNS.
And voila, after only several hours I was able to connect to my db.
Quick sidenote: As mentioned above, I edited the obcad32 file in /Windows/system32. This is the file for 64 bit DNS. There is a file with the exact same name in the folder /Windows/SysWOW64 that manages the 32 bit DNS, so if you have a similar problem as I had, pay attention to which file you edit.

Related

Connect Oracle SQL to VBA

I am able to connect Python to my Oracle SQL DB on a Windows computer, but strangely enough I cannot seem to get it working from VBA.
For reference, this is the Python Code to connect:
cx_Oracle.connect(f'{self.user}/{self.pwd}#//{self.host}:{self.port}/{self.service_name}')
I have tried replicating this similar structure in my VBA code, but I get an error message. Here is the code I've been using:
Sub CallDB_Return_Flexible(stSQL As String, rstStart As Range)
Dim sqlConn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim rtn As Boolean
Dim cmd As New ADODB.Command
Dim cs As String
'On Error Resume Next
Set sqlConn = New ADODB.Connection
cs = "Provider=OraOLEDB.Oracle;User ID=myuserID;Password=myPsswd;Data Source=host:port/service_name;"
sqlConn.ConnectionString = cs
sqlConn.CommandTimeout = 10000
sqlConn.Open
I get the following error messages:
1) The system cannot find message text for message text 0x80040e0c in the message file for OraOLEDB.
2) Sometimes, dependent on the iteration of the connection I use (IE a TNS connection), I get a TNS listener error message.
I currently have the ActiveX 6.1 Data Objects Library enabled. Furthermore, I am on a 64-bit machine with a 64-bit Oracle client installed. I have a 64-bit Office. I am quite certain I have a 64-bit ADO as well - my ODAC (if this is the same) is the same as the 64 bit version online. I have an ODP.NET of 2.12 and another with 4.121 in the registry. I just have an ODP.NET.Managed of 4.12.
This doesn't make much sense to me, how in one language I can connect with no issues, but in another I cannot!
I backtested using an earlier ActiveX Data Library - this solved it.
Thank you!!!!!

Link PostgreSQL to MS Access using ADO library via ODBC driver (DSN-less)

Hello :) I've searched Stackoverflow and Google for a lot of hours and would like to have a clear answer to my problem, because I got a little bit confused. I want to link a table from my PostgreSQL database to my MS Access frontend using VBA. Therefore I'm using the ADO library and installed the 32 bit ODBC driver for PostgreSQL. It works great if I use a connection string with a defined DSN, code looks like this:
Set conn = New ADODB.Connection
Dim strConnect As String
strConnect = "DSN=PostgreSQL35W;Database=...;UID=...;PWD=..."
conn.Open
But the thing is that I dont wan't to use a connection string with DSN, because I dont want to mess around with these DSNs, esspecially not in a distributed environment. Therefore I would like to use a DSN-less connection string. But it seems like that the ADO library doesn't support DSN-less connection strings while using the ODBC driver for PostgreSQL. Is that true?
Here's the code I used for the DSN-less connection:
Set conn = New ADODB.Connection
Dim strConnect As String
strConnect = "ODBC;Driver={PostgreSQL Unicode};Server=...;Port=5432;Database=...;UID=...;PWD=..;TABLE=...
I know there are a lot of threads about that topic, but I couldn't get a clear answer for it. So thank you very much in advance!

Accessing Oracle SQL Developer database from VBA

New to VBA and returning to SQL after 15 years. I'm am trying to use VBA to run SQL commands to download data into excel for further manipulation.
I have SQL Developer installed on my machine and I am able to access the database using the UI. I have figured out how to connect to the database and run a query within the SQL Developer interface, but honestly, I'm not an IT guy.
Any ideas how to do this? Even basic command line commands to connect to the database and run the query would be helpful.
I found this code on another site, but I'm having trouble getting my connection string to work. My macro errors out on the cnn.Open statement and says the Provider is not installed. I think it's because PROVIDER is set up for a different SQL Database type, but I can't seem to get the connection string to work.
I know my username and password, which I have successfully used to connect in the SQL Developer UI. I'm not sure what to put for remote IP address or database. Would that be the hostname and the SID in the connection properties dialog in SQL Developer? (The hostname looks more like a url without the http than an ip address. Not sure if hostname and ip address is an interchangeable term).
Sub Download_Reports()
'Initializes variables
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String
'Setup the connection string for accessing MS SQL database
'Make sure to change:
'1: PASSWORD
'2: USERNAME
'3: REMOTE_IP_ADDRESS
'4: DATABASE
ConnectionString = "Provider=ORAOLEDB.ORACLE;Password=PASSWORD;Persist Security Info=True;User ID=USERNAME;Data Source=REMOTE_IP_ADDRESS;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Use Encryption for Data=False;Tag with column collation when possible=False;Initial Catalog=DATABASE"
'Opens connection to the database
cnn.Open ConnectionString
'Timeout error in seconds for executing the entire query; this will run for 15 minutes before VBA timesout, but your database might timeout before this value
cnn.CommandTimeout = 900
'This is your actual MS SQL query that you need to run; you should check this query first using a more robust SQL editor (such as HeidiSQL) to ensure your query is valid
StrQuery = "SELECT TOP 10 * FROM tbl_table"
'Performs the actual query
rst.Open StrQuery, cnn
'Dumps all the results from the StrQuery into cell A2 of the first sheet in the active workbook
Sheets(1).Range("A2").CopyFromRecordset rst
End Sub
I'm needing help with my connection string. Can anyone help me with this?
I'm connecting to a database via Oracle SQL Developer. I am able to connect within the UI by providing the following items:
Connection Name - got it.
myUsername - got it.
myPassword - got it.
Connection Type = Basic, Role = default
myHostname - got it.
myPort - got it.
mySID - got it.
However, I am unable to get my connection string to work in VBA. When I run the script I get
"Run-time Error '3076'. Provider cannot be found.
It may not be properly installed" on the line beginning with cnn.open.

ADOBC Connection Fails on Windows 10 Machines Only

I'm fairly new to working with Access/VB (started 2 months ago) so bear with me.
I inherited a database that has an ADODB connection to Oracle that works perfect on the Windows 7 machines that it's been tested on (a total of 5), but gives the following error when tested on Windows 10 machines (total of 2). (All machines are running Access 2010).
Run-time Error '3709': The connection cannot be used to perform this operation. It is either closed or invalid in this context.
Here's the code:
Public Function PTMNConnect() As ADODB.Connection
Dim Cn As ADODB.Connection
Dim Conn As String
Conn = "DRIVER={Microsoft ODBC for Oracle};CONNECTSTRING=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST=host)(PORT=port))(CONNECT_DATA=(SERVICE_NAME=PROD)));uid=username;pwd=password"
Set Cn = New ADODB.Connection
With Cn
.ConnectionString = Conn
.CursorLocation = adUseServer
.Open '**Errors Out on Open**
End With
Set PTMNConnect = Cn
End Function
I've been racking my brain and searching everywhere for a possible solution to this for the past two days with no luck.
Here's what I've tried/verified so far based on other posts I'd seen:
Verified that the Oracle client was installed
Ensured that the Path variable contained the necessary paths
Verified the references and file paths are the same on all machines
Any insight or references that you may be able to provide would be greatly appreciated!
I was able to come up with a fix for this!
It turns out that Windows 7 and Windows 10 have different versions of the {Microsoft ODBC for Oracle} driver which I believe is what was causing the error. I decided to use the Oracle supplied driver (which remains the same for each PC) rather than the Microsoft supplied driver (which, as I found out, varies).
Replacing the Conn string from above with the following code fixed my issue. (This route requires an existing DSN):
Conn = "DRIVER={Oracle in OraClient11g_home1}; Dbq=DSN_TNSServiceName; uid=username; pwd=password"

Excel VBA Database connection error: Cannot open database '(unknown)'

I am trying to connect to Access Database using ADO DSN.
StrPath = Sheets("Sheet1").Range("DB_location")
strCon = "DSN=MS Access Database;DBQ=" & StrPath & ";"
Set con = New ADODB.Connection
con.Open strCon
I can connect to DB without any problem on my machine. However, when I tried running this exact macro on another PC, I ran into this issue on the last line:
I assumed it was due to Database Engine, but installing "Microsoft Access Database Engine 2010 Redistributable" from Microsoft's website didn't solve this problem. All the necessary references in VBA editor are present.
There is no problem with DB or the macro, as I checked on a different PC, but this specific computer runs into this problem.
Changing
strCon = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " & StrPath & ";"
solved this problem, which means the problem was somehow due to DSN. Does anyone have a guess about where does this problem might come from?
I had this error and it ended up being my primary keys. WHen i fixed my primary keys in the table it worked like a charm.