在模块中自定义函数:ExistDuplicateRecords(Expr As String, Domain As String) As Boolean
调用时第一个参数是要查重复值的字段名,第二个参数是表或查询名称。有重复值时返回True。
'函数名称:ExistDuplicateRecords(参数1,参数2)
'函数功能:检查一个表中某字段是否有重复值
'参数:函数有两个参数,参数1为想要检查的字段名,参数2为要查询的表名或查询名称
'返回值:如果字段(参数1)在表(参数2)中有重复值,函数返回True,否则返回False
Public Function ExistDuplicateRecords(Expr As String, Domain As String) As Boolean
On Error Resume Next
Dim cnn As Object 'ADODB.Connection
Dim rst As Object 'ADODB.Recordset
Dim strSQL As String
Set cnn = CurrentProject.Connection
strSQL = "Select * FROM " & Domain & _
" Where " & Expr & _
" In (Select " & Expr & _
" From " & Domain & _
" Group By " & Expr & _
" Having Count(*)>1);"
Set rst = OpenADORecordset(strSQL, , cnn)
If rst.recordCount > 0 Then
ExistDuplicateRecords = True
End If
rst.Close
Set cnn = Nothing
Set rst = Nothing
End Function