There is currently no numbering on
the tasks, though I think it would be fine if we added numbering, then I
guess we could just sort the form numerically.
Yes, we have to do the above (even the user does NOT have to see the above,
but we still must provide numbering.
The reason for this is that order of records in ms-access is random and ONLY
if you set the order will records return in that order.
(this means that now you been "lucky" that the records are in the same order
that they are entered, but is not a given unless you actually set the order
of the sub-form )
But I would love to do the
right click, insert row option. I just think it's much more elegant.
Sure, we can still use the code example given, but we just need to
incorpoate a numbering sheme into that code....
Assuming we add a field called onum (integer)
Assuming you change the sub-form to a query that is ORDERD by onum,
, then we can use:
Public Function AddToSub()
Dim rst As dao.Recordset
Dim lngNewID As Long
Dim f As Form
Dim intNext As Integer
Dim strSql As String
Me.Refresh
Set f = Me.child1_test.Form
If f.RecordsetClone.RecordCount = 0 Then
intNext = 0
Else
intNext = Nz(f!onum, 0)
End If
If intNext > 0 Then
f.Refresh
' we have to move down all records
strSql = "update contactchild set onum = onum + 1 where onum >= " & _
intNext & " and contact_id = " & Me.ContactID
CurrentDb.Execute strSql
Else
intNext = 1
End If
Set rst = f.RecordsetClone
rst.AddNew
rst!contact_id = Me.ContactID
rst!onum = intNext
lngNewID = rst!ID
rst.Update
Set rst = Nothing
' now re-load sub-form to display new record
f.Requery
' now move to this new reocrd
f.Recordset.FindFirst "id = " & lngNewID
' now set the focus to the sub-form
Me.child1_test.SetFocus
' now set the focus to the field on teh sub-form
Me.child1_test.Form.desc.SetFocus
End Function