you're welcome, but on doing further testing i found that my solution was
pretty poor. besides the issue you pointed out, the code kept producing
error 3052:
"File shairng lock count exceeded. Increase MaxLocksPerFile registry entry."
The default number of records that can be updated in a recordset is 9500,
per Help. so while it's possible to update the table by looping through
recordsets, handling the error when it occurs, it's not efficient.
i ended up taking my recordset idea, and incorporating Ken's Update query
solution, and came up with the following, which updates a table with 26,000
records in 0-1 seconds:
'this is a global variable
Public lngCount As Long
Public Function isIncrementedCount(ByVal x As Variant) As Long
'the "x" variable is never used in the function. but you have
'to pull a value from each record in the query, or else
'the function will only be called once - in the first record
'in the query - and all your records will be assigned a value
'of 1.
lngCount = lngCount + 1
isIncrementedCount = lngCount
End Function
Public Sub AssignNumbers()
Dim strSQL1 As String, strSQL2 As String, strVal As String
Dim rst As DAO.Recordset
'the next two variables are only used to time the process.
'you can comment them out anytime - and the code they're used in.
Dim dtBegin As Date, dtEnd As Date
dtBegin = Now
strSQL1 = "SELECT MyDupField FROM MyTable " _
& "WHERE MyDupField Is Not Null " _
& "GROUP BY MyDupField"
Set rst = CurrentDb.OpenRecordset(strSQL1, dbOpenDynaset)
rst.MoveFirst
Do
lngCount = 0
strVal = rst("MyDupField")
'note: you can use any field in the isIncrementedCount function call,
'it doesn't have to be MyDupField.
strSQL2 = "UPDATE MyTable SET MyCountField = " _
& "isIncrementedCount([MyDupField]) " _
& "WHERE MyDupField='" & strVal & "'"
CurrentDb.Execute strSQL2, dbFailOnError
rst.MoveNext
Loop Until rst.EOF
rst.Close
Set rst = Nothing
dtEnd = Now
MsgBox DateDiff("s", dtBegin, dtEnd)
End Sub
again, replace MyTable, MyDupField, and MyCountField everywhere in the code,
with the correct names.
the caveat is that the above code will not process records where MyDupField
is null, or is a zero-length string. if you *may* encounter either scenario
at any time, post back and i'll work up an alternate solution that includes
processing those records too. (shouldn't be hard, i just don't want to
invest the extra time unless needed.)
hth
sharman said:
Thanks Tina, for the quick response. I don't know how long this procedure
will take because I have 25000 records but if "MyDupField" is sorted
ascending, are some changes possible to this code so that it doesn't search
for the same value till it reaches the end and start all over from the
beginning. If there is a way to do this I think it will decrease the time to
complete the procedure. Thanks again.