I have an excel file, which needs to have a connection to a mysql database with odbc. I am aware of the option to do it through UI, but i would prefer not to (data - get_data - from other sources - from odbc). I have made it in access, the linked tables are the same as the ones that i can create through the UI. In excel, i have not found a solution for this. For now, i have some weird code that establishes the connection just fine, but creates weird tables in excel when trying to get the data from the database (numbers being converted to dates, some data not even loading, ...).
Here is the code that i have for now in excel:
Function connect2mssql()
Dim con As ADODB.connection
Dim rs As ADODB.Recordset
Set con = New ADODB.connection
Dim SQL As String
con.Open "DRIVER={MySQL ODBC 5.3 Unicode Driver};SERVER=192.168.12.192;DATABASE=rezervni_deli;UID=vzdrzevalec;PWD=unichem"
''####################
SQL = "SELECT * FROM rezervni_deli;"
Set rs = New ADODB.Recordset
rs.Open SQL, con
For intColIndex = 0 To rs.Fields.Count - 1
Worksheets("rezervni_deli").Range("A1").Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
Worksheets("rezervni_deli").Range("A2").CopyFromRecordset rs
''####################
SQL = "SELECT * FROM p_linija;"
Set rs = New ADODB.Recordset
rs.Open SQL, con
For intColIndex = 0 To rs.Fields.Count - 1
Worksheets("p_linija").Range("A1").Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
Worksheets("p_linija").Range("A2").CopyFromRecordset rs
''####################
rs.Close
Set rs = Nothing
End Function