On Click - open to new record

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What do I need to add to my button On Click so that the form will open to a
new record?

Private Sub OpenComplaints_Click()
On Error GoTo Err_OpenComplaints_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmComplaints"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_OpenComplaints_Click:
Exit Sub

Err_OpenComplaints_Click:
MsgBox Err.Description
Resume Exit_OpenComplaints_Click

End Sub

THANKS!
 
Private Sub OpenComplaints_Click()
On Error GoTo Err_OpenComplaints_Click

DoCmd.GoToRecord acDataForm, Me.Name, acNewRec

Exit_OpenComplaints_Click:
Exit Sub

Err_OpenComplaints_Click:
MsgBox Err.Description
Resume Exit_OpenComplaints_Click

End Sub
 
Oops! Disregard my last post. I wasn't paying close enough attention.
If the form is going to always be opened to a new record, you can just set
the form's Data Entry property to True. If you are going to want to be able
to edit existing records as well as enter new records, you will have to tell
the form to open to a new record when you open it. You can do that by using
the OpenArgs argument of the OpenForm method. You also have to look for the
value in the Load event of the form you are opening.

DoCmd.OpenForm stDocName, , , stLinkCriteria, , , "New"

Then in the Load event of the form you are opening:

If Me.OpenArgs = "New" Then
DoCmd.GoToRecord adDataForm, Me.Name, acNewRec
End If
 
Thanks, Klatuu.

I'm not sure I follow this. The code doesn't reference "frmComplaints" and
therefore doesn't open it.

What am I not seeing?
 
I know, this was not the correct answer, I did send a follow up that should
correct the problem.
 
Thanks. I've set it up to open to a new record and add/edit records but when
I click the button I get an error: The object 'frmComplaints' isn't open.
 
Klatuu said:
Post back with the code as it is now, please.

Button:
Private Sub OpenComplaints_Click()
On Error GoTo Err_OpenComplaints_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmComplaints"
DoCmd.OpenForm stDocName, , , stLinkCriteria, , , "New"

Exit_OpenComplaints_Click:
Exit Sub

Err_OpenComplaints_Click:
MsgBox Err.Description
Resume Exit_OpenComplaints_Click

End Sub

Form:
Private Sub Form_Load()
If Me.OpenArgs = "New" Then
DoCmd.GoToRecord adDataForm, Me.Name, acNewRec
End If

End Sub
 
My typo
adDataForm should be acDataForm
I can't be sure that is the problem, but try that.
This is in the Load event of the form frmComplaints, right?
 
Back
Top