Running a sub on a newly opened form (1 Viewer)

maff811

Registered User.
Local time
Today, 23:38
Joined
May 1, 2014
Messages
45
Hi,

Attempts to find an answer to this have been unsuccessful thus far, so hopefully someone can assist here.

I have a form 'Lodgers' open and I open a form 'Payments' using the cmdAddNewPayment button. At the same time, I want to execute the cmdEditPayment sub when the form 'Payments' opens. So far I have:

Code:
Private Sub cmdAddNewPayment_Click()

DoCmd.OpenForm "Payments", acNormal, , , acFormAdd, acDialog

End Sub

I have tried different methods that I think would work, but don't achieve the desired result. I have made the sub in the form 'Payments' Public instead of Private and added a line of code under the DoCmd.OpenForm line, but to no avail.

Code:
Public Sub cmdEditPayment_Click()

Me.Payment_Date.Enabled = True
Me.Payment_Date.Locked = False
Me.Amount.Enabled = True
Me.Amount.Locked = False
Me.Payment_Type.Enabled = True
Me.Payment_Type.Locked = False
Me.cmdEditPayment.Enabled = False
Me.cmdSavePayment.Enabled = True

End Sub

Any help is appreciated.
 

JHB

Have been here a while
Local time
Today, 16:38
Joined
Jun 17, 2012
Messages
7,732
Call the procedure/sub in the form "Payments", "on open" or "on load" event.
 

maff811

Registered User.
Local time
Today, 23:38
Joined
May 1, 2014
Messages
45
Hi JHB,

Thanks for the reply. I would do that, except there is another instance when the form is opened where I want the fields' Enabled values to be False and the Locked values to be True. The user must then click an Edit button which sets these fields values to True and False respectively.

In that circumstance, if I call that function on the 'on load' event, it undermines the edit/save function i have included to safeguard against accidental changes to data.

Do you know of any other way?
 

bob fitz

AWF VIP
Local time
Today, 15:38
Joined
May 23, 2011
Messages
4,717
Hi JHB,

Thanks for the reply. I would do that, except there is another instance when the form is opened where I want the fields' Enabled values to be False and the Locked values to be True. The user must then click an Edit button which sets these fields values to True and False respectively.

In that circumstance, if I call that function on the 'on load' event, it undermines the edit/save function i have included to safeguard against accidental changes to data.

Do you know of any other way?
Assign a value to the forms Opening Argument property in the code you use to open the form. You could then test for a value in the Opening Arguments property in the On Load or On Open event.
 

maff811

Registered User.
Local time
Today, 23:38
Joined
May 1, 2014
Messages
45
Hi Bob,

Whilst Open Args worked as desired, I had the unwanted effect of the other instance where the form can be opened of the open args being initiated there. SHould that be the case?
 

maff811

Registered User.
Local time
Today, 23:38
Joined
May 1, 2014
Messages
45
FYI this is what I did

In 'Lodgers'
Code:
Private Sub cmdAddNewPayment_Click()

DoCmd.OpenForm "Payments", acNormal, , , acFormAdd, acDialog, "Edit"

End Sub

In 'Payments'
Code:
Private Sub Form_Open(Cancel As Integer)

If Me.OpenArgs() = "Edit" Then
cmdEditPayment_Click
End If

End Sub
 

bob fitz

AWF VIP
Local time
Today, 15:38
Joined
May 23, 2011
Messages
4,717
Hi Bob,

Whilst Open Args worked as desired, I had the unwanted effect of the other instance where the form can be opened of the open args being initiated there. SHould that be the case?
I wasn't expecting that but perhaps you can reset the forms Opening Arguments property to Null or "" in the forms Unload event.
 

maff811

Registered User.
Local time
Today, 23:38
Joined
May 1, 2014
Messages
45
There are two ways I open the Payments form - the first is through a command button which runs VBA code; the second is via a macro that runs when I click a hyperlink when records are shown in a subform.

The open args worked for the first way I open the form, but the second way had the same result. I'm not sure how that's possible!?!?
 

maff811

Registered User.
Local time
Today, 23:38
Joined
May 1, 2014
Messages
45
Here you go
 

Attachments

  • Rental Database.zip
    629.8 KB · Views: 54

maff811

Registered User.
Local time
Today, 23:38
Joined
May 1, 2014
Messages
45
I'll try and upload it again when I get home - just at work now. Thanks for checking it out.
 

Grumm

Registered User.
Local time
Today, 16:38
Joined
Oct 9, 2015
Messages
395
I can open the file but I don't see any form where you made this code :
Code:
Private Sub cmdAddNewPayment_Click()

DoCmd.OpenForm "Payments", acNormal, , , acFormAdd, acDialog, "Edit"

End Sub
...
 

Grumm

Registered User.
Local time
Today, 16:38
Joined
Oct 9, 2015
Messages
395
Yes, I see what you try to do.
My 2 cents will be to remove that macro and just add vba code to open the same form without the args in it. Maybe that way it will not cause confusion.
 

Users who are viewing this thread

Top Bottom