Unable to reconnect to linked tables ODBC VBA - mysql

I'm using MS Access 2007 with Win7 Professional x64.
I have to connect to an external MySQL database passing by the ODBC v5.1 local driver.
From VBA I'm able to connect to my database succesfully and from Access I can open my linked tables and see the data.
You can see below the code that I'm using to create the linked table. I dont want to use DSN way; I prefer the driver way.
Dim CnnString As String
Dim tdf As DAO.TableDef
CnnString = "ODBC;DRIVER=MySQL ODBC 5.1 Driver;DATABASE=xxxx;OPTION=2048;PORT=0;SERVER=xxxxx;UID=xxxxx;PWD=xxxx;Connect Timeout=45; Command Timeout=90;"
Set tdf = CurrentDb.CreateTableDef("table1")
tdf.Connect = CnnString
tdf.SourceTableName = "table1"
CurrentDb.TableDefs.Append tdf
Set tdf = Nothing
When I quit my VBA/Access application and I reopen it, the linked table appears in the tables list but if I double click on it, the ODBC connector windows appears asking me a DSN connection, I cannot see the data and my application doesn't work.
How can I make the connection to my linked table permanent?
Thanks in advance

Now I've had the chance to check how I did this...
I have successfully (and permanently) linked DSN-less ODBC tables with VBA using the DoCmd.TransferDatabase command:
' CnnString as above
DoCmd.TransferDatabase _
TransferType:=acLink, _
DatabaseType:="ODBC", _
DatabaseName:=CnnString, _
ObjectType:=acTable, _
Source:="table1", _
Destination:="table1", _
StructureOnly:=False, _
StoreLogin:=True
The important part is StoreLogin:=True -- I don't think you can specify this when using the CreateTableDef method.

Related

VBA/ADODB incorrectly sees the database as read-only

I asked a similar question here.
The second block of code worked perfectly before. But now I'm getting the read-only error on it for some reason. I pasted it again below.
Dim oConn As New ADODB.Connection
Dim cmd As New ADODB.Command
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.1 Driver};" & _
"SERVER=server;" & _
"DATABASE=database;" & _
"USER=user;" & _
"PASSWORD=password;" & _
"Option=3"
Set cmd = New ADODB.Command
cmd.ActiveConnection = oConn
cmd.CommandText = strSQL
cmd.Execute
oConn.Close
MsgBox ("Query Successful")
I tried looking through the documentation and didn't see anything too helpful. I also mainly did an insert on the database and it still ran fine so the database isn't read-only.
What's wrong? Is there a way to force a read/write property?
Ok I essentially just created a new endpoint that was a copy of my old server, set server= to something else, and it worked. Both servers have the same permissions so I'm not exactly sure why but that's my fix for anyone who encounters this in the future.
I've got lot of problem with the ODBC 5.1 Drivers for MySQL (provided by default). Same case than you, worked fine and one day, many problems without any modification... But was solved with an update to the last version of MySQL Connector, the 8.0.
You can go into "ODBC Data Source Adminsitrator" from the Start Menu, and check into your "Drivers" panel if you have the last one. If not, you can download it on the official MySQL website here.
Then replace DRIVER={MySQL ODBC 5.1 Driver} by DRIVER={MySQL ODBC 8.0 Driver}.
Hope will help on your problem.

Connecting to another access database in windows 365

We have upgraded from Windows 7 to windows 365.
We have a macro within an access database. The macro opens another database using the code below. However since moving across to windows 365 it doesn't like the cn.open line.
How do you connect to another database from within Access in windows 365?
dim cn As ADODB.Connection
set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & pDBPATH & pDBNAME
the error message is,
run-time error 3706: Provider cannot be found. it may not be properly installed.
Although after reading it looks like Microsoft Jet OLEDB 4.0 is not 64 bit. So is there another way?
There are many ways to connect between databases in Access.
The most usual provider for OLEDB connection is the Microsoft.ACE.OLEDB.12.0 provider:
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & pDBPATH & pDBNAME
You can view two different providers Access uses to connect to the current database, by executing the following code:
Debug.Print CurrentProject.Connection.ConnectionString 'Usual connection
Debug.Print CurrentProject.AccessConnection.ConnectionString 'Used for updateable forms

Connect to online mySQL database using VBA

Objective:
I'm trying to connect to a database (e.g. associated with a website hosted by goDaddy) via VBA; using MS Word. I would like to distribute the VBA code via a word template so that others can also connect to my database.
Current Understanding - Is it correct?
In order to connect to a remote mySQL database I MUST configure a ODBC Data Source using (for example) mySQL Connector/ODBC (available here)?
There seems to be a way to connect without using a DSN as suggested here.
Issue:
I have been trying to use the mySQL Connector tool and am attempting to configure it with the information I have at hand. Steps taken:
download connector tool from dev.mysql.com
Control Panel > System & Security > Administrative Tools > ODBC Data Sources 64 Bit
add host: www.mywebsite.com
add user: NameOfDataBaseUser
add pwrd: PWForUser
I get the impression that I am using the wrong credentials... I found some documentation that said a list of DataBases would be displayed if connection is successful. That suggests to me that I should be using credentials for a master user - which user would that be?
Disclaimer
I do plan to connect to an online DB via VBA, but suspect that it might be better to connect indirectly via a php web-page.
If anyone has thoughts on this (security, ease of deployment, other) please let me know, it would probably be a new question. Other disclaimer, I am highly INexperienced with databases but keen to learn - slowly ;-)
I am currently working on a project with Excel where I am successfully connecting to a remote MySQL database.
I am using the DSN-less approach and this could probably work well for you, too:
Set remoteCon = New ADODB.Connection
conStr = "DRIVER={MySQL ODBC 5.3 Unicode Driver};" & _
"SERVER=myhomepage.com;PORT=3306;DATABASE=mydb;" & _
"UID=username;PWD=secret"
remoteCon.Open conStr
remoteCon.Execute ("USE mydb;")
In order for this to work, you also have to add a reference (in VBA backend): Tools > References > Check "Microsoft ActiveX Data Objects x.x Library".
You also need to have the MySQL ODBC Driver (in my case "MySQL ODBC 5.3 Unicode Driver") installed on your computer.
Queries can then be executed like this:
Dim rs As ADODB.Recordset
Set rs = remoteCon.Execute("SELECT * FROM table")
If Not rs.BOF And Not rs.EOF Then
result = rs.GetRows
End If
How to connect VBA to a Remote mySQL DataBase using ODBC
Thanks to #EVilliger & #tobifasc for your help with this, there are many 'how to configure mySQL questions' floating around but none that solved my (larger) issue.
Basic problem - my host does NOT allow remote connections to the database, except for from a single white-listed IP (this seems fairly common).
Questions Answered:
It turns out that you do NOT need to configure the mySQL connection using the connector, however you DO NEED to have an appropriate ODBC driver installed. The connector (with the driver) can be found here: https://dev.mysql.com/downloads/connector/odbc/
I uninstalled the mySQL Connector and everything seemed to continue working, until it didn't. Conclusion don't uninstall the mySQL Connector unless you have something to replace it with.
The credentials to use can be for a database user, not some elevated user.
For anyone interested in setting up and experimenting with mySQL from VBA here is a way forward:
Download & install the drivers (see above) - if the install fails you may need to install vcredist_x64.exe
Set-up a free mySQL database, I used HelioHost: https://www.heliohost.org/ (they will also give you a domain)
Create a database & user in HelioHost cPanel
Configure remote access - use wildcard % to allow all IPs
Add user to database
Use the following code to connect...
Code from accepted answer:
Sub connect2OnlineSQL()
' Note: add referecne to Microsoft ActiveX Data Objects #.# Library
' Tools > References > (scroll down...)
Set remoteCon = New ADODB.Connection
conStr = "DRIVER={MySQL ODBC 5.3 Unicode Driver};" & _
"SERVER=something.heliohost.org;PORT=3306;DATABASE=db_name;" & _
"UID=db_user;PWD=yourPassWordHere"
remoteCon.Open conStr
remoteCon.Execute ("USE db_name;")
End Sub
Thanks again :)
Following solution worked for me
Prerequisite:
Under Developer ->Tools->Reference Add the relevant Plug in for Oracle
Download and install ODBC Driver version for Oracle which you are using (32bit/64 bit)
Windows search -> ODBC Data Source -> Add Oracle driver under user DSN and System DSN
Copy paste below code which I used for MySQL and change the parameter based on your requirement
Sub ConnectToDB()
dbName = InputBox("Enter DB Name")
'Connection To MySQL
Dim oConn As ADODB.Connection
Dim str As String
str = "DRIVER={MySQL ODBC 8.0 Unicode Driver};SERVER=localhost;DATABASE=Employeeportal;PORT=3306;UID=root;PWD=root;"
Set oConn = New ADODB.Connection
oConn.Open str
MsgBox "Connected to MySQL DB"
'Exporting result set to Excel
Dim query As String
query = "select * from " & dbName
Dim recordSet As New ADODB.recordSet
recordSet.Open query, oConn
Sheet1.Range("A1:D1").CopyFromRecordset recordSet
oConn.Close
End Sub

MariaDB 2.0.13 vs MySQL 5.3 ODBC Drivers (VBA Connection)

In Excel-VBA We used to have a sql database connection using MySQL drivers. Since this wasn't working for everyone we decided to install MariaDB drivers at every pc and use these drivers. The speed of opening an connection however, has decreased dramatically. Here is the code of the new and old vba script.
p_dbConn.ConnectionString = _
"DRIVER={MariaDB ODBC 2.0 Driver};" & _
"SERVER=xx;" & _
"DATABASE=xx;" & _
"UID=xx;PASSWORD=xx;OPTION=3"
p_dbConn.Open
p_dbConn.ConnectionString = _
"DRIVER={MySQL ODBC 5.3 ANSI Driver};" & _
"SERVER=xx;" & _
"DATABASE=xx;" & _
"UID=xx;PASSWORD=xx;OPTION=3"
p_dbConn.Open
I ran the script in debug mode and the .open statement takes way longer with the mariadb driver. Anyone has any idea why?
Thanks!
I know it's not the driver that you are using ... but I use the following connection in my VBA code to connect to SQL Servers:
Reference File: Microsoft DAO 3.6 Object Library
'*************************************
'* SQL Server database connection *
'*************************************
Dim db As ADODB.Connection
Set db = New ADODB.Connection
db.Open "Provider=sqloledb; Data Source = 192.168.0.10; Database = [your DB Name];User Id = xxxxxx; Password = xxxxxxxx"
if a command takes longer than 45 seconds to execute ... use this to extend the execution default time
db.CommandTimeout = 1200 '(1200/60 = 20 minutes)
Sample Simple Commands
Set rst = db.execute("Select * from [your table name]")
db.execute("Delete from [your table name] where [your criteria]")

Excel VBA to SQL Database

I've just started looking into interacting with an SQL database via Excel VBA, and I started off with the basic connection code from MSDN:
Sub GetDataFromADO()
'Declare variables'
Set objMyConn = New ADODB.Connection
Set objMyRecordset = New ADODB.Recordset
Dim strSQL As String
'Open Connection'
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=fatcoin;User ID=root;Password=root;"
objMyConn.Open
'Set and Excecute SQL Command'
strSQL = "select * from productlist"
'Open Recordset'
Set objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Open strSQL
'Copy Data to Excel'
ActiveSheet.Range("A1").CopyFromRecordset (objMyRecordset)
End Sub
The issue I'm having is while I have an SQL instance running on my machine, on port 3306, which I can access using for example, HeidiSQL, every time I run this code I get an error message:
[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied
I have also tried adding a port:
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost,3306;Initial Catalog=fatcoin;User ID=root;Password=root;"
And other such things. I can't see any reason it shouldn't work, but I haven't played with this much at all. I've tried searching through other threads to no avail.
Any ideas?
I am working with Excel 2010, on a 64-bit Machine, using MySQL 5.7
I should mention the above coding is being inputted into the "Module1" section of the VBAProject on Excel.
Thanks
Thanks for the answers.
I solved my own question via a helpful link I found that suggested I go into ODBC 32-bit (because Excel is 32-bit) and create a System DSN. Then, I used the connections string below:
objMyConn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;User ID=root;Data Source=localhost;Initial Catalog=fatcoin"
It worked a treat.