【代码】EXCEL VBA操作ACCESS数据库,实现增删改查【基础篇】(免费)!
文摘
教育
2024-10-19 16:13
江苏
1、在工作表“操作”里,命令按钮点击事件,调用相应过程:Dim sql As String, tbl As String
Private Sub CmdAddNew_Click()
Call sqlInsert
End Sub
Private Sub CmdDelete_Click()
'//sql语句,删除除张三以外的所有记录
tbl = "tb员工"
sql = "Delete * from " & tbl & " Where 姓名<>'张三' "
Call sqlDelete(sql)
'//重置年龄=0
Call resetAge
End Sub
Private Sub CmdQuery1_Click()
'//查询所有记录
tbl = "tb员工"
sql = "select * from " & tbl
Call sqlQuery(sql)
End Sub
Private Sub CmdQuery2_Click()
'//查询所有记录
tbl = "tb员工"
sql = "select * from " & tbl
Call sqlQuery(sql)
End Sub
Private Sub CmdQuery3_Click()
'//查询姓名=张三的所有记录
tbl = "tb员工"
sql = "select * from " & tbl & " Where 姓名='张三'"
Call sqlQuery(sql)
End Sub
Private Sub CmdUpdate_Click()
'//更新年龄
tbl = "tb员工"
sql = "UPDATE " & tbl & _
" SET 年龄 = DateDiff('yyyy', 出生日期, Date()) - IIf(Format(出生日期, 'mmdd') > Format(Date(), 'mmdd'), 1, 0)"
Call sqlUpdate(sql)
End Sub
2、在myModule里,定义变量,sqlQuery过程,参数sql,根据sql语句查询数据:Dim cnn As Object
Dim rs As Object
Dim strCnn As String '//连接字符串
Dim dbs As String
Dim tbl As String
Dim sql As String
Dim temp()
Sub sqlQuery(sql As String)
'
Set cnn = CreateObject("ADODB.Connection")
'//ACCESS数据库文件路径
dbs = ThisWorkbook.Path & "\DataBase1019.accdb"
' '//表名
' tbl = "tb员工"
'
'
strCnn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbs
'//打开数据库连接
cnn.Open strCnn
' '//sql语句,选择tbl表中所有内容
' sql = "select * from " & tbl
Set rs = cnn.Execute(sql)
'//把记录集存到数组
temp = rs.GetRows
Sheet2.UsedRange.Clear
Sheet2.Range("A1").Resize(UBound(temp) + 1, UBound(temp, 2) + 1) = temp
'
rs.MoveFirst
'// 遍历结果集
Do Until rs.EOF
Debug.Print rs.Fields("姓名").Value
rs.MoveNext
Loop
'
rs.MoveFirst
'//把记录集输出到工作表
Sheet1.UsedRange.Clear
Sheet1.Range("A1").CopyFromRecordset rs
'
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing
MsgBox "查询成功!"
End Sub
3、在myModule里,sqlDelete过程,参数sql,根据sql语句删除指定条件的记录;sqlInsert过程,插入记录:Sub sqlDelete(sql As String)
'//创建连接对象
Set cnn = CreateObject("ADODB.Connection")
'//ACCESS数据库文件路径
dbs = ThisWorkbook.Path & "\DataBase1019.accdb"
'//表名
tbl = "tb员工"
'//创建连接字符串
strCnn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbs
'//打开数据库连接
cnn.Open strCnn
cnn.Execute (sql)
cnn.Close
Set cnn = Nothing
MsgBox "删除成功!"
End Sub
Sub sqlInsert()
'//创建连接对象
Set cnn = CreateObject("ADODB.Connection")
'//ACCESS数据库文件路径
dbs = ThisWorkbook.Path & "\DataBase1019.accdb"
'//表名
tbl = "tb员工"
'//创建连接字符串
strCnn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbs
'//打开数据库连接
cnn.Open strCnn
'//sql语句,插入新记录
sql = "Insert Into " & tbl & " (姓名, 出生日期, 部门, 住址) " & _
"VALUES ('李四', #1981-10-19#, '财务部', '江苏省南京市中山路1019号')"
cnn.Execute (sql)
cnn.Close
Set cnn = Nothing
MsgBox "插入成功!"
End Sub
4、在myModule里,resetAge过程,把年龄设置为0;sqlUpdate过程,参数sql,根据sql语句更新记录值:Sub resetAge()
'
Set cnn = CreateObject("ADODB.Connection")
'
dbs = ThisWorkbook.Path & "\DataBase1019.accdb"
'
tbl = "tb员工"
'
strCnn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbs
'
cnn.Open strCnn
'
sql = "update " & tbl & " set 年龄=0"
cnn.Execute (sql)
cnn.Close
Set cnn = Nothing
'
' MsgBox "年龄重置成功!"
End Sub
Sub sqlUpdate(sql)
'
Set cnn = CreateObject("ADODB.Connection")
'
dbs = ThisWorkbook.Path & "\DataBase1019.accdb"
'
tbl = "tb员工"
'
strCnn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbs
'
cnn.Open strCnn
cnn.Execute (sql)
cnn.Close
Set cnn = Nothing
MsgBox "更新成功!"
End Sub
| 安利小店 安利的牙膏非常不错,用了以后就不想再用其他的了;洗洁精、洗衣液也是日常必备,用过都说好! |
| 合谷医疗 合谷医疗专攻各种疑难杂症,尤其擅长抑郁症、焦虑失眠、儿童神经发育异常、多动症、自闭孤独症、腰颈椎疾病治疗,可谓神乎其技!体验过的直呼早点来就好了! |
喜欢就点个赞、点在看、留言评论、分享一下呗!感谢支持!