I'm really new to Excel macros and VBA. What I'm try to do is to get the data from MySQL database using macro. I search for code to connect the Excel to MySQL and i got this one
Sub connect()
Dim Password As String
Dim SQLStr As String
Dim Server_Name As String
Dim User_ID As String
Dim Database_Name As String
Dim table_name As String
Set rs = CreateObject("ADODB.Recordset")
Server_Name = ""
Database_Name = ""
User_ID = ""
Password = ""
table_name = ""
Set cn = CreateObject("ADODB.Connection")
cn.Open "Driver={MySQL ODBC 5.1 Unicode Driver};Server=" & _
Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
SQLStr = ""
rs.Open SQLStr, cn, adopenstatic
ThisWorkbook.Sheets(1).Range("A1").CopyFromRecordset rs
cn.Close
MsgBox "DONE!"
End Sub
I change some of code to match my system
Sub connect()
Dim Password As String
Dim SQLStr As String
Dim Server_Name As String
Dim User_ID As String
Dim Database_Name As String
Dim table_name As String
Set rs = CreateObject("ADODB.Recordset")
Server_Name = "localhost"
Database_Name = "sakila"
User_ID = "root"
Password = "Password"
table_name = ""
Set cn = CreateObject("ADODB.Connection")
cn.Open "Driver={MySQL ODBC 8.0 Unicode Driver};Server=" & _
Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
SQLStr = "select * from film_actor"
rs.Open SQLStr, cn, adopenstatic
ThisWorkbook.Sheets(1).Range("A2").CopyFromRecordset rs
cn.Close
MsgBox "DONE!"
End Sub
It works just fine with query like that, but when I try to change the query to
SQLStr = "select a.title, concat(b.first_name, Space(1),b.last_name), c.name, a.description, a.rating from film_actor z"
left join film a on a.film_id = z.film_id
left join actor b on b.actor_id = z.actor_id
left join film_category y on y.film_id = z.film_id
left join category c on c.category_id = y.category_id
order by a.title asc"
It always show 'Expected: end of statement' and highlighting the 'film' word in 1st left join and there's a yellow arrow also highlighting 'Sub connect()' in 1st row. I already remove the quotation mark after 'film_actor z' but its keep coming back. Is there anything wrong with my query / code?