可以使用以下代码在EXCEL上使用VBA连接MYSQL数据库 :
Dim strconnt As Stringstrconnt = ""
Set connt = New ADODBConnection
Dim sevip, Db, user, pwd As String
'设服务器地址、所连数据,及登录用户密码
sevip = "localhost"
Db = “test"
user = "root"
pwd = "123456"
strconnt = "DRIVER={MySql ODBC 53 Unicode Driver};SERVER=" & sevip & ";Database=" & Db & ";Uid=" & user & ";Pwd=" & pwd & ";Stmt=set names GBK"
conntConnectionString = strconnt
conntOpen
1、基本上所有的数据库都有对应的写法(有些数据库是要装连接驱动才能连),比如"Provider=MicrosoftJetOLEDB40(用来连接access)或者provider =MSDASQL,而且每种数据库也可能会有多种连接方法(Provider意即数据库引擎),常见的有下面这些
Provider 代码 Provider
ADSDSOObject Active Directory Services
MicrosoftJetOLEDB40 Microsoft Jet databases
MSDAIPPDSO1 Microsoft Internet Publishing
MSDAORA Oracle databases
MSDAOSP Simple text files
MSDASQL Microsoft OLE DB provider for ODBC
MSDataShape Microsoft Data Shape
MSPersist Locally saved files
SQLOLEDB Microsoft SQL Server
2、参见>
Private Sub CommandButton1_Click()
Dim mydate As String, mytable As String, sql As String, i As Integer
Dim cnn As ADODBConnection
mydata = ThisWorkbookPath & "\商品信息表mdb"
mytable = "inventory"
Set cnn = New ADODBConnection
With cnn
Provider = "microsoftjetoledb40;Persist Security Info=False;Jet OLEDB:Database Password=123"
Open mydata
End With
For i = 1 To 4
sql = "update inventory set 商品编码='" & Chr(i + 64) & "'+MID(商品编码,3) where 商品编码 like '0" & i & "%'"
cnnExecute sql
Next
MsgBox "存货编码批量替换成功!", vbInformation
cnnClose
Set cnn = Nothing
End Sub
Sub 查找()
Set conn = CreateObject("adodbconnection")
Set Rst = CreateObject("ADODBrecordset")
connOpen
"Provider=MicrosoftACEOLEDB120;Data Source="
& "D:/Databaseaccdb" ''后面是数据库路径 自己修改,看上去是07版本的,如果是03版本的将前面的字符串换成 "provider=MicrosoftjetOLEDB40;data source="
WITH Rst
Open "select from 基站信息 where 地点='" & [b3] & "'", conn, adOpenKeyset, adLockOptimistic
If RecordCount >0 Then
[B4]=FIELDS("经度")
[B5]=FIELDS("纬度")
[B6]=FIELDS("PROVINCE")
ELSE: MsgBox "找不到该名称"
End If
end with
connClose
Set conn = Nothing
End Sub
以上就是关于excel中vba怎么连接mysql数据库并展示数据全部的内容,包括:excel中vba怎么连接mysql数据库并展示数据、求教VBA连接SQL数据库的详细方法及解释。、请教如何在vba里同时运行多个update语句更新access数据库对应表等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)