码迷,mamicode.com
首页 > 数据库 > 详细

vba 读取数据库

时间:2016-07-18 02:17:00      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

1、安装数据库

2、创建数据源

 

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Row = 1 And Target.Cells.Column = 1 Then
       Call mySQL
    End If
End Sub

Public Function mySQL()
  strconnt = ""
  Set connt = CreateObject("ADODB.Connection")
  Dim sevip, Db, user, pwd As String
  设服务器地址 所连数据,及登录用户密码
  sevip = "localhost"
  Db = "excelTest"
  user = "root"
  pwd = "900921"
  strconnt = "DRIVER={MySql ODBC 5.3 Unicode Driver};SERVER=" & sevip & ";Database=" & Db & ";Uid=" & user & ";Pwd=" & pwd & ";Stmt=set names GBK"
  connt.ConnectionString = strconnt
  connt.Open
  MsgBox "链接状态:" & connt.State & vbCrLf & "ADO版本:" & connt.Version, vbInformation, ""
   Set Rec = CreateObject("ADODB.Recordset")
   Set Rec = connt.Execute("select * from `uses`", iRowscount, adCmdText)
   Range("a6:c6").Value = Array("id", "name", "password")
   Range("a7").CopyFromRecordset Rec
End Function

 

vba 读取数据库

标签:

原文地址:http://www.cnblogs.com/xiaofanke/p/5679684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!