Open Form with New Record

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
 
D

Dirk Goldgar

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.
 
D

dymondjack

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
 
J

Jeanette Cunningham

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
 

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