Public Sub CreateHistogram(TableName As String, FieldName As String, Optional Bins As Integer = -1)
Dim min As Double
Dim max As Double
Dim range As Double
Dim observations As Long
Dim inc As Double
Dim i As Double
Dim strSql As String
Dim binStart As Double
Dim ObsInBin As Long
min = DMin(FieldName, TableName)
max = DMax(FieldName, TableName)
max = 1.05 * max
range = max - min
If Bins = -1 Then
observations = DCount("*", TableName, FieldName & " is not Null")
Bins = 1 + 3.22 * Log(observations)
End If
inc = range / Bins
Debug.Print "Bins " & Bins & " Range " & range & " obs " & observations & " inc " & inc
strSql = "delete * from tblHistogram"
CurrentDb.Execute strSql
binStart = min
For i = (min + inc) To max Step inc
ObsInBin = DCount("*", TableName, FieldName & " between " & binStart & " AND " & i)
Debug.Print "start " & binStart & " end " & i & " obs " & ObsInBin
strSql = "Insert into tblHistogram (BinStart, BinEnd, NumberObservations) values (" & binStart & ", " & i & ", " & ObsInBin & ")"
CurrentDb.Execute strSql
binStart = i
Next i
End Sub