使用VBA导入mySQL数据时出现乱码的特殊字符

时间:2015-03-18 21:39:38

标签: mysql excel vba excel-vba

我创建了一个在MySql中连接的宏,并使用文本文件中的SQL代码运行查询。经过寄存器并添加一个新的工作表。 问题是在workSheet中显示未配置的文本字段。 例: 原样:“VISÃOCPF” 应该是:“VISÃOFFF”

我来自巴西,如果这是问题。

连接程序:

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=intelserv.caio;pwd=@c@io89idav;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

运行查询程序:

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

0 个答案:

没有答案