Connecting Ms Access Db to Mysql through Vba - mysql

I have been trying to connect mysql database to ms Access but no result.I don't think personally I am using the DAo.Connection and the workspace properly. I keep on getting the 3001 connection error when I set mySqlCon. I guess my arguments are not properly set but I was following an example from here.
Function connectingMySql()
Dim mySqlCon As Dao.Connection
Dim wrkODBC As Workspace
Set wrkODBC = CreateWorkspace("newODBCWorkspace", "admin", "", dbUseODBC)
Set mySqlCon = wrkODBC.OpenConnection("connection1", , , "DRIVER={MYSQL ODBC 5.1 DRIVER};" _
& "SERVER=testserver.com;port=3306;" _
& "DATABASE=test;" _
& "USER=root;" _
& "PASSWORD=pass;" _
& "Option=3;")
End Function
More infos:
Running Ms Access 2003

This MSDN page says that using dbUseODBC will cause a runtime error.
ODBCDirect workspaces are not supported in Microsoft Access 2010. Setting the type argument to dbUseODBC will result in a run-time error. Use ADO if you want to access external data sources without using the Microsoft Access database engine.
Try Set wrkODBC = CreateWorkspace("newODBCWorkspace", "admin", "")

After much struggle, I fixed it. Basically my arguments where wrong. Now I am getting a connection Error, which means the argument error has been fixed.
Function connectingMySql()
Dim mySqlCon As Dao.Connection
Dim wrkODBC As Workspace
Set wrkODBC = CreateWorkspace("newODBCWorkspace", "admin", "", dbUseODBC)
Set mySqlCon = wrkODBC.OpenConnection("DRIVER={MYSQL ODBC 5.1 DRIVER};" _
& "SERVER=testserver.com;port=3306;" _
& "DATABASE=test;" _
& "USER=root;" _
& "PASSWORD=pass;" _
& "Option=3;")
End Function

Related

Excel VBA connection to external MySQL server (ODBC 8.0)

I wanted to connect an excel file with a MySQL server for a project, so I searched the internet for a solution. I found some, but all of it was old and I don't prefer 8–10-year-old methods and drivers.
All I got is [Microsoft][ODBC Driver Manager] Data source name not found, and no default driver specified
So, I setted up the following setup:
A MySQL 8.0 server (on a Ubuntu server in the oracle cloud) 64-bit, ODBC connector 8.0 Unicode 64-bit
Office 365 64 bit
I tried to make a connection with some of the methods, but they gave all the same error:
[Microsoft][ODBC Driver Manager] Data source name not found, and no default driver specified
Public Function OpenConnection() As ADODB.Connection
''This function requires the "Microsoft ActiveX Data Objects" Library (Choose v2.8 from references for compatibility across Office versions)
Dim source As String, location As String, user As String, password As String
location = My server IP
user = "ExcelTest"
password = "Pass"
database = "Test"
mysql_driver = "MySQL ODBC 8.0 Driver" ''Tried "MySQL ODBC 8.0 Unicode Driver" too
''Build the connection string
Dim connectionString As String
connectionString = "Driver={" & mysql_driver & "};Server=" & location & ";Database=" & database & ";UID=" & user & ";PWD=" & password
''Create and open a new connection
Set OpenConnection = New ADODB.Connection
OpenConnection.CursorLocation = adUseClient
Call OpenConnection.Open(connectionString)
End Function
Usually I found a pretty good tutorial or walkthrou but this time nothing up to date.
I yes. I am pretty sure All the things are 64 bit.
I do not know where to search Please help
Test the driver has installed correctly by using Windows ODBC Data Source Administrator first.
Option Explicit
Sub test()
Dim conn
Set conn = OpenConnection()
With conn
.CursorLocation = adUseClient
MsgBox "Connected to " & .DefaultDatabase, vbInformation
End With
End Sub
Public Function OpenConnection() As ADODB.Connection
Const location = ""
Const user = ""
Const password = ""
Const database = "test"
Const mysql_driver = "MySQL ODBC 8.0 Unicode Driver"
' Build the connection string
Dim s As String
s = "Driver={" & mysql_driver & "};Server=" & _
location & ";Database=" & _
database & ";UID=" & _
user & ";PWD=" & password
Debug.Print s
' Open connection
Set OpenConnection = New ADODB.Connection
OpenConnection.Open s
End Function

COMException including Excel during ADO connection

Ok, i don't know if it will be useful but i want to share. Modos and admin will delete if they want.
I'm developping a VB application (actually, i migrate one from Excel VBA) to interact with MySQL Database.
I use VS 2019 16.8.2, Windows 7 x64.
I got a cryptic issue during connection to the database (using ADO, reference used: MS ADO 2.6).
Here's the code:
Public Sub test_connect()
Dim server_name As String
Dim database_name As String
Dim login_database As String
Dim port_number As String
Dim pwd_database As String
server_name = "my_server"
database_name = "my_database"
login_database = "whoisyourdaddy"
pwd_database = "youare"
connexion_dlweb_doc = New ADODB.Connection With {
.ConnectionString = "DRIVER={MySQL ODBC 8.0 ANSI Driver};" _
& "SERVER=" & server_name & "; " _
& "PORT=" & port_number & "; " _
& "DATABASE=" & database_name & "; " _
& "UID=" & login_database & "; " _
& "PWD=" & pwd_database & "; " _
& "OPTION=3"
}
If connexion_dlweb_doc.State <> adStateOpen Then
connexion_dlweb_doc.Open()
End If
End Sub
So, i got an exception with the " connexion_dlweb_doc = New ADODB.Connection" command. It's where it becomes cryptic:
System.TypeInitializationException : 'The type initializer for 'MyAPP.mod_declaration_variables' threw an exception.'
COMException : Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154 Classe non enregistrée (0x80040154 (REGDB_E_CLASSNOTREG)).
mod_declaration_variables is a module where i declare global variables like connexion_dlweb_doc:
Public connexion_dlweb_doc As ADODB.Connection
So, i asked my best friend google about this cryptic message and the CLSID {00020819-0000-0000-C000-000000000046} refer to...Microsoft.Interop.Excel 14.0.0.0
I use the reference MS Excel Objet Library 14.0 in my project. How did this interact with the ADO connection part? What's the link? Why the code crash here and not before?
I don't know why but the solution was to delete (or comment) every variable declaration using Excel library, e.g:
Public xlApp_arbo_wiki As Excel.Application
Guess it would help noobs like me.

Error while connecting to Access database through Excel 2010

I am trying to retrieve data from an access database on my C drive and i am getting the following error:
"Cannot start your application. The workgroup information file is missing or opened exclusively by another user."
the debugger shows the error in the following piece of code:
MyConnObj.Open _
"Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\<database>.mdb;" & _
"User ID=<username>;" & _
"Password=<pass>;"
how do i fix this error?
I have the following definitions:
Dim MyConnObj As New ADODB.Connection 'ADODB Connection Object
Dim myRecSet As New ADODB.Recordset 'Recordset Object
Dim sqlStr As String ' String variable to store sql command
Not sure, but that error means that you need to specify a Workgroup (system database) file.
Try to add this to your connection
MyConnObj.Open _
"Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\<database>.mdb;" & _
"Jet OLEDB:System Database=<path to system.mdw with information on username/pass>;" & _
"User ID=<username>;" & _
"Password=<pass>;"
in alternative, if your access mdb is not protected with a system.mdw file, you could try to remove the "User ID" and "Password" parts from the connection string.

Why am I getting Data provider or other service returned an E_FAIL status? SQL Native Client

I'm switching our ASP code to use SQL Native Client so that we can connect to a mirrored database with a failover partner, since you can only supply the failover partner parameters in SQL Native Client. When I run a proc that returns an nvarchar(max) column with Driver={SQL Server} everything works fine. When I run procs that return small colums with Driver={SQL Server Native Client 10.0} that works fine. It's only when I try to run a proc that returns an nvarchar(max) column while using Driver={SQL Server Native Client 10.0}; that I get the error. The error happens as soon as we hit
rs.Open cmdTemplate
So I'm not even referring to the column. Setting the conn string like this:
if bUseSQLNative then
connString = "Driver={SQL Server Native Client 10.0}; Network=DBMSSOCN; server=" & rs("SERVER_NAME") & "," & rs("PORT_NUM") & ";database=" & rs("DATABASE_NAME")
connString = connString & ";uid=" & rs("USER_NAME") & ";pwd=" & UnProtectValueEx(ConnSaltForDBPwd(), rs("CONNECTION_NAME"), rs("PASSWORD"))
else
connString = "Driver={SQL Server}; Network=DBMSSOCN; server=" & rs("SERVER_NAME") & "," & rs("PORT_NUM") & ";database=" & rs("DATABASE_NAME")
connString = connString & ";uid=" & rs("USER_NAME") & ";password=" & UnProtectValueEx(ConnSaltForDBPwd(), rs("CONNECTION_NAME"), rs("PASSWORD"))
end if
connString = connString & ";"
And opening like this:
set rs = server.CreateObject("ADODB.RecordSet")
rs.CursorLocation = 3
rs.CursorType = 3
rs.CacheSize = 50
on error resume next
rs.Open cmdTemplate
The error is:
Microsoft Cursor Engine (0x800A0001)
Data provider or other service returned an E_FAIL status.
In my case, the data to be saved (string) was larger than the specified nvarchr().
Increasing the field size solved the problem
I found it. I had to use
connString = "Provider=SQLNCLI10; DataTypeCompatibility=80;...
The DataTypeCompatibility makes the nvarchar max etc map back to formats ado can handle. And for some reason that parameter doesn't take effect with Driver={SQL Server Native Client 10.0};, only with Provider=SQLNCLI10
Use the reference MSADO 6.1 Library and then construct a data environment to connect to the database and establish the recordset to be used like this:
mydata = is a data environment with the connection to the database
getItemRec = is the query or command inside the mydata
myRecSet = is a Recordset Variable.
Do the code like this:
myData.Commands("getItemRec").CommandText = "Select * from myTable"
myData.getItemRec
Set myRecSet = myData.rsgetItemRec
With myRecSet
If .RecordCount <> 0 Then .MoveNext
Do While .EOF = False
....
....
.movenext
Loop
end With
Hope this will Help.

Connection string for Access 2010

I want to connect to the access 2010 database from excel.I am using VBA.I wrote the connection string as
Public objCon As New ADODB.Connection
objCon.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ActiveWorkbook.Path & _
"\asset_database.accdb;ACE OLEDB:Database Password=password;"
But it is giving the error "could not find installable ISAM".What is this error?
Oddly enough, it is Jet OLEDB Password, not ACE:
objCon.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ActiveWorkbook.Path _
& "\asset_database.accdb;Jet OLEDB:Database Password=password;"
See: http://www.connectionstrings.com/access-2007
Try "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ActiveWorkbook.Path & "\asset_database.accdb;JET OLEDB:Database Password=password;"
If you have Access installed, or you install the Access Database Engine on the workstation that has the Excel spreadsheet open, then you don't need to create an OLEDB connection; you can just open a database object as follows:
dim db as database
set db=opendatabase("c:\db path\my db.accdb")