Public Function GetNextID(ByVal fldname As String, ByVal rstName As String) As Integer
Dim rstNextID As Recordset
Set rstNextID = gdbName.OpenRecordset("SELECT COUNT (" & fldname & ") FROM [" & rstName & "];")
GetNextID = IIf(IsNull(rstNextID.Fields(0)), 0, rstNextID.Fields(0)) + 1
Repeat:
Set rstNextID = gdbName.OpenRecordset("SELECT (" & fldname & ") FROM [" & rstName & "] WHERE (((" & fldname & ")=" & GetNextID & "));")
If rstNextID.RecordCount = 0 Then
GoTo CloseIt
Else
GetNextID = GetNextID + 1
GoTo Repeat
End If
CloseIt:
rstNextID.Close
End Function