In the form's module:
----------------------------------------------------
Private Sub cmdCheck_Click()
Dim rst As DAO.Recordset
Dim sql As String
Dim rstParsed As DAO.Recordset
If Not (IsNumeric(txtEnd) And IsNumeric(txtStart)) Then
MsgBox "Enter only numbers for the ticket range"
Exit Sub
End If
If CLng(txtEnd) < CLng(txtStart) Then
Dim temp As Long
temp = txtEnd
txtEnd = txtStart
txtStart = temp
End If
sql = "SELECT tblSOs.TechID, tblSOs.SvcOrder FROM tblSOs " & _
"WHERE tblSOs.SvcOrder BETWEEN " & txtStart & " AND " & txtEnd
'this recordset will give us a list of numbers and the techs
'to whom the numbers have been assigned. we are getting the
'parameters from two textboxes on the form
Set rst = CurrentDb.OpenRecordset(sql)
'if there are numbers in that range then they would
'conflict with the assigning of new numbers to a new
'tech
If rst.RecordCount Then
Label22.Caption = "Records overlap"
'numbers have been found. let's parse out the numbers
'in the range format (#-#, #, #-#, ...)
'this is basically an aggregate function for the records
'i do not know of a way to implement this in sql directly
'using an inline function.
Set rstParsed = parseNumbersRstTech(rst, 0, 1)
rstParsed.MoveFirst
Set Me.Form4.Form.Recordset = rstParsed
Else
Label22.Caption = "Free for use"
Set Me.Form4.Form.Recordset = Nothing
End If
cmdCheck.SetFocus
End Sub
In the main module:
----------------------------------------------
Public Function parseNumbersRstTech(rst As DAO.Recordset, _
techCol As Integer, numCol As Integer) As DAO.Recordset
Dim rstOut As DAO.Recordset
Dim records As String
Dim cur As Long
Dim st As Long
Dim srt As String
Dim tech As Long
Dim tempTable As TableDef
'========================================================
'this function requires that a recordset be passed in containing
'one column with techids and another column with the svcorders
'as well as two zero based values indicating which columns contain
'the required fields it will then return a recordset containing a
'row for each tech and a string field with the parsed numbers
'========================================================
'get sort information to keep for replacing sort
srt = rst.Sort
rst.Sort = rst.Fields(techCol).Name & "," & rst.Fields(numCol).Name
Set rst = rst.OpenRecordset
'create temporary tabledef to return a recordset to the calling
'function that holds the required information
On Error Resume Next
Set tempTable = CurrentDb.TableDefs("TemporaryTableDefForparseNumbersRstTech")
If Err.number <> 0 Then
Set tempTable = CurrentDb.CreateTableDef("TemporaryTableDefForparseNumbersRstTech")
tempTable.Fields.Append tempTable.CreateField("TechID", dbLong)
tempTable.Fields.Append tempTable.CreateField("SvcOrderRange", dbMemo)
CurrentDb.TableDefs.Append tempTable
Err.Clear
Else
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE FROM TemporaryTableDefForparseNumbersRstTech"
DoCmd.SetWarnings True
End If
On Error GoTo 0
'open a recordset based on that table to return to the calling
'function
Set rstOut = CurrentDb.TableDefs("TemporaryTableDefForparseNumbersRstTech") _
.OpenRecordset
'#BEGIN Un-important stuff to parse out (aggregate) the records
'============================================================
If rst.RecordCount Then
tech = rst(techCol)
st = rst(numCol)
cur = st
rst.MoveNext
While Not rst.EOF
If tech = rst(techCol) Then
If rst(numCol) <> cur Then
If rst(numCol) <> cur + 1 Then
If st = cur Then
records = records & st & ", "
Else
records = records & st & "-" & cur & ", "
End If
st = rst(numCol)
cur = rst(numCol)
Else
cur = rst(numCol)
End If
End If
Else
If st = cur Then
records = records & st
Else
records = records & st & "-" & cur
End If
rstOut.AddNew
rstOut(0) = tech
rstOut(1) = records
rstOut.Update
records = ""
tech = rst(techCol)
st = rst(numCol)
cur = st
End If
rst.MoveNext
Wend
If st = cur Then
records = records & st
Else
records = records & st & "-" & cur
End If
rstOut.AddNew
rstOut(0) = tech
rstOut(1) = records
rstOut.Update
If srt <> "" Then
rst.Sort = srt
Set rst = rst.OpenRecordset
End If
rstOut.MoveFirst
'#END
'============================================================
'this line will fail. i cannot delete the tabledef that a
'recordset was created from while the recordset exists
'there is a definite relationship between the two
'CurrentDb.TableDefs.Delete "TemporaryTableDefForparseNumbersRstTech"
'if this assignment is not made with the .OpenRecordset function
'then it will fail when the recordset is assigned in the calling
'form's assignment: Set Me.subform.recordset = parseNumbersRstTech
Set parseNumbersRstTech = rstOut.OpenRecordset
End If
End Function
A TableDef is a DAO object that represents a "table" of some sort (generally a database table). In general, you won't really use that to work with data, but instead you'd use that when you need to work with the "physical" table object (like if you you need to relink the tables, for example).
A DAO Recordset is a set of data which can be derived from the contents of a Table (a TableDef really has no bearing on a DAO Recordset).
<what relationship does the recordset returned by OpenRecordset have to the recordset it was called on>
I'm not sure I follow you. OpenRecordset "returns" a DAO.Recordset object. It's not dependent on another Recordset - it's built by DAO and is the return item from the OpenRecordset method. Your call here:
Set Me.Recordset = rstResults.OpenRecordset
Is basically saying "Open a Recordset named 'rstResults', and then set my form's Recordset to that Recordset". You could also do this:
Dim rstRestuls As DAO.Recordset
Set rstResults = DAO.OpenRecordset("SELECT * FROM SomeTable")
Set Me.Recordset = rstResults