how do I add a new record from a different form

  • Thread starter Terence Lorenzo Vittorio
  • Start date
T

Terence Lorenzo Vittorio

In an attempt to improve my (semi)-disgraceful coding techniques, I have
devised a control form for all my data entry forms. I based my idea around
the "contact management database" found in the tutorials on
office.microsoft.com. The problem I am having is that I managed to make the
Form Task Pad (as I optimistically call it) work for all forms. But it will
not work for subforms when adding a new record. And to make it more
complicated, my subform is in a tab. After I ironed out all problems I am
stuck on just this!

the code in the frmTaskPad is as follows take or leave a few lines

OnClick of the TaskPad form I call it with the relevant parameter
frmActiveForm is set OnLoad and OnFocus of the last form used

Private Function sRecordNav(ByVal lngDirection As AcRecord)

Dim rsClone As DAO.Recordset ' Recordset object
Dim booNewRec As Boolean

Set rsClone = frmActiveForm.RecordsetClone

If lngDirection = acNewRec Then

' Add new record to parent form's recordset

If frmActiveForm.Form.Visible = False Then
frmActiveForm.Form.Visible = True
End If

If SysCmd(acSysCmdGetObjectState, acForm, frmActiveForm.Name) = 0 Then

Rem frmActiveForm.Parent.Form.SetFocus
frmActiveForm.Form.SetFocus
DoCmd.GoToRecord , , acLast

Else
DoCmd.GoToRecord acDataForm, frmActiveForm.Name, acNewRec
End If

frmActiveForm.[DateCreated].Value = Date

Else

booNewRec = frmActiveForm.NewRecord
Set rsClone = frmActiveForm.RecordsetClone
rsClone.Bookmark = frmActiveForm.Bookmark

Select Case lngDirection

' Move to first record
Case acFirst
rsClone.MoveFirst

' Move to previous record
Case acPrevious
' However, if parent is on a new record
' then move to the last record
If booNewRec Then
rsClone.MoveLast
Else
rsClone.MovePrevious
End If

' Move to next record
Case acNext
rsClone.MoveNext

' Move to last record
Case acLast
rsClone.MoveLast

End Select ' lngDirection

' Align parent form's recordset with its clone
frmActiveForm.Bookmark = rsClone.Bookmark

End If

End Function

I have seen many posts where to add a new record in a sub-form, the method
of setFocus Add/DoCmd.GoToRecord , , acLast is used: (I tested by creating a
command button on the form itself and it works fine) but tis solution doesn't
work when one tries to wrap it in a generic function...or at least I am not
capable enough.

I would appreciate any help or comments
cheers

Terence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top