vba 链接mysql

 首先下载 mysql odbc 驱动

 然后配制数据源

 

'MsgBox Sheets(4).Range("A1").End(xlDown).Row
    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim fid As ADODB.Field
    Dim sql As String  'DRIVER={MySQL ODBC 3.51 Driver}
    Set conn = CreateObject("ADODB.Connection")  ' 这个很重要 没有会报错
    ' connstr = "DRIVER={MySql ODBC 5.1 Driver};SERVER=127.0.0.1;Database=test;Uid=root;Pwd=liyudong"
    conn.ConnectionString = "DRIVER={MySql ODBC 5.1 Driver};SERVER=127.0.0.1;Database=test;Uid=root;Pwd=123456"  ’ 驱动版本要正确,不能太低
   
    conn.Open
    Set rs = New ADODB.Recordset
    rs.Open "select * from lyd_test", conn
    'rs.MoveFirst
    'Do Until rs.EOF
    '   For Each fid In rs.Fields
    '       MsgBox fid.Value
    '
    '   Next
    'Loop
   
    conn.Close
    Set conn = Nothing

你可能感兴趣的:(mysql,server,String,database,each,VBA)