Create new record on secondary form

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Create new record on secondary form

Post by agibsonsw »

Hello. Access 2007.
I have a staff form with a button that opens a second form to show their holiday details. But if I start a new holiday record it doesn't remember
their staff id number.
What's the best way to carry across the staff id to the holidays form please? Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

JohnH
3StarLounger
Posts: 287
Joined: 09 Mar 2010, 23:16
Location: Canberra Australia

Re: Create new record on secondary form

Post by JohnH »

Set the default value of the StaffID control on the second form (it can be hidden) to:
=Forms("frmstaffDetails")!StaffID

This assumes that you left the staff form open when you open the second form.
Regards

John

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Create new record on secondary form

Post by HansV »

You can use the On Load event of the holidays form to set the default value of StaffID:

Code: Select all

Private Sub Form_Load()
  On Error Resume Next
  Me.StaffID.DefaultValue = Chr(34) & Forms!frmStaff!StaffID & Chr(34)
End Sub
where frmStaff is the name of the staff form and StaffID the id field.
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Create new record on secondary form

Post by agibsonsw »

Hello. Thanks both. Is there an advantage to using the On Load event? Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Create new record on secondary form

Post by HansV »

If you only open the holidays form from the staff form, John's method is easier because it doesn't use VBA. But if the user can open the holidays form while the staff form is not open, you'd get #Error in staff id in a new record.
You won't get the error with the On Load event because the line On Error Resume Next prevents it.
Best wishes,
Hans

JohnH
3StarLounger
Posts: 287
Joined: 09 Mar 2010, 23:16
Location: Canberra Australia

Re: Create new record on secondary form

Post by JohnH »

If you always view Holidays in the context of having the Staff form open, the easiest solution of the lot would be to add Holidays as a subform to the Staff form, then Master and child linking will fill in StaffID automatically. You could add a Tab control, and put the Holidays on another page.
Regards

John