【MySQL + VBA】从Access将excel数据insert到MySQL

6 篇文章 0 订阅
5 篇文章 0 订阅

MS Access, MySQL, Ado


一. Access连接到MySQL:

  1. 在本机状态下,用mysql(8.0)做后台,用Access(2019)做前端`
  2. 并在VBA界面中,点『运行』-『引用』-
    microsoft activex data objects 6.1 library,
    microsoft excel 16.0 object library,
连接MySQL的准备
Option Compare Database
Option Explicit

	Const MYDRIVER = "Driver={MySQL ODBC 8.0 Unicode Driver};"      '驱动程序
	Const MYSERVER = "Server=127.0.0.1;"                            '服务器
	Const MYPORT = "Port=3306;"                                     '端口
	Const MYDATABASE = "Database=abc;"                              'MySQL数据库名称
	Const MYUSER = "User=root;"                                     '用户名
	Const MYPASS = "PWD=123456789abcd;"                             'Mysql密码
	Public constr As String
ADO的定义

在Option Compare Database下

    Public cn As New ADODB.Connection                              
    Public rs As New ADODB.Recordset
组装连接MySQL的准备
	constr = MYDRIVER & MYSERVER & MYPORT & MYDATABASE & MYUSER & MYPASS
	cn.ConnectionString = constr
	cn.Open

二、Excel的准备

1. Excel的连接
    Dim EXL As New Excel.Application
    EXL.Workbooks.Open "C:\Users\Administrator\Desktop\mesa_入职.xlsx"
    EXL.ActiveWorkbook.Sheets(1).Select
2. Excel的取数范围
	Dim Crow As Long, Lrow As Long

    Lrow = Cells(Rows.Count, 1).End(xlUp).Row
    For Crow = Lrow To 2 Step -1

	Dim 地区 As String, 职级 As String
	Dim 入职日期 As String, 离职日期 As String

    地区 = Sheets("mesa_入职").Range("B" & Crow).Value
    职级 = Sheets("mesa_入职").Range("N" & Crow).Value    
    入职日期 = Sheets("mesa_入职").Range("F" & Crow).Value
    离职日期 = Sheets("mesa_入职").Range("G" & Crow).Value

三、SQL文(insert)

Dim sqlstr As String
    sqlstr = "INSERT INTO mesa_入职_临时(地区, 职级, 入职日期, 离职日期) " & _
            "VALUES('" & 地区 & "'," & _
                    "'" & 职级 & "'," & _
                    "'" & 入职日期 & "'," & _
                    "'" & 离职日期 & "')"

四、执行

Dim cm As New ADODB.Command
    cm.ActiveConnection = cn
    cm.CommandText = sqlstr
    cm.Execute
Next

五、释放内存

    Set cm = Nothing
    cn.Close: Set cn = Nothing

五、完整代码

Option Compare Database
Option Explicit

'连接MySQL的准备
	Const MYDRIVER = "Driver={MySQL ODBC 8.0 Unicode Driver};"      '驱动程序
	Const MYSERVER = "Server=127.0.0.1;"                            '服务器
	Const MYPORT = "Port=3306;"                                     '端口
	Const MYDATABASE = "Database=abc;"                              'MySQL数据库名称
	Const MYUSER = "User=root;"                                     '用户名
	Const MYPASS = "PWD=123456789abcd;"                             'Mysql密码
	Public constr As String

'ADO的定义    	
	Public cn As New ADODB.Connection                              
	Public rs As New ADODB.Recordset

Private Sub Command0_Click()

'组装连接MySQL的准备
	constr = MYDRIVER & MYSERVER & MYPORT & MYDATABASE & MYUSER & MYPASS
	cn.ConnectionString = constr
	cn.Open
	
'Excel的准备
	'1. Excel的连接	
	Dim EXL As New Excel.Application
	EXL.Workbooks.Open "C:\Users\Administrator\Desktop\mesa_入职.xlsx"
	EXL.ActiveWorkbook.Sheets(1).Select
			
	'2. Excel的取数范围
	Dim Crow As Long, Lrow As Long

    Lrow = Cells(Rows.Count, 1).End(xlUp).Row
    For Crow = Lrow To 2 Step -1
    
	Dim 地区 As String, 职级 As String
	Dim 入职日期 As String, 离职日期 As String

    地区 = Sheets("mesa_入职").Range("B" & Crow).Value
    职级 = Sheets("mesa_入职").Range("N" & Crow).Value
    
    入职日期 = Sheets("mesa_入职").Range("F" & Crow).Value
    离职日期 = Sheets("mesa_入职").Range("G" & Crow).Value

'SQL文(insert)

	Dim sqlstr As String
    sqlstr = "INSERT INTO mesa_入职_临时(地区, 职级, 入职日期, 离职日期) " & _
            "VALUES('" & 地区 & "'," & _
                    "'" & 职级 & "'," & _
                    "'" & 入职日期 & "'," & _
                    "'" & 离职日期 & "')"
'执行								
Dim cm As New ADODB.Command
    cm.ActiveConnection = cn
    cm.CommandText = sqlstr
    cm.Execute
Next

'释放内存
    Set cm = Nothing
    cn.Close: Set cn = Nothing
end sub  	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Allan_lam

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值