【问题标题】:Delete all records from table - doCMD.RunSQL从表中删除所有记录 - doCMD.RunSQL
【发布时间】:2014-03-04 13:47:42
【问题描述】:

我希望在向其添加新数据之前清除所有记录的本地表。我正在尝试使用 doCMD.RunSQL 命令执行此操作,但一直收到运行时错误,我猜是因为它位于打开的连接中,我不确定如何执行此操作。

任何帮助表示赞赏。

谢谢

Sub GetUsers()
    Dim oConnection As Object
    Dim oSheet As Object
    Dim oCell As Object
    Set oConnection = CreateObject("ADODB.Connection")
    Dim strDBPath As String
    strDBPath = "C:/Users/stevemcco/Desktop/Users.accdb"
    Dim sConn As String
    sConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                        "Data Source=" & strDBPath & ";" & _
                        "Jet OLEDB:Engine Type=5;" & _
                        "Persist Security Info=False;"

    oConnection.Open sConn

    DoCmd.RunSQL "Delete * from Table1"

    For Each oSheet In ThisWorkbook.Sheets
        For Each oCell In oSheet.Columns(1).Cells
            If oCell.Value = "" Then
                Exit For
            End If

            If (oCell.Row > 1) Then 'Jumps the header
                oConnection.Execute " Insert Into Table1(ID,Area) " _
                & " VALUES ('" & oCell.Value & "','" & oSheet.Name & "')"
            End If

        Next
    Next
    oConnection.Close
    Set oConnection = Nothing
End Sub

【问题讨论】:

  • Excel 中没有 DoCmd 对象。
  • 我必须添加对 MS DAO 3.6 对象库的引用才能使其可见。请注意,仍然无法正常工作。

标签: sql excel vba ms-access ms-access-2013


【解决方案1】:

对于您将使用的本地数据库:CurrentDb.Connection.Execute "DELETE * FROM Table1"

在你的情况下使用:oConnection.Execute "DELETE * FROM Table1"

【讨论】:

  • 如果我想查询这个新表数据并将任何重复的数据返回到我的主 Excel 工作表中,我该怎么做?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-07
  • 2013-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
相关资源
最近更新 更多