Open Form with New Record

  • Thread starter Thread starter Steve Haack
  • Start date Start date
S

Steve Haack

I have a form with a button on it. I want the button to open another Form,
which is linked to a specific table, and when the Form opens, it is on a new
record.

Steve
 
Steve Haack said:
I have a form with a button on it. I want the button to open another Form,
which is linked to a specific table, and when the Form opens, it is on a
new
record.


The easiest way is to open the form in data-entry mode, using a line of code
like this to open the form:

DoCmd.OpenForm "YourFormName", DataMode:=acFormAdd

If you do it that way, the user won't be able to page back to see any
existing records.
 
Open the form using DoCmd from your button's OnClick event, passing an OpenArg

Ex.
Priavte Sub Button_Click
DoCmd.OpenForm "formname", , , , , , "NewRec"
End Sub

Then, in the OnOpen event of the form you are opening, check the OpenArgs
and if it equals NewRec then jump to a new record:

Private Sub Form_Open(Cancel As Integer)
If Nz(Me.OpenArgs, "") = "NewRec" Then
DoCmd.GotoRecord acNewRecord
End If
End Sub



--
Jack Leach
www.tristatemachine.com

- "A designer knows he has reached perfection not when there is nothing left
to add, but when there is nothing left to take away." - Antoine De Saint
Exupery
 
Hi Steve,
if you always want the form to open at a new record, set its Data Entry
property to Yes, on the form's property dialog, data tab.

If you want the form to open to a new record only sometimes, you can use
DoCmd.GoToRecord, , acNewRec

You will need a way to tell the form when to run the code to go to a new
record.
You can do this using Open Args

Here's an example on the form that is opening the form for data entry.
DoCmd.OpenForm "NameOfForm", , , , , , "new"

On the form that's being opened you can code its open or load event like
this
If Len(Me.OpenArgs) >0 Then
DoCmd.GoToRecord, , acNewRec
End If


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
Back
Top