How to import mysql data with VBA without defacing text

Asked

Viewed 507 times

2

I have a macro that connects to mysql and executes a query that is in text file and adds the records in an excel spreadsheet. It turns out that the text is disfigured. Example: Correct: CALL QTDE PERFORMED (CPF VIEW) As it comes: QTDE CALLS MADE (Visã3º CPF)

How do I configure and where the query brings me results without problems with text?

Connection procedure:

Sub Conexao(SQL)

    On Error GoTo Error


    Set myCon = New ADODB.Connection
    myCon.ConnectionString = _
    "driver={mysql odbc 5.2 Unicode Driver};" & _
    "server=172.17.0.140;database=DWH_RBZ;uid=usuario;pwd=senha;Option=3;"
    myCon.CursorLocation = adUseClient
    myCon.Open

    
    With myCmd
          Set .ActiveConnection = myCon
          .CommandType = adCmdText
          .CommandText = SQL
    End With
    
    With myRS
       .LockType = adLockPessimistic
       .CursorType = adOpenKeyset
       .CursorLocation = adUseClient
       .Open myCmd
    End With

    Exit Sub

Error:
    MsgBox Err.Number & vbCrLf & Err.Description, vbExclamation, "ERRO DE CONEXAO COM O BANCO"
    

End Sub

Procedure that pastes query data in spreadsheet

Sub Roda_Query_1()
Dim SQL As String
Dim dias As Integer

    dias = CDate(Dt_Final) - CDate(Dt_Incio)
    For i = 0 To dias
        SQL = LinhaALinhaLer
        SQL = Mid(SQL, 4, 1000000)
        SQL = Replace(SQL, "(@inicio)", "('" & Format(Dt_Incio, "yyyy-mm-dd") & " 00:00:00')")
        SQL = Replace(SQL, "(@fim)", "('" & Format(Dt_Final, "yyyy-mm-dd") & " 23:59:59')")
        
        Conexao (SQL)
        
        If myRS.RecordCount = 0 Then
            MsgBox ("Erro na consulta do relatório")
            myRS.Close
            myCon.Close
            Exit Sub
        End If
        
        
        If VerificaExistePlan("Produtividade") = False Then
           Call Cria_Plan_Dados
        End If
        Sheets("Dados").Range("A2").CopyFromRecordset myRS
    Next i



    myRS.Close
    myCon.Close
   End Sub

1 answer

1

I believe that the Charset for the connection will solve the problem.

myCon.Charset = "utf-8"

Also check the Charset of the table. You can also test using the driver ANSI instead of Unicode used.

I hope to have help, anything post a feedback that we will test with more time to find the solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.