Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open a new blank sub form record connected to main form

Status
Not open for further replies.

spartansFC

Programmer
Apr 1, 2009
165
GB

Hi

I'm trying to open a new blank record for data entry linked to whatever person/record i'm on.

For example, i if i want to add new bookings to record 1 (lngPeopleID). I should just be able to click on the new booking command button, the form should open with the correct lngPeopleID with blank fields to fill in.

Main form: frmCustomers (tblCustomers), lngPeopleID (PrimaryKey)
Subform (frmCustomersBookingSubform), qryBookings, lngBookingID (Primary Key), lngPeopleID (ForeignKey)

The subform on the main form is just for viewing only, it can't be edited and the user can only click on edit booking command button on the main form which brings up a pop up form which they can then edit, close it down which refreshes the subform again.

I know how to jump to a record to edit a booking via the subform with this:

Code:
Private Sub EditBooking_cmdbutton_Click()

    Dim stDocName As String
    Dim stLinkCriteria As String
    stDocName = "frmAddEditBookings"
    If Me.Dirty Then Me.Dirty = False
    DoCmd.OpenForm stDocName, , , stLinkCriteria, , , Me![frmCustomersBookings Subform].Form![lngBookingID]
    
Exit_EditBooking_cmdbutton_lest_Click:
    Exit Sub

Err_EditBooking_cmdbutton_lest_Click:
    MsgBox Err.Description
    Resume Exit_EditBooking_cmdbutton_lest_Click

End Sub

I just want to be able to add a new booking to a new persons record, i did try:

Code:
Private Sub AddNewBooking_cmdbutton_Click()
On Error GoTo Err_AddNewBooking_cmdbutton_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "frmAddNewBooking"
    DoCmd.OpenForm stDocName, , , stLinkCriteria, , , Me.lngPeopleID
    DoCmd.GoToRecord , , acNewRec

Exit_AddNewBooking_cmdbutton_Click:
    Exit Sub

Err_AddNewBooking_cmdbutton_Click:
    MsgBox Err.Description
    Resume Exit_AddNewBooking_cmdbutton_Click
    
    

End Sub

All this does is open a blank record but it isn't linked to the person, i do have the OpenArgs in the OnLoad event of the form

Code:
Private Sub Form_Load()
If Len(Me.OpenArgs) > 0 Then
        Dim rs As Object
        Set rs = Me.Recordset.Clone
        rs.FindFirst "[lngPeopleID]=" & Me.OpenArgs
        Me.Bookmark = rs.Bookmark
    End If
End Sub

Just can't figure it out

Michael
 
I believe you need to set the Default Value in the add form.
Code:
Private Sub Form_Load()
    If Len(Me.OpenArgs) > 0 Then
        Me.[lngPeopleID].DefaultValue = Me.OpenArgs
    End If
End Sub

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
SpartansFC,
You don't have any code that will "brings up a pop up form which they can then edit, close it down which refreshes the subform again." Is this working as expected?

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Hi Duane

It does work, on the OnClose event i have

Code:
Forms!frmCustomers![frmCustomersBookings Subform].Form.Requery

This seems to do the trick
 
Thanks for the clarification. Sometimes developers will place the Requery code in the main form after the add form has been opened. This would only work if the add form was opened acDialog. Your method should work well.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top