'需要的参数
Const MYPROVIDER = "Provider=sqloledb;"
Const MYSERVER = "Server=xxx.xxx.xxx.xxx;"
Const MYPORT = "Port=3306;"
Const MYDATABASE = "Database=xxxxxxx;"
Const MYUSER = "User ID=xxxxxxx;"
Const MYPASS = "Password=xxxxxxx;"
Public constr As String
Public cn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public cm As New ADODB.Command
constr = MYPROVIDER & MYSERVER & MYPORT & MYDATABASE & MYUSER & MYPASS
cn.ConnectionString = constr
'连接数据库
cn.Open
sql = "......."
'执行sql语句
cm.ActiveConnection = cn
cm.CommandText = sql
Set rs = cm.Execute '有结果返回的就在rs中,没有结果的就不需要用rs捕获单纯 cm.Execute即可
Set cm = Nothing
'关闭数据库
cn.Close
标签:VBA,Const,cn,cm,rs,数据库,xxx,Public,连接
From: https://www.cnblogs.com/tutuleilife/p/17486141.html