Joe, thank you for the example. It works for the original recordset returned but I get an error when trying to use a fabricated recordset. I needed to add some additional data to the original recordset so I created another recordset with the new data and tried to equate to the Form recordset and the error I get is:
The object you entered is not a valid recordset property.
Here is my code in case you can see a problem with my thought pattern on this.
Option Compare Database
Option Explicit
Public recCount As Integer
Public rs1 As New ADODB.Recordset
Private Sub Form_Open(Cancel As Integer)
Dim cn As New ADODB.Connection, Sql1 As String
Dim rs As New ADODB.Recordset
Set cn = CurrentProject.Connection
'--- Get recordset from the database
Sql1 = "select * from IDTable order by ID, startdate"
rs.Open Sql1, cn, adOpenStatic, adLockOptimistic
rs.MoveFirst
recCount = rs.RecordCount
'--- Build an internal recordset to manipulate data
Call CreateRecordset(rs)
Dim yy As Form
Set yy = Forms!frm_IDTable.Form
Set yy.Recordset = rs
'--
Debug.Print "form name = "; yy.Name
Debug.Print "rs1 = "; rs1.RecordCount
Set yy.Recordset = rs1
''--------ERROR ON rs1 AT THIS POINT. rs will work okay.
rs.Close
Set rs = Nothing
End Sub
Public Function CreateRecordset(rs As ADODB.Recordset)
'-- create a fabricated recordset
With rs1.Fields
.Append "ID", adInteger
.Append "startdate", adDBDate
.Append "enddate", adDBDate
.Append "gap", adInteger, adFldIsNullable
.Append "gapNum", adInteger, adFldIsNullable
End With
Dim arrFields As Variant
arrFields = Array("ID", "startdate", "enddate", "gap", "gapNum"
Dim indx As Integer
'''''''''Debug.Print "in Createrecordset "; recCount
rs1.Open
rs.MoveFirst
For indx = 0 To recCount - 1
rs1.AddNew arrFields, Array(rs!ID, rs!startdate, rs!enddate, 0, 0)
rs.MoveNext
Next '- end of recordset
Dim theGap As Integer
rs1.MoveFirst
rs1.MoveNext '-- start with 2nd record
While Not rs1.EOF
theGap = FindRecord(rs1!ID, rs1!startdate, rs1)
rs1!gap = theGap
rs1!gapNum = 1
'''''''Debug.Print "gap update"; rs1!gap; " , "; rs1!gapNum
rs1.Update
rs1.MoveNext
Wend
End Function
Public Function FindRecord(prmID As Integer, prmSdate As Date, rs1 As Recordset) As Integer
''-- Get the prior enddate and subtract and return difference
Dim tempenddate As Date, aGap As Integer
rs1.MovePrevious
tempenddate = rs1!enddate
aGap = DateDiff("d", tempenddate, prmSdate)
rs1.MoveNext
''''Debug.Print "the gap = "; aGap
FindRecord = aGap
End Function