Connecting to MySQL server from excel with VBA code - mysql

I'm running a MySQL server on my old computer and trying to connect it from my current computer. I would like to access to the server from Excel but I keep getting the error:
'[Microsoft] [ODBC Driver Manager] Data source name not found and no default driver specified'
I've downloaded a 'MySQL connector ODBC 8.0.20 win 32' which should match my computer requirements. At this point I'm quite clueless why I keep getting the error. Please find my VBA code below.
Private Sub Data_to_SQL_Click()
Dim con As ADODB.Connection
Set con = New ADODB.Connection
Dim strConn As String
strConn = "Driver={MySQL ODCB 8.0 Unicode Driver};SERVER=(my old computer ip address);DATABASE=Database;USER=root;PASSWORD=(password)"
con.Open strConn
Dim rng As Range
Set rng = Application.Range("A2:E14")
Dim row As Range
For Each row In rng.Rowss
Dim Sql As String
Sql = "insert into kavijamaarat (pvm, ravintola, ruoka, hinta, kpl) values ('" & row.Cells(1).Value & "', '" & row.Cells(2).Value & "', '" & row.Cells(3).Value & "', '" & row.Cells(4).Value & "', '" & row.Cells(5).Value & "')"
con.Execute Sql
Next row
con.Close
MsgBox "Done"
End Sub

Rewrite
strConn = "Driver={MySQL ODCB 8.0 Unicode Driver};SERVER=(my old computer ip address);DATABASE=Database;USER=root;PASSWORD=(password)"
to
strConn = "Driver={MySQL ODBC 8.0 Unicode Driver};SERVER=(my old computer ip address);DATABASE=Database;USER=root;PASSWORD=(password)"

Related

Connecting to MySQL Database (hosted in Heroku) from Excel VBA

I'm hosting the MySQL database in Heroku. I created a connection from Excel with 'MySQL for Excel' add-in, but I want to establish a connection from Excel with VBA code.
I get
Access denied for user 'user' to database 'database'
My VBA code, but I think the reason for the error is somewhere else (privileges, database settings, etc... ).
Dim con As ADODB.Connection
Set con = New ADODB.Connection
Dim strConn As String
strConn = "Driver={MySQL ODBC 8.0 Unicode Driver};SERVER=serverName;DATABASE=databaseName;USER=username;PASSWORD=password"
con.Open strConn
For exammple, here is a piece of code by using mariaDB, btw you have to install corresponding driver for database:
host_address = "xxx.xxx.xxx.xxx"
Set conn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
conn.ConnectionString = "DRIVER={MariaDB ODBC 3.0 Driver};" _
& "SERVER=" & host_address & ";" _
& " DATABASE=databasename;" _
& "UID=username;PWD=password; OPTION=3"
conn.Open
strSQL ="....."
....

Connect MySQL(multiple instances) Database with Excel VBA

I have multiple instances of MySQL installed in my pc which are instance1 & instance2...
1st instance data directory is "C:\MYSQL2\Data", address is: localhost, port= 3306;
2nd instance data directory is "C:\ProgramData\MySQL\MySQL Server 5.7\Data", address is: localhost, port= 3308;
server_name = "localhost"
port = "3308"
database_name = "test2"
user_id = "root"
password = "xxxx"
Set conn = New ADODB.Connection
conn.Open "DRIVER={MySQL ODBC 3.51 Driver}" _
& ";Data Source=localhost" _
& ";SERVER=" & server_name _
& ";PORT=" & port _
& ";DATABASE=" & database_name _
& ";UID=" & user_id _
& ";PWD=" & password _
& ";OPTION=16427"
When i run the above code to connect with 2nd instance database it showing me error:
Run-time error '-2147467259(80004005)': [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
This code also showing error for my 1st instance which server name is localhost and port is=3306
This code running fine where only default instance is installed.
In the connection string if
Driver={MySQL ODBC 8.0 Unicode Driver}
then if you open ODBC data Sources (32-bit) not ODBC data Sources (64-bit)
see the same name
Then this code will work
Public Sub ask_sql1()
Dim SQL As String
SQL = "SELECT * FROM Users"
Dim Conn As ADODB.Connection
Set Conn = New ADODB.Connection
Conn.ConnectionString = "Driver={MySQL ODBC 8.0 Unicode Driver};Server=127.0.0.1;Database=test;UID=root;PWD=*********"
Conn.Open
Dim recordSet As ADODB.recordSet
Dim Field As ADODB.Field
Set recordSet = New ADODB.recordSet
recordSet.Open SQL, Conn, adOpenStatic, adLockReadOnly, adCmdText
If recordSet.State Then
For Each Field In recordSet.Fields
MsgBox Field.Name
Next Field
Set recordSet = Nothing
End If
Conn.Close
End Sub
also is you office 64 or 32
Can you add a screenshot of ODBC data Sources (32-bit)/Drivers ?

How To Insert Data in Hosting Database Using VB.NET

I've a Hosting that contains mysql database, and i simply want to insert data into that database but using VB.NET.
Ex :
There's a Textbox and a button, when click on button is performed the value from that textbox have to be inserted in database of hosting.
What service or functions should i use??
Dim Conn As New ADODB.Connection
Conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & server & ";DATABASE=" & database & ";UID=" & user & ";PWD=" & pass & ";PORT=" & port & ";"
Conn.Open()
Conn.Execute("insert into tblname (fld1,fld2) values('"& textVal &"',"& numericVal &")")
Conn.Close()
Conn = Nothing
Paste in .vb file and edit connectionstring & insert string.
Good luck..

How do I setup an ADODB connection to SQL Server 2008 in Microsoft Access 2010?

I just installed SQL Server 2008 on my laptop. I also have Microsoft Access 2010 installed. Using VBA, I am trying to create an ADODB connection to my own database on SQL Server but I'm having trouble finding the right line of code:
When I use this below, it doesn't work.
The name of my computer is LAPTOPX and the database is HomeSQL.
I am sure it's super easy but since I'm just starting out I can't seem to find the right way to ask the question.
Thanks!
Dim DBCONT As Object
Set DBCONT = CreateObject("ADODB.Connection")
Dim strDbPath As String
strDbPath = "LAPTOPX/HomeSQL"
Dim sConn As String
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source =" & strDbPath & ";" & _
"Jet OLEDB:Engine Type=5;" & _
"Persist Security Info=False;"
DBCONT.Open sConn
First, you need to make sure SQL Native Client is instaled. Reference
SQL Server 2008
Standard security
Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;Uid=myUsername;
Pwd=myPassword;
Trusted connection
Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;
Trusted_Connection=yes;
Connecting to an SQL Server instance
The syntax of specifying the server instance in the value of the server key is the same for all connection strings for SQL Server.
Provider=SQLNCLI10;Server=myServerName\theInstanceName;Database=myDataBase;
Trusted_Connection=yes;
Source
Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim sConnString As String
Dim recordsAffected as Long
'Create connection string
sConnString = "Provider=sqloledb; Server=LAPTOPX; Database=HomeSQL; Trusted_Connection=True;"
'Open connection and execute
conn.Open sConnString
'Do your query
With cmd
.ActiveConnection = conn
.CommandType = adCmdText
.CommandText = "Select ...;"
.Execute recordsAffected 'Includes a return parameter to capture the number of records affected
End With
Debug.Print recordsAffected 'Check whether any records were inserted
'Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set cmd = Nothing
Set conn = Nothing
This connetion string works under Excel VBA. In MsAccess also should.
dbName = "test" 'your database name
dbFilePath = "C:\db.mdf" 'your path to db file
connStr = "Driver={SQL Server native Client 11.0};" & _
"Server=(LocalDB)\v11.0;" & _
"AttachDBFileName=" & dbFilePath & ";" & _
"Database=" & dbName & ";" & _
"Trusted_Connection=Yes"
Full solution: http://straightitsolutions.blogspot.com/2014/12/how-to-connect-to-sql-server-local.html

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