从查询vba中删除重复记录

时间:2015-09-04 10:49:22

标签: ms-access access-vba

我已经为重复记录构建了一个查询。我需要运行一些VBA代码来检查记录是否已经在表中(查询)。如果是这样,我需要删除最后一条记录。

我的表单由一个带有值的按钮组成,这样当您单击按钮时,数据就会插入到表中

Dim qry As QueryDef
Set qry = CurrentDb.QueryDefs("duplicate records")
'which method do i use to see if the query got duplicate record'

With rstCategories
.MoveLast 0
End With
With rstCategories
rstCategories.Delete
End With

MsgBox "The problem already reported before!"

2 个答案:

答案 0 :(得分:1)

我要做的是在你的桌子上快速查询:

Dim db as Database
Dim rec as Recordset

Set db = CurrentDB
Set rec = db.OpenRecordset ("SELECT * FROM MyTable WHERE MyValue = '" & Me.MyValue & "'")

If rec.EOF = true then
  'No match found, so the value isn't in the table.  Add it.
Else
  'Match found.  This value is already in the table.
  MsgBox "This value is already in the table"
End If

答案 1 :(得分:1)

好吧我解决了  我使用find duplicates查询向导创建了一个查询。

then i insert this code in "form close"
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = Application.CurrentDb
Set rs = db.OpenRecordset("qry_Duplicates")


Do Until rs.EOF
rs.MoveFirst
rs.delete
rs.Close
Set rs = Nothing
Set rs = db.OpenRecordset("qry_Duplicates")
Loop

此代码删除整行