Can't connect to Access DB, neither with OleDB, DAO, etc - vba

I want to read and write data from a CATIA macro to and from an Access DataBase. I've got Windows 10 and Office 2013 on it (64bit Windows).
Unfortunately I can't connect to that Access DataBase from VBA. (From VB.NET works fine)
I tried it all:
Various connection strings (JET.4.0, ACE.12.0) etc with ADODB
-> Error that Provider cant be found
Connection via DAO
-> Various other errors
Any idea why I cant connect?
I referenced all DLLs possible to reference, etc.
Only idea I have that theres a problem with Access 2013 32bit and the 64 bit Windows?

I tried from Excel with the following code (source), it works like a charm.
Sub test()
Dim cnn As ADODB.Connection 'Requieres reference to the Microsoft
Dim rs As ADODB.Recordset 'ActiveX Data Objects Library
Set cnn = CreateObject("adodb.Connection")
cnn.Open "DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\someFolder\myDb.accdb;"
Set rs = cnn.Execute("SELECT * FROM versions")
While Not rs.EOF
Debug.Print rs(1), rs(2), rs(3)
rs.MoveNext
Wend
rs.Close
End Sub
if that doesn't work, the client PC may lack a piece of software ?

Related

Access 2007 Error 3151 from DAO.OpenRecordSet. How to fix?

Well this is fascinating. I am using MS-Access 2007 to make a from that will allow users to enter data from lab results.
The from utilizes a VBA code call:
DAO.OpenRecordset("{Sql query}" , DB_OPEN_DYNASET, dbSeeChanges)
This call normally works just fine, but I've noticed that when this form is closed and then re-opened, a 3151 ODBC error is generated.
We are using linked tables through Access 2007 for this form, our SQL-Server 2012 version is as follows.
Microsoft SQL Server Management Studio 11.0.2100.60
Microsoft Analysis Services Client Tools 11.0.2100.60
Microsoft Data Access Components (MDAC) 6.1.7601.17514
Microsoft MSXML 3.0 6.0
Microsoft Internet Explorer 8.0.7601.17514
Microsoft .NET Framework 4.0.30319.237
Operating System 6.1.7601
The DAO.OpenRecordset call is utilized in multiple events. Here is the list.
On_Current , _AfterUpdate , Form_Unload
I have found several reported instances of similar errors on Access 2007 with linked tables to SQL-Server, but none seem to have workable solutions for this issue.
As best I can tell, the 3151 doesn't appear to be affecting the form or data in any negative way. I am able to open the form and everything is where it should be as best I can tell. There are about 20 thousand records on this table, so I may be missing something.
Does anyone have any ideas?
9/14 In response to comments I am adding some additional code.
Here is the call on Form_Unload. It contains the failing line.
Private Sub Form_Unload(Cancel As Integer)
Dim ImportID
Dim rst As DAO.Recordset
Dim db As Database
Set db = CurrentDb
Set ImportID = Me.ImportID
#This is the failing line below.
Set rst = db.OpenRecordset("SELECT [dbo_t_inspect].* FROM [dbo_t_inspect] WHERE [dbo_t_inspect].ImportID= " & ImportID & ";", DB_OPEN_DYNASET, dbSeeChanges)
Here is the call from On_Current. This On_Current call is actually made by a sub-form on the main form.
Private Sub Form_Current()
Dim rst As DAO.Recordset
Dim strImportID As String
If IsNull(Me.ImportID) Then
[Forms]![Inspection Receiving]![LabComplete].Visible = False
[Forms]![Inspection Receiving]![LabPending].Visible = True
Else
strImportID = Me.ImportID
Dim db As Database
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT [dbo_t_health].*, [dbo_t_health].ImportID FROM [dbo_t_health] WHERE ((([dbo_t_health].ImportID)=" & Me.ImportID & "));", DB_OPEN_DYNASET, dbSeeChanges)
I am not sure where DAO is defined in this solution. I cannot find it's declaration in my folder titled Microsoft Office Access Class Objects I am open to suggestions on where that might be located.
The precise error message is as follows
Failed to connect to {ODBC Connection Name} Error 3151.
I can then select the debug option and the debugger will take me to the line that tried and failed to connect the the ODBC Connection I am using.
The Error is highly reproducible. All I have to do is enable VBA Macros on the Security Alert and then open the form in question. I close out the form after it loads. Once I attempt to reopen the form, I get the error described above. The Form opens anyway after I click Ok on the error.

Teradata ODBC 15.0 "ODBC Driver does not support the requested properties"

I'm using ADODB in Excel 2007 VBA to connect to a Teradata 14.0 server using the Teradata ODBC 15.0 driver. Everything works as expected, except for when I submit very large queries through ADODB.Recordset.Open.
Sporadically, when I attempt recordset.open with a query that is 5000+ characters it throws ODBC Driver does not support the requested properties Error -2147217887.
The error appears after what seems like a set amount of time (30 seconds or so) when I don't get a loaded recordset object back.
The code is straightforward:
Sub getData()
Dim strSQL As String
Dim scRS As ADODB.Recordset
Dim adoConn As ADODB.Connection
strSQL = "Very Large SQL"
'open the connection
Set adoConn = New ADODB.Connection
On Error GoTo adoExit
adoConn.Open "SessionMode=Teradata;Driver=Teradata;DBCName=<SERVERIP>;Database=<DATABASE>;CharSet=UTF16;Uid=<USERID>;Pwd=<PASSWORD>"
'get the data
Set scRS = New ADODB.Recordset
scRS.Open strSQL, adoConn, adOpenKeyset, adLockOptimistic
...
End Sub
Edited to add: I've tried variations of nearly anything that sounded like it might affect packet sizes/timeouts/character sets (which I thought might affect total bytes)/etc in the connection string using http://www.info.teradata.com/htmlpubs/DB_TTU_13_10/index.html#page/Connectivity/B035_2509_071A/2509ch03.05.06.html as a guide. And still I get sporadic timeouts.
Had the same problem while trying to run a composite query.
Try to add: ;QueryTimeout=0 at the end of your connection string (the one that follows adoConn.Open)

How to connect VBA to postgreSQL and run query

I am trying to run a query from an Microsoft excel application and have not been able to connect successfully.
I have PostgreSQL 9.3 on my local machine, and am running 64 bit windows 7. I have a sample database name dvdrental which is a demo database.
I simply need to connect to the database, run a query, and view the output in my worksheet(or immediate window, either one resolves the connection issue).
Here is what I have so far which is not working.
Option Explicit
Public objConnection As ADODB.Connection
Public strConnection As String
Public Sub TestPostgresConnection()
Dim strConnection As String
strConnection = "Driver={PostgreSQL Unicode};Server=localhost;Port=5432; Database=dvdrental;UID=sa;PWD=wrox;"
Set objConnection = New ADODB.Connection
Set objRecordSet = New ADODB.Recordset
objConnection.Open strConnection
With objRecordSet
.ActiveConnection = objConnection
.Open "SELECT * FROM actor"
End With
Do While Not objRecordSet.EOF
Debug.Print objRecordSet.Fields(0).Value
objRecordSet.MoveNext
Loop
objRecordSet.Close
objConnection.Close
Set objRecordSet = Nothing
Set objConnection = Nothing
End Sub
Here is a list of my references;
Visual Basic For Applications
Microsoft Excel 14.0 Object Library
OLE Automation
Microsoft Office 14.0 Object Library
Microsoft Forms 2.0 Object Library
Microsoft Access 14.0 Object Library
Microsoft ADO Ext. 6.0 for DOL and Security
Microsoft ActiveX Data Objects 2.8 Library
Microsoft Windows Common Confrols 6.0 (SP6)
When I execute this test method TestPostgresConnection, I get "[Miscrosoft][ODBC Driver Manager] Data source name not found and no default driver specified"
My setup of postgres has been standard and I have simply followed the directions on their website for creating a local RDBMS for testing.
Can anyone tell me why I am not able to connect and run a query?
None of the solutions have worked so far. Thanks.
My answer is a general answer. Giving back to the community. So be nice. I had a similar question and I used the following to set it up.
Note: I suggest using DSN instead of the driver then you can use a named connection that has the password already, rather than having the password in your code.
These instructions were a huge help generally:
http://www.dashbay.com/2011/03/working-with-postgres-on-windows-via-odbc/
The download link in the link above didn't work for me. I found the ODBC downloads here:
https://www.postgresql.org/ftp/odbc/versions/msi/
I think I downloaded this one:
psqlodbc_09_05_0400-x86.zip
I used Konstantin's answer to get %WINDIR%\SysWOW64\odbcad32.exe from this link:
PostgresSQL ODBC Drivers on Windows 7 not showing up
I also downloaded MS Power Query here which I found helpful:
https://www.microsoft.com/en-us/download/details.aspx?id=39379
I am happy to edit my answer if I should add clarification.
Below is the sub for the query and below that is a sub that demonstrates how you use it.
Sub CcQueryPg(sSql As String, Optional sOdbcName As String = "ConnectionNameHere")
'Declare a Connection object
Dim cnDB As New ADODB.Connection
'Declare a Recordset Object
Dim rsRecords As New ADODB.Recordset
'Open the ODBC Connection using this statement
cnDB.Open sOdbcName
rsRecords.Open sSql, cnDB
'Close everything and set the references to nothing
rsRecords.Close
Set rsRecords = Nothing
cnDB.Close
Set cnDB = Nothing
End Sub
Sub SendQuery()
Call CcQueryPg("COPY sometablenamehere FROM '/mnt/somepathhere/somefilename.csv' DELIMITER ',' CSV HEADER;")
End Sub
The above file reference is to a file on a Linux machine. Your path format may differ.

Are Access 2010 databases not accessible with Excel if password protected using default encryption (High Security)?

I am currently supporting an Excel 2010 spreadsheet and Access 2010 database that were written by business users. One of the requirements of the Access database is that it be encrypted. It was encrypted with the default encryption settings "Use default encryption(Higher security)" which can be set in Options -> Client Settings.
Now that the database is password protected and encrypted, I am unable to connect to the database through Excel. My testing revolves around importing data into Excel, but what I really need to do is create a row in a log table. I am trying both to import directly to the sheet using the "Data" tab and "From Access" selection and through VBA code. Using the Excel interface, the password dialog box comes up and will never accept the correct password. Using VBA and ADO, the Open statement throws a "not a valid password" error. Both methods work fine if I encrypt the database using the "Use legacy encryption" setting.
I thought it also may be my setup, I'm using Windows 7 32-bit and Office 2010. I have also tried with Windows 8.1 64-bit using Office 2013 with the same results. It works with legacy encryption, but not with default encryption. I didn't try anything earlier. The default higher security encryption was introduced with Office 2010 and Windows 7.
My research has led me to this Technet thread and this Stackoverflow question, both suggesting that Excel cannot interact with Access using the default encryption method. I haven't found a whole lot more discussing this exact issue.
My question to you is does password protecting an Access 2010 database using the default settings really prevent Excel 2010 from importing data (when using the password)? Something about that doesn't sound right to me since sharing data between the two applications is a pretty basic function. I also think that if it were an issue, Google would have turned up more information about it. My guess at this point is that Excel and Access are using the Next Generation encryption engine by default, but that the ADO library has not been updated to use it.
I've attached the connection code for review. For testing I am doing a simple Now() command and emitting the results. The connection fails on the open with a "not a valid password" error even when using the correct password. Works with legacy encryption, not with default encryption.
Sub ADOCNGConnect()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim ds As String
'setting up connection
Set cn = New ADODB.Connection
With cn
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source='FILEPATH'" & _
";Jet OLEDB:Database Password=password"
.Open
End With
'setup recordset object
Set rs = New ADODB.Recordset
'retrieve new number
rs.Open "SELECT Now() AS qryTest", cn, adOpenKeyset
MsgBox rs!qryTest
rs.MoveLast
'close ADO object vars
rs.Close: Set rs = Nothing
cn.Close: Set cn = Nothing
End Sub
According to ConnectionStrings.com, the ACE provider doesn't work with the new stronger Access 2010 db encryption:
"Note! Reports say that a database encrypted using Access 2010 - 2013 default encryption scheme does not work with this connection string. In Access; try options and choose 2007 encryption method instead. That should make it work. We do not know of any other solution."
However, that doesn't tell the whole story. Your code worked as an Access VBA procedure and successfully connected to another ACCDB which had the stronger Access 2010 encryption. But I could not find any way to make similar code work as an Excel VBA procedure.
Eventually I abandoned that effort. Since your goal seems to be to make an ADO recordset containing Access data available to Excel, I decided to automate Access and use its CurrentProject.Connection.Execute method to load the recordset.
This may seem kind of clunky, but it works ...
Const cstrPath As String = "C:\Users\hans\Documents\a2010_DbPass_foo.accdb"
Const cstrPwd As String = "foo"
Dim objAccess As Object ' Access.Application
Dim rs As Object ' ADODB.Recordset
Dim strSelect As String
Set objAccess = CreateObject("Access.Application")
objAccess.Visible = True
objAccess.OpenCurrentDatabase cstrPath, , cstrPwd
'strSelect = "SELECT Now() AS qryTest"
strSelect = "SELECT some_text AS qryTest FROM tblFoo"
Set rs = objAccess.CurrentProject.Connection.Execute(strSelect)
MsgBox rs!qryTest
rs.Close
Set rs = Nothing
objAccess.Quit
Set objAccess = Nothing
Note when I used "SELECT Now() AS qryTest" for strSelect, Access crashed at .Quit I don't understand why that happened. But the code worked trouble-free in Excel 2010 as written.

Multiple databases in MS Access VBA?

This Microsoft KB article details how to run a query on another database than the current one used by the Access project. However it only states how to connect to DBase, Foxpro, Paradox, BTrieve and ODBC.
I want to be able to do something like this:
UPDATE MSSQLDatabase.Table
SET MSSQLDatabase.Table.Column = AccessDatabase.Table.Column
WHERE MSSQLDatabase.Table.Column = AccessDatabase.Table.ID
INSERT INTO AccessDatabase.Table
VALUES (AccessDatabase.Table.ID)
Can you give me any pointers of where to begin? The database I want to connect to is a SQL Server 2008 Provider Native connection. I'm using Access 2007.
To do this in VBA would be perfect.
By far the easiest way to work with SQL Server in MS Access is to use linked tables. However, you can also run pass-through queries and refer to a connection in-line:
SELECT * FROM [ODBC;FILEDSN=Z:\Docs\Test.dsn;].table_1
Or
SELECT * FROM
[ODBC;DRIVER=SQL Server;SERVER=srvr;Trusted_Connection=Yes;DATABASE=Test;].table_1
Or
SELECT * FROM [ODBC;Driver={SQL Server Native Client 11.0};Server=svr;Database=test;Trusted_Connection=yes;].table_1
see also http://www.connectionstrings.com/sql-server-2008
This solution allows to catch errors:
Private Sub Command10_Click()
On Error GoTo Err1:
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
With cn
.Provider = "SQL Native Client"
.ConnectionString = "Server=myserver\myinstance;Database=mydb;Uid=myuser;Pwd=mypass;]"
.Open
End With
MsgBox "Connection successful!"
cn.Close
Exit Sub
Err1:
MsgBox Err.DESCRIPTION
End Sub
The only thing to note, is that within the Visual Basic Editor, you must first go to Tools > References, and check Microsoft ActiveX Data Objects 2.x Library.