Excel中VBA编程学习笔记(⼗五)--Access数据库删除操作【例1】使⽤Connection对象的Execute⽅法删除数据
数据库格式:
Sub test()
Dim conString$, sqlString$
Dim cnn, rst
Set cnn = CreateObject("ADODB.Connection")
Set rst = CreateObject("ADODB.Recordset")
Dim i%, sex$, Address$, Name$, birthDay$
conString = "provider=Microsoft.ace.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\test.accdb;"
cnn.Open conString
sqlString = "delete from students where sName='张三'"
cnn.Execute sqlString
MsgBox "删除成功"
cnn.Close
End Sub
【例2】使⽤Recordset删除数据
数据表字段如下:
Sub exercise()
Dim cnn ', rst
Set cnn = CreateObject("tion")
'Set rst = CreateObject("dset")
Dim rst As New ADODB.Recordset
Dim sqlStr$, conStr$
vba数据库编程conStr$ = "provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\test.mdb"
sqlStr = "select * from students where 总分 <161"
cnn.Open conStr$
rst.Open sqlStr, cnn, adOpenDynamic, adLockOptimistic Do Until rst.EOF
rst.Delete
rst.MoveNext
Loop
cnn.Close
End Sub
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论