%@Language="vbscript" %>
Remove Duplicate Records
<%
Const adOpenForwardOnly = 0
Const adLockReadOnly = 1
Const adCmdTableDirect = &H0200
Const adCmdText = &H0001
sPassword = "admin" ' fill in your desired password here
If Request("password") <> "" Then
Session("Password") = Request("password")
End If
If Session("Password") <> sPassword Then
Response.Write ""
Else
Dim sdbPath
Dim sdbTable
Dim spriKey
Dim sdbField
Dim strSQL
Dim strConn
Dim rs
Dim ttCount
Dim i
Dim totalFound
Dim lookFor
Dim tempVal
Dim foundDups
Dim tempArr
Dim prCount
Dim File
Dim Posi
Dim delDub
delDub = False
File = Request.ServerVariables("SCRIPT_NAME")
File = StrReverse(File)
Posi = InStr(File, "/")
File = Left(File, Posi - 1)
File = StrReverse(File)
If Request.Form("db") <> "" Then
On Error Resume Next
DSNName = Request.Form("db")
sdbTable = Request.Form("tb")
spriKey = Request.Form("pk")
sdbField = Request.Form("fn")
sdbPath = "DBQ=" & Server.MapPath(DSNName) & ";Driver={Microsoft Access Driver (*.mdb)};"
strSQL = "SELECT COUNT(*) AS ttCount FROM " & sdbTable
Set strConn = Server.CreateObject("ADODB.Connection")
strConn.Open sdbPath
Set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorLocation = adUseClient
rs.Open(strSQL), strConn, adOpenForwardOnly, adLockReadOnly, adCmdText
Set rs = strConn.Execute(strSQL)
tempArr = ""
totalFound = rs("ttCount")
rs.Close
Set rs = Nothing
totalFound = CInt(totalFound)
Response.Write totalFound
Response.Write " records" & vbCrLf
Response.Write "
" & vbCrLf
strSQL = "SELECT " & sdbField & "," & spriKey & " FROM " & sdbTable & " ORDER BY " & sdbField
Set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorLocation = adUseClient
rs.Open(strSQL), strConn, adOpenForwardOnly, adLockReadOnly, adCmdText
If Err = 0 Then
For i = 0 To (totalFound - 1)
rs.MoveFirst
rs.Move(i)
lookFor = rs.fields(sdbField)
If lookFor = tempVal Then
foundDups = rs.fields(spriKey)
tempArr = tempArr & foundDups & ","
End If
tempVal = lookFor
Next
rs.Close
Set rs = Nothing
If tempArr <> "" Then
Response.Write "Primary Key ID containing duplicate data in the selected field:" & vbCrLf
Response.Write "
" & vbCrLf
End If
If tempArr = "" Then
Response.Write "No duplicate records found." & vbCrLf
Else
tempArr = Left(tempArr, Len(tempArr) - 1)
Response.Write tempArr
tempArr = Split(tempArr, ",", -1, 1)
prCount = UBound(tempArr)
prCount = prCount + 1
Response.Write "
" & vbCrLf
For i = 0 To (prCount - 1)
strSQL = "DELETE FROM " & sdbTable & " WHERE " & spriKey & " = " & tempArr(i)
strConn.Execute(strSQL)
Next
End If
delDub = True
Else
Response.Write Err.description
End If
strConn.Close
Set strConn = Nothing
If Err = 0 And delDub = True And tempArr <> "" Then
Response.Write "
" & prCount & " duplicate record"
If prCount <> 1 Then
Response.Write "s"
End If
Response.Write " deleted successfully." & vbCrLf
End If
End If
%>
Remove duplicate database records
<%
End If
%>