要用vba删除access数据库中表的记录,可以有如下步骤:
- 用ADODB.Recordset数据集对象连接打开access数据库;
- 用SQL DELETE 语句删除数据库中的表。
代码如下:
Sub QQ1722187970() Const adOpenForwardOnly = 0 Const adOpenKeyset = 1 Const adOpenDynamic = 2 Const adOpenStatic = 3 Const adOpenUnspecified = -1 Const adLockReadOnly = 1 Const adLockPessimistic = 2 Const adLockOptimistic = 3 Const adLockBatchOptimistic = 4 Const adLockUnspecified = -1 Dim oRecordSet As Object Set oRecordSet = CreateObject("ADODB.Recordset") Dim sConstr As String Dim sPath As String Dim sTableName As String Dim sDataBase As String Dim sSql As String Dim oWK As Worksheet Set oWK = Excel.ActiveSheet '要导入的Access数据库中的表名 sTableName = oWK.Name '要导入的Access文件名称 sDataBase = "数据库" sPath = Excel.ThisWorkbook.Path & "\" sSql = "DELETE * FROM " & sTableName sVersion = Excel.Application.Version '创建连接字符串 If sVersion <= 12 Then sConstr = "Provider='Microsoft.Jet.OLEDB.4.0';Data Source=" & sPath & sDataBase & ".accdb" Else sConstr = "Provider='Microsoft.ACE.OLEDB.12.0';Data Source=" & sPath & sDataBase & ".accdb" End If With oRecordSet .Open sSql, sConstr MsgBox "删除完毕!" End With Set oRecordSet = Nothing Set oWK = Nothing End Sub
发表评论