Does VBA have a psycopg2 style way to connect to PostgreSQL? - excel

I have been running queries in SQL in Python using psycopg2. This is very simple and I just do the following:
conn2 = psycopg2.connect('postgresql://YourUserName:YourPassword#YourHost:5432/YourDatabase')
query = "xxx"
query_output = pd.read_sql_query(query, conn2).drop_duplicates(keep='first')
Is it possible to something similarly as easy in VBA? I am hoping to create an easy-to-use excel add-in so want to avoid asking the user to have to set up 32-bit drivers in ODBC Driver Manager.
I am trying to develop a setup here:
Dim objDb_con
Dim strSomeValue As String
Set objDb_con = CreateObject("ADODB.Connection")
Set Rsdatatype = CreateObject("ADODB.RecordSet")
glbConnString = "Driver={PostgreSQL UNICODE};Database=XX;port=XX;server=XX;UID=XX;Pwd=XX;"
objDb_con.Open glbConnString
But I get the error:
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Looking around it appears this is to do with ODBC driver managers and even though I tried to set-up the default as 32-bit I still get this error.
Is there a psycopg2-style alternative for VBA that is simple and doesn't require set-up for the user?

I always pull connection strings from this website:
https://www.connectionstrings.com/progress/
They clearly show that there is no built-in .NET libraries or OLE DB providers for PostGress. Thus, you cannot get around installing the driver. You could package an installer to automate this process.
If you have the ODBC drivers installed you would use a connection string like this:
Standard
Include only the parameters you want to override from the system DSN settings
DSN=myDSN;HOST=myServerAddress;DB=myDataBase;UID=myUsername;PWD=myPassword;PORT=2055;
Alternative using long names
Include only the parameters you want to override from the system DSN settings
DataSourceName=myDSN;HostName=myServerAddress;Database=myDataBase;LogonID=myUsername;Password=myPassword;PortNumber=2055;

Related

Impossible to connect to PostgreSQL DB via VBA

I have a problem trying to connect to my local PGSQL db via VBA. My code looks like this :
Set conn = CreateObject("ADODB.Connection")
strCnx = "Driver={PostgreSQL Unicode}; Server=127.0.0.1; Database=postgres;
UID=postgres;Pwd=***;port=5432"
conn.Open strCnx
The error I get is
[Microsoft][ODBC Driver Controler]DATA SOURCE NAME NOT FOUND AND NO
DEFAULT DRIVER SPECIFIED
I have tried a few things I found on stackoverflow, mainly installed the odbc drivers for postgresqlx64, and tried to connect to the database using directly ODBC, which told me success when I tried to connect (picture below).
Testing connection to PGSQL DB directly via ODBC
But despite everything I tried, the error is the same when I try to execute my VBA code, and I'm out of ideas, so if there are any way you could help me, I would be most thankful :)
Thanks
Frost
Make sure you install an ODBC driver with the same bitness as the application hosting your VBA. So 32-bits Excel means you need the 32-bits (x86) ODBC driver.
If you install the proper ODBC driver, this error should go away.

Trouble connecting to Sage 50 with Excel via ODBC

I'm trying to get into Sage 50 (v20 2014) via ODBC in Excel 2016 64bit. I keep running into connection problems.
I've set up a 64bit DSN using the utility on the Sage disk and I get the following...
If I go through 'Data/From other sources/From Microsoft Query' I get the usual 'Choose data source' box and the Sage DSN is visible. If I click it and click ok MS Query launches and then crashes immediately and I get 'Microsoft Query has stopped working'.
Interestingly, if I go back to the 'Choose Data Source' box, I get the error 'One or more of the folders in the search path could not be accessed'.
The default path set in the DSN is to 'C:\ProgramData\Sage\Accounts\2014\Company.000\ACCDATA' - the actual Sage database that I'm trying to access is in a network folder - 'V:\SHARED\Sage\COMPANY.002\ACCDATA'. I've tried both paths in the DSN and get the same results.
If I go through 'Data/New Query/From Other Sources/From ODBC' and then choose the Sage DSN, I get a full list of the tables but when I click on one of them to generate a preview, I get the following error...
DataSource.Error:ODBC:ERROR[42S02] Table not found
Details:
DataSourceKind=Odbc
DataSourcePath=dsn=SageLine50v20
OdbcErrors=Table
Also, if I use 'From data Connection Wizard', Excel crashes as soon as I choose the Sage DSN.
I'm not using any kind of connection string so I don't think it's authenticating with a user - I'm wondering if that would make a difference and if so, what would the connection string be?
I've been through everything that comes up if I google the problem and I've set up logins correctly etc but it will not connect. I don't know if this might be a 32/64bit comparability issue, but I'm using the 64bit Sage driver from the disk so I assumed that it wouldn't be a problem.
I've also tried connecting with MS PowerBI but I get the same problems.
Sage 50 is temperamental in how it allow connections to their database, what I mean: When it don't change the port, it just reject connections in awkward ways or not connect at all.
I didn't find a solution. However I found this workaround: Open sage, login and close it then connect using the DSN sage created with the sysadmin account(It grants you full access).
When defining the data path, it is critical to include a final backslash e.g
C:\ProgramData\Sage\Accounts\2018\COMPANY.001\ACCDATA\

Query excel based database from SQLDeveloper

I am on a Windows 7 machine and have configured a ODBC connection by name of 'MyExcelDb' to an excel file. I am able to programatically connect to 'MyExcelDb' using type-1 jdbc driver and everything is working fine.
I now want to use Oracle SQLDveloper to query the DSN 'MyExcelDb' but I am not able to make this connection as I do not find any option for ODBC Connection. I have tried using the 'advanced' option in Oracle section and tried putting custom jdbc URL as 'jdbc:odbc:excelDB' bugt to no avail.
I have tried adding entries in SQLDeveloper for third party drivers like:
- sourceforge.net/projects/xlsql/
- code.google.com/p/sqlsheet/
- hxtt.com/excel.html
Despite this but no new connection option would appear and I am still struggling to make connection between the two.
I have searched around and found that I could install Oracle and add a TNS for the excel and that way I may be able to connect using the Oracle TNS in SQLDeveloper. Installing Oracle database but would be like killing a mosquito using a canon.
I wonder if there is a simple solution to my problem.
Have you tried following Oracle's guidelines for connecting to Excel files? Did you set up a system DSN as described in step three of the Oracle guide. Importing files into the database is routine and simple but using SQL Developer in the manner you describe, without the client installed. Here is an example of reading an Excel file via PL/SQL.
My question is why use SQL Developer to manipulate a datastore in Excel when Excel is designed to manipulate the data?

OLE DB Provider for ODBC Drivers Error "80004005'

I have to move some customer sites from a very old IIS Server to a newer one, and some sites have troubles to work in the correct way. Most of them complain about a failure called:
Microsoft OLE DB Provider for ODBC Drivers error '80004005'
[Microsoft][ODBC Driver Manager]Data source name not found and no default driver specified.
I've read on the internet that this could depend on missing rights given to the user; other sites states that a Temp folder is missing (I can't imagine that this is right)… There are several other "solutions":
Open the rights for everyone on the server (as someone stated) is not an option for me. Also it is very painful to give explicit rights to every customer (there are several customers which needs the rights).
Do you know an easier solution, a similar way, or an alternative?
That error is nearly always caused by a bad connection string when an ADODB.connection object has its .open() method called.
For example, take the following code:
Dim SqlUsername : SqlUsername = "YOURSQLUSERNAME"
Dim SqlPassword : SqlPassword = "YOURSQLPASSWORD"
Dim ConnectionString : ConnectionString = "DRIVER={SQL Server};SERVER=YOURSERVERNAME;DATABASE=YOURDATABASENAME;UID=" & SqlUsername & ";PWD=" & SqlPassword
Dim db
Set db = Server.CreateObject("ADODB.Connection")
db.Open ConnectionString , SqlUsername , SqlPassword
Note how the connection string includes a driver identifier, in this example that is SQL Server.
Somewhere in your application you'll have an adodb.connection.open() method being called with a connection string, you need to find it, determine the driver being used and install it on your server.
Another thing to keep in mind, some data source drivers are 32bit and if your running your website in a 64bit application pool in IIS you'll need to allow 32bit objects - see this related question on that: Uploading picture after migration from IIS 6.0 to IIS 7.5
I have got the similar issue while working with classic asp and with IBM DB2 ODBC Driver. I do have two websites configured in my local IIS 7.5, Connection.Open is working fine with default web site whereas it does not with the second website, after several configuration changes as per my knowledge and as per sugesions from stackoverflow did not helped me in this case.
What worked for me is to enable (Set to True) the 32bit applications Advanced setting of ASP.NET V4.0 Classic Application Pool.
Application Pools-->ASP.NET V4.0 Classic-->
Advanced Settings--> under General Options double click Enable 32-bit Applications to set to True.
This small configuration may help some one who has the same issue.
Your old server has some ODBC DSN (Data Source Names) defined, and this is how your applications are connecting to the databases. You need to define these on your new server. Look in your server's Control Panel.
For sure now you have solved your problem but nevertheless for knowledge purposes here is what can work:
On top of what #webaware said please follow the steps below on your new server machine:
Go to Control Panel
Administrative Tools
Data Sources (ODBC)
Click System DSN tab
Click the Add button
Look for and select the Oracle in Client driver.
Now type in the Data Source Name, Description, TNS Service Name and User ID
Note: Ask your database administrator for the correct TNS Service name and User ID. You will also need the user id for testing your connection.
Click the Test Connection button
Type in your TNS Service name as the Service Name, User ID as the User Name and the password of the User ID
Click the Ok button
Your connection should be successful now.
I had the same problem after update Control Panel Plesk 12.5 to Plesk Onyx 17.5.3 and see updated ODBC Driver to 5.3.
To resolve the problem I changed ( 5.1 ) to ( 5.3 Unicode Driver ) in connection string.
Change this :
Conn.Open "DRIVER={MySQL ODBC 5.1 };SERVER=localhost; DATABASE=psa; UID=admin;PASSWORD=mypassword;Port=8306; OPTION=3"
To :
Conn.Open "DRIVER={MySQL ODBC **5.3 Unicode Driver**};SERVER=localhost; DATABASE=psa; UID=admin;PASSWORD=mypassword;Port=8306; OPTION=3"
Be sure to place the connection code at the top of the page vs bottom. That fixed my issue with this error.
The driver is not found because these drivers are not configured or registered in the system, I too got such an error when I run a asp page website in localhost. The same page was running smoothly in the host server.
To register, go to Microsoft ODBC Administrator -> SYSTEM DSN tab -> Add your driver by clicking 'configure' button. Hope this helps.
For me it was just a missing ";" after database name in connection string
This was not related to permissions. You are may be you defining database connections that explicit referenced a specific version of the mysql ODBC driver.
You will have to Update your connection strings.
i have same error when use asp classic script , MySQL and Plesk control Panel (windows)
You can Enable 32-bit applications in Plesk :
Go To Plesk Control Panel => Hosting & DNS => Dedicated IIS Application Pool for Website => Enable 32-bit applications

access Lotus notes database using ODBC and C#

Please provide some sample code how to access Lotus notes database using ODBC and C#.
Now I am able to accessing database suing NotesSession() class.but I want how to do via ODBC
Thanks and Regards
Naresh Mesineni
Thanks for your replay.Still I am facing some problem.I downloaded the NotesSQL 8.5 and installed.
I am trying to get the Notes data using ODBC still It is throwing some error.Here I am attaching connection string.
string sConnString = "Driver={Lotus NotesSQL 8.5 (32-bit) ODBC DRIVER (*.nsf)};" +
"Server=serverXXXX;" +
"database="DBName;"
"Uid=userxxx;" +
"Pwd=pwdxxx";
OdbcConnection odbccon = new OdbcConnection(sConnString);
odbccon.Open();
while opening connection it is throwing error
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"
Please provide some solution.it is help full if any one provide sample code with getting data from Notes.
Thanks and Regards
Naresh Mesienni
You'll need to use the NotesSQL driver, and the help file is here.
You can setup a DSN, or use the connection strings found on connectionstrings.com
Driver={Lotus NotesSQL 8.51 (32-bit) ODBC DRIVER (*.nsf)};Server=myServerAddress; Database=dbPath\myDb.nsf;Uid=myUsername;Pwd=myPassword;
How about LEI? (Lotus Enterprise Integrator)
I think is free with Domino and it lets you create ODBC connections and schedules jobs, etc.
You should look into it.

Resources