Subform Add Record Button Syntax

A

Amy McElroy

I'm trying having trouble programming an Add Record button
to insert a record on a subform.

The subform Purchase_History_Form is on a tab control on
the main form, Contact_Info_Form.

I got the Edit Record and Save Record buttons to work, but
the Add Record button brings up the message: "The
object 'Purchase_History_Form' isn't open."

Can anyone point out my error? The code on the subform is
below.

Thank you!

- Amy

Option Compare Database
Option Explicit
Private Sub MakeFormReadOnly()
Me.AllowEdits = False
Me.BTNEditRecord.Enabled = True
Me.BTNSaveRecord.Enabled = False
End Sub
Private Sub BTN_Add_Record_Click()
On Error GoTo Err_BTN_Add_Record_Click

Me.AllowAdditions = True
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
Me.ContactID.SetFocus
Me.BTNEditRecord.Enabled = False
Me.BTNSaveRecord.Enabled = True
Exit_BTN_Add_Record_Click:
Exit Sub

Err_BTN_Add_Record_Click:
MsgBox Err.Description
Resume Exit_BTN_Add_Record_Click

End Sub
Private Sub BTNEditRecord_Click()
Me.AllowEdits = True 'Allow Record to be edited
Me.SerialNumber.SetFocus
Me.BTNEditRecord.Enabled = False
Me.BTNSaveRecord.Enabled = True
End Sub

Private Sub Form_AfterUpdate()
MakeFormReadOnly 'Prevent Record from being
edited
End Sub

Private Sub Form_Current()
MakeFormReadOnly 'Prevent Record from being
edited
End Sub
Private Sub BTNSaveRecord_Click()
On Error GoTo Err_BTNSaveRecord_Click
Me.SerialNumber.SetFocus
DoCmd.DoMenuItem acFormBar, acRecordsMenu,
acSaveRecord, , acMenuVer70
MakeFormReadOnly 'Prevent Record from being
edited
Exit_BTNSaveRecord_Click:
Exit Sub

Err_BTNSaveRecord_Click:
MsgBox Err.Description
Resume Exit_BTNSaveRecord_Click

End Sub
 
D

Dirk Goldgar

Amy McElroy said:
I'm trying having trouble programming an Add Record button
to insert a record on a subform.

The subform Purchase_History_Form is on a tab control on
the main form, Contact_Info_Form.

I got the Edit Record and Save Record buttons to work, but
the Add Record button brings up the message: "The
object 'Purchase_History_Form' isn't open."

Can anyone point out my error? The code on the subform is
below.

Thank you!

- Amy

Option Compare Database
Option Explicit
Private Sub MakeFormReadOnly()
Me.AllowEdits = False
Me.BTNEditRecord.Enabled = True
Me.BTNSaveRecord.Enabled = False
End Sub
Private Sub BTN_Add_Record_Click()
On Error GoTo Err_BTN_Add_Record_Click

Me.AllowAdditions = True
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
Me.ContactID.SetFocus
Me.BTNEditRecord.Enabled = False
Me.BTNSaveRecord.Enabled = True
Exit_BTN_Add_Record_Click:
Exit Sub

Err_BTN_Add_Record_Click:
MsgBox Err.Description
Resume Exit_BTN_Add_Record_Click

End Sub
Private Sub BTNEditRecord_Click()
Me.AllowEdits = True 'Allow Record to be edited
Me.SerialNumber.SetFocus
Me.BTNEditRecord.Enabled = False
Me.BTNSaveRecord.Enabled = True
End Sub

Private Sub Form_AfterUpdate()
MakeFormReadOnly 'Prevent Record from being
edited
End Sub

Private Sub Form_Current()
MakeFormReadOnly 'Prevent Record from being
edited
End Sub
Private Sub BTNSaveRecord_Click()
On Error GoTo Err_BTNSaveRecord_Click
Me.SerialNumber.SetFocus
DoCmd.DoMenuItem acFormBar, acRecordsMenu,
acSaveRecord, , acMenuVer70
MakeFormReadOnly 'Prevent Record from being
edited
Exit_BTNSaveRecord_Click:
Exit Sub

Err_BTNSaveRecord_Click:
MsgBox Err.Description
Resume Exit_BTNSaveRecord_Click

End Sub

Since a subform isn't technically an open form, as far as Access is
concerned, this line:
DoCmd.GoToRecord acDataForm, Me.Name, acNewRec

is failing, since it tells Access to go to a new record *on the named
form*. But if you write it as

DoCmd.GoToRecord , , acNewRec

(defaulting to "go to a new record on the active data object") I think
it will work.
 

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