ClaraBarton
Registered User.
- Local time
- Yesterday, 16:13
- Joined
- Oct 14, 2019
- Messages
- 468
I copied this sub from somewhere a long time ago. I'd like to add a column to find the highest ID in all the tables. All ID fields are the first field but all have different names. Like BooksID and BookAuthorsID and PublisherID. My problem is getting the DMax (MxID) line to work. I've tried many things and think I could be close but would appreciate any help.
Code:
Public Sub sCountRecords()
On Error GoTo Error_Handler
Dim db As dao.Database
Dim tdf As dao.TableDef
Dim rst As dao.Recordset
Dim rstCount As dao.Recordset
Dim i As Integer
Dim MxID As Long
Dim strSQL As String
Set db = CurrentDb
strSQL = "DELETE * FROM tblRecordCounts;"
db.Execute strSQL
Set rstCount = db.OpenRecordset("tblRecordCounts", dbOpenDynaset)
For i = 0 To db.TableDefs.Count - 1
Set tdf = db.TableDefs(i)
If Mid(tdf.Name, 2, 3) <> "sys" Then
If tdf.Name <> "tblRecordCounts" Then
Set rst = db.OpenRecordset(tdf.Name, dbOpenSnapshot)
MxID = DMax(tdf.Name & ".Fields(0)", tdf.Name)
If Not rst.EOF Then
rst.MoveLast
With rstCount
.AddNew
!TableName = tdf.Name
!recCount = rst.RecordCount
!MaxID = MxID
.Update
End With
Else
With rstCount
.AddNew
!TableName = tdf.Name
!recCount = 0
!MaxID = MxID
.Update
End With
End If
rst.Close
Set rst = Nothing
End If
End If
Next i
Exit_Here:
On Error Resume Next
Set tdf = Nothing
rstCount.Close
Set rstCount = Nothing
MsgBox "Done!"
Error_Handler:
Resume Exit_Here
End Sub