Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从表-doCMD.RunSQL中删除所有记录_Sql_Excel_Vba_Ms Access_Ms Access 2013 - Fatal编程技术网

从表-doCMD.RunSQL中删除所有记录

从表-doCMD.RunSQL中删除所有记录,sql,excel,vba,ms-access,ms-access-2013,Sql,Excel,Vba,Ms Access,Ms Access 2013,我希望在添加新数据之前清除所有记录的本地表。我正试图使用doCMD.RunSQL命令来执行此操作,但由于它位于打开的连接中,我一直收到运行时错误,我猜是因为它位于打开的连接中,我不确定如何执行此操作 谢谢你的帮助 谢谢 Sub GetUsers() Dim oConnection As Object Dim oSheet As Object Dim oCell As Object Set oConnection = CreateObject("ADODB.Conn

我希望在添加新数据之前清除所有记录的本地表。我正试图使用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

对于本地数据库,您将使用:
CurrentDb.Connection.Execute“DELETE*fromtable1”


在您的示例中,请使用:
occonnection.Execute“DELETE*FROM Table1”

Excel中没有DoCmd对象。我必须为MS DAO 3.6对象库添加引用以使其可见。如果我想查询这个新的表数据并将任何重复的数据返回到我的主excel表中,我该怎么做呢?