从excel vba中删除.mdb数据库中的重复行

时间:2016-09-03 15:56:39

标签: database excel vba ms-access

我的代码到目前为止: (在alt语句中注意错误......)

Sub Test()
    DeleteDuplicates_Database "Mytable", ThisWorkbook.Path & "\DATA.mdb"
End Sub

Sub DeleteDuplicates_Database(sData_Sht_Table As String, sPathToDatabase As String)
    ' delete duplicate records in a database table
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim sSQL$

    Application.ScreenUpdating = False

    'Open connection to database
    Set db = OpenDatabase(sPathToDatabase)
    Set rs = db.OpenRecordset(sData_Sht_Table)

    sSQL = "ALTER IGNORE TABLE " & sData_Sht_Table & " ADD UNIQUE (EventID)"

    db.Execute sSQL

    db.Close
    Set rs = Nothing
    Set db = Nothing
    Application.ScreenUpdating = True

End Sub

2 个答案:

答案 0 :(得分:1)

我不是DAO的农场主,但我会用ADO做这件事。

strSQL = "SELECT DISTINCT * INTO new_table FROM " & sData_Sht_Table & " ;"
AdoConnection.Execute strSQL

注意:未经测试。

答案 1 :(得分:1)

  

我也看到过这样的事情:

     

SELECT DISTINCT * INTO TempTable FROM OriginalTable

     

(但我也无法让它工作,它只是复制了所有   行)

然后你没有欺骗。就这么简单。