mysqlvbaexcelcharacter-encoding

Get ?? when taking chinese characters from excel sheet and exporting to mysql via vba


Let me preface this question by saying that I know that this subject is tackled from various angles in numerous posts. However, I have spent hours and hours on this and still cannot get it to work, so asking for help.

I have some chinese characters in cells in an Excel sheet. I am using VBA to export the content of these cells into a mysql table. From the mysql command line, I am able to insert the chinese characters into the table successfully (I am using utf8mb4 charset).

I have also set charset=utf8mb4 in the VBA connection string.

However, I cannot get VBA to include the chinese characters in the query string, which is as follows:

    SQLStr = "INSERT INTO " & VBA.Trim(databaseName) & ".translationTable" & " VALUES ('" & _
    timestamp & "','" & _
    Replace(Worksheets("translationTable").Range("IORange").Cells(Counter, 2), Chr(10), "") & "','" & _
    Replace(Worksheets("translationTable").Range("IORange").Cells(Counter, 3), Chr(10), "") & "','" & _
    Replace(Worksheets("translationTable").Range("IORange").Cells(Counter, 4), Chr(10), "") & "','" & _
    Replace(Worksheets("translationTable").Range("IORange").Cells(Counter, 4), Chr(10), "") & "')"
    rs.Open SQLStr, oConn

I know that VBA editor will not display the chinese characters in any case. However, chinese characters inserted from VBA are showing up as ?? in the database as well.

It seems that something that I am doing is converting the chinese characters to ASCII, before the INSERT statement is executed.

What am I doing wrong?


Solution

  • The usual cause of multiple question marks:

    If those tips don't suffice, then show us what is in the tables, using something like

    SELECT col, HEX(col) FROM tbl...
    

    Chinese cannot be "converted to ascii".

    Addenda

    Try setting the DNS's 'Connect Options'->'Initial Statement' to 'SET NAMES utf8mb4'. -- MySQL ODBC 3.51 Driver UTF-8 encoding