0

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
2
  • Give an example of a number that has been converted to a date, ie number = dd/mm/yyyy Commented Jan 31, 2023 at 12:11
  • imgur.com/CVw2g4C Here is the link to the numbers, the ones that are normal are manually formated by me, i clicked on general, so that they got converted from date. I have no idea why they get auto-converted to DATE tho. Commented Jan 31, 2023 at 13:09

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.