Actually, the above (by
raskew) is somewhat different form the direction outllined in my earlier post. I had thought that the outline was sufficiently clear to implement and did not consider the necessity of posting a complete (code) soloution. Since this has (obviously) failed and [/b]raskew[/b] has done the majority of the real work, I provide a modification to his procedure more in keeping with the outline previously submitted.
I have made a few cosmetic changes, just to maintain soem personal style conventions (var names etc) and modified the dynamic SQL statements to accept the recordset and field names as argumnets instead of relying on the existance of specific table and field names within the procedure for the recordset for the list of "missing" values. The other 'significant' change is the creation of the (permanant) table
tblSeqNums with the single field
SeqNum which 'holds' the set of sequence values between the low and hi values found. This is, I believe' necessary for at least two reasons.
First, the user will probably want to do SOMETHING with or about the elements and the simple message box display provides little opportunity to work with or save the result. Athough the code is obviously intended as a simple illustration of a technique and it was predicated on a limited number of 'items' being returned, it is not even verifiable without some 'pencil work' to record what the procedure has returned.
Secondly, the message box can rapidly expand to an unmanageable size for a large number of missing items. It can actually easily expand to greater then the screen height, maing even the dismissal of the message box itself difficult.
The other area of change is, of course, the actual display of the results (the "UnMatched" query), as the replacement for the message box. I have included the SQL string for a SAMPLE of such a query generated by using the wizzard. It, of couse, refers to a table and field (
tblIdNum.IdNumAsText which I generated just for the purpose of this exercise. It is easy to generate the sql string in code, or equally simple to simply use the wizzard to generate another query whenever desired, so the sql strin is JUST to illustrate the general syntax and is not necessary for the procedure,
Code:
Function basSeqGapsMR(MyRs As String, MyFld As String) As Boolean
'Adapted from raskew's post to Tek-tips thread701-511412 _
to find (and "Display") missing numbers from a sequence
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
Dim LoEnd As Long
Dim HiEnd As Long
Dim RecCount As Long
Dim RecNum As Long
Dim RecHold As Long
Dim NL As String
Dim MyMsg
Dim Quo As String * 1
Dim Idx As Long
Quo = Chr(34)
NL = Chr(13) & Chr(10) ' Define newline.
'Start the clock
a2kuStartClock
Set dbs = CurrentDb
strSQL = "SELECT Min(" & MyRs & "." & MyFld & ") AS MyLo, " & _
"Max(" & MyRs & "." & MyFld & ") AS MyHi, " & _
"Count(" & MyRs & "." & MyFld & ") AS MyCnt, " & _
"[MyHi]-[MyLo]+1 AS MyNumRecs" & _
" FROM " & MyRs & ";"
Set rst = dbs.OpenRecordset(strSQL)
LoEnd = rst!MyLo
HiEnd = rst!MyHi
RecCount = rst!MyCnt
RecNum = rst!MyNumRecs
RecHold = LoEnd
strSQL = "Delete * From tblSeqNums;"
dbs.Execute strSQL
Set rst = dbs.OpenRecordset("tblSeqNums", dbOpenDynaset)
Idx = RecHold
While Idx <= HiEnd
With rst
.AddNew
!SeqNum = Idx
.Update
End With
Idx = Idx + 1
Wend
'This is just the SQL statement for the Query to Display the Results, _
the Missing values in the sequence
strSQL = "SELECT tblSeqNums.SeqNum " & _
"FROM tblSeqNums " & _
"LEFT JOIN tblIdNum ON tblSeqNums.SeqNum = tblIdNum.IdNumAsText " & _
"WHERE (((tblIdNum.IdNumAsText) Is Null));"
'Stop the clock and display the results
MyMsg = "This procedure executed in: " & a2kuEndClock & " milliseconds"
MsgBox MyMsg, vbInformation + vbOKOnly, "Find Missing OrderID's"
basSeqGapsMR = True
End Function
MichaelRed
m.red@att.net
Searching for employment in all the wrong places