COMException including Excel during ADO connection - mysql

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.

Related

Connecting Ms Access Db to Mysql through Vba

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

How to connect to mysql using vba in excel 2007

I have used the code bellow to connect to mysql and I have install mysql odbc connector version 5.3.
'Connect to database
Function CnnOpen(ByVal ServerName As String, ByVal DBName As String, _
ByVal TblName As String, ByVal User As String, ByVal PWD As String)
Dim CnnStr As String
Set Cnn = CreateObject("ADODB.Connection")
Cnn.CommandTimeout = 15
CnnStr = "DRIVER = {MySql ODBC 5.3 Driver}; SERVER =" & ServerName & _
"; Database =" & DBName & "; USER =" & User & "; PASSWORD =" _
& PWD & "; Option=3"
Cnn.ConnectionString = CnnStr
Cnn.Open
End Function
I have the following error:
Run-time error '-2147467259 (80004005)':
[Microsoft][ODBC Driver Manager]Data source name not found and
no default driver specified
Is there any incorrect connection string or any mistake in the code? Please, help.

VBA Connect to MYSQL

I am trying to setup a remote database connection to a server running MySQL version 5.0.96. When I execute I get the following error.
Any assistance is appreciated.
Here is my connection code:
Public Function opendb()
Dim oConn As ADODB.Connection
Dim Server_Name As String
Dim User_Name As String
Dim Password As String
Dim Database_Name As String
Server_Name = Sheets("_config").Range("B2").value
User_Name = Sheets("_config").Range("B3").value
Password = Sheets("_config").Range("B4").value
Database_Name = Sheets("_config").Range("B5").value
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.0.96 Driver};" & _
"SERVER=Server_Name;" & _
"DATABASE=Database_Name;" & _
"USER=User_Name;" & _
"PASSWORD=Password;" & _
"Option=3"
End Function
Dim oConn As ADODB.Connection
Dim Server_Name As String
Dim User_Name As String
Dim Password As String
Dim Database_Name As String
Server_Name = Sheets("_config").Range("B2").value
User_Name = Sheets("_config").Range("B3").value
Password = Sheets("_config").Range("B4").value
Database_Name = Sheets("_config").Range("B5").value
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.0.96 Driver};" & _
"SERVER=" & Server_Name & ";" & _
"DATABASE=" & Database_Name & ";" & _
"USER=" & User_Name & ";" & _
"PASSWORD=" & Password & ";" & _
"Option=3"
There might be other problems with your drivers but when I tested the code you posted on my end I got the same error - it's not going to read your variables unless you concat them
Edit - I'm using a 5.2a server - when I try to connect using 5.0.96 I get the same error again , but of course using the 5.2a driver in the connection string works fine. So you will probably need to download the 5.0.96 driver specifically. I did a little searching and couldnt find it, I'll keep looking around for a minute
MySQL server is running 5.0.96. DO NOT INSTALL MySQL Connector/ODBC 5.2.6. This driver is NOT backwards compatible from VBA. If you have it installed and you are having the same problem I did do the follow:
1) FIRST remove any User & System Data Sources you have added
2) Uninstall all ODBC 5.2 drivers
3) Installed Connector/ODBC 3.51.30. (You May Need to Reboot)
4) Make your Connection String > DRIVER={MySQL ODBC 3.51 Driver};
Hope this saves you lots of time.
You may write Set oConn = CreateObject("ADODB.Connection") 'NEW STATEMENT
instead of Set oConn = New ADODB.Connection

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.