VB ACCESS数据库清空
我是一个菜鸟,想作一个菜单“清空”数据库中的数据,如何写程序代码(我不知道清空数据库的命令怎么写),谢谢大虾们!!...
我是一个菜鸟,想作一个菜单“清空”数据库中的数据,如何写程序代码(我不知道清空数据库的命令怎么写),谢谢大虾们!!
展开
2个回答
展开全部
窗体上画一command,数据库为a ,表名为b
Private Sub Command1_Click()
Dim msgQk
msgQk = MsgBox("清空数据库将清除所有数据!", 32 + vbOKCancel, "请选择")
If msgQk = 1 Then
Dim cnDel As New ADODB.Connection
Dim rDel As New ADODB.Recordset
Dim sqlDel1 As String
cnDel.Open "DBQ=" & App.Path & "\a.mdb;Driver={Microsoft Access Driver (*.mdb)};"
sqlDel1 = "delete * from b"
cnDel.Execute sqlDel1
MsgBox "数据库清空完成!", 32, "完成"
cndel.close
End If
End Sub
Private Sub Command1_Click()
Dim msgQk
msgQk = MsgBox("清空数据库将清除所有数据!", 32 + vbOKCancel, "请选择")
If msgQk = 1 Then
Dim cnDel As New ADODB.Connection
Dim rDel As New ADODB.Recordset
Dim sqlDel1 As String
cnDel.Open "DBQ=" & App.Path & "\a.mdb;Driver={Microsoft Access Driver (*.mdb)};"
sqlDel1 = "delete * from b"
cnDel.Execute sqlDel1
MsgBox "数据库清空完成!", 32, "完成"
cndel.close
End If
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
下面的代码放在模块里
Private mConn As New ADODB.Connection
Public Sub OpenConnection(ByVal FileName As String)
With mConn
If .State <> adStateOpen Then
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileName & ";Persist Security Info=False"
.Open
End If
End With
End Sub
Public Function OpenRecordset(ByVal strSql As String) As ADODB.Recordset
Dim rs As New ADODB.Recordset
With rs
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.Open strSql, mConn, , , adCmdText
End With
Set OpenRecordset = rs
End Function
Public Function RunTrans(ByVal tranSql As String)
mConn.BeginTrans
mConn.Execute tranSql
mConn.CommitTrans
End Function
代码
Private Sub Form_Load()
OpenConnection App.Path & "\Test.mdb"
Dim strSql As String
strSql = "delete from AddressList"
RunTrans strSql
End Sub
Private mConn As New ADODB.Connection
Public Sub OpenConnection(ByVal FileName As String)
With mConn
If .State <> adStateOpen Then
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileName & ";Persist Security Info=False"
.Open
End If
End With
End Sub
Public Function OpenRecordset(ByVal strSql As String) As ADODB.Recordset
Dim rs As New ADODB.Recordset
With rs
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.Open strSql, mConn, , , adCmdText
End With
Set OpenRecordset = rs
End Function
Public Function RunTrans(ByVal tranSql As String)
mConn.BeginTrans
mConn.Execute tranSql
mConn.CommitTrans
End Function
代码
Private Sub Form_Load()
OpenConnection App.Path & "\Test.mdb"
Dim strSql As String
strSql = "delete from AddressList"
RunTrans strSql
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询