Re-Entering a Form - Different Methods Invoked? (1 Viewer)

ytene

Registered User.
Local time
Today, 11:59
Joined
Oct 22, 2015
Messages
20
I have a relatively simple VBA project which includes a simple user authentication form, a simple menu form and then a series of forms which perform different functions.

So far, everything has been working exactly as intended. However, during testing today I observed something not previously seen... The sequence to create the problem is:-

From the menu screen, have an operator request access to a functional form. The Menu code then invokes this with a Form_Load() call - this works flawlessly. However, I then have my user decide that they made a mistake and want to return to the Menu. They click a "Cancel" button and sure enough they are returned to the Menu, again perfectly.

If I then have them change their mind a second time - i.e. re-enter the form, it is displayed for the user, but this seems to be happening without the Form_Load() method being invoked.

It looks to me [I am not sure] as though VBA uses a different method when it "shows" a Form that has been previously loaded - in other words, even though I am using Form_Load both times, for the second invocation the engine is doing something slightly different.

Can anyone direct me to the method or function that would be invoked in such a scenario please? I would like to insert some of my own logic to trigger on that event, so that fields on the form are populated on entry. The logic that I have in my "Form_Load" method is not being triggered on re-entry...

Thank you in advance for any suggestions.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 19:59
Joined
Jul 9, 2003
Messages
16,363
I have struggled with the various form events over the years. To get it to do exactly what I wanted I circumvented the form events. Basically add a public function to the form you are going to open I usually call it fSetUp() ... "Public"--- is very important or you won't be able to call it from outside the form.


Now when you call the Form you use code similar to this:-

Code:
Private Sub btnNewHistEvnt_Click()
Dim strFrmName As String
strFrmName = "frmClientFoldHistEvntDetl"
    DoCmd.OpenForm strFrmName   ', , , , acFormAdd
    
    With Forms(strFrmName)
    '.prpClientID = Me.ClientID
    .fSetUp
    '.Caption = "I CAN CHANGE THE CAPTION"
    End With
End Sub

The line of code:- .prpClientID = Me.ClientID sets a "Custom Property" (you create) in the form. In this example it's commented out and won't be used.

The line:- .fSetUp runs that function that's within the form you are opening and then you can do anything you need.

The line:- '.Caption = "I CAN CHANGE THE CAPTION" (which won't work because it's commented out) changes the caption of the form. It's here as an example of to remind you that other properties of the form are accessible in this process

I also blogged (YouTube Videos) about it HERE:-
https://sites.google.com/site/msaccess457966vmfjg/got-ya-s/form-load-events
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:59
Joined
Feb 28, 2001
Messages
27,319
My question is whether this non-load situation occurs if another form is opened next AND THEN the user says, "Oh, I really wanted to use that first form anyway." A quick search of the web doesn't show this as a common complaint.

Have you tried this with a breakpoint set in the first executable line of the Form_Load code? To be honest, I'd be very surprised to see that the second iteration doesn't trigger the breakpoint a second time.
 

JHB

Have been here a while
Local time
Today, 20:59
Joined
Jun 17, 2012
Messages
7,732
.. They click a "Cancel" button and sure enough they are returned to the Menu, again perfectly.
A Cancel button you've made?
If yes, show the code behind it.
I've never expired that a Form Load event isn't trigged, (if the form is closed correctly), even if you open and closed it again and again.
Couldn't you post your database with some sample data, (zip it) + a description how to reproduce the fault.
 

ytene

Registered User.
Local time
Today, 11:59
Joined
Oct 22, 2015
Messages
20
Firstly, thank you everyone who has posted a response to this thread. I very much appreciate your interest, suggestions and support. Secondly, please also understand that I am an inexperienced VBA programmer. Whilst I have worked professionally as a programmer, this was primarily on mainframe COBOL systems 25 years ago...

OK, with those caveats to explain any stupidity in what follows, on to the actual VBA I've been experimenting with. Here is the code I use to navigate between my Menu form and activity/functionality forms:-


Private Sub btnMenu02_Click()
nextForm = appMenu.menuElement(2, 3)
nextParam = appMenu.menuElement(2, 4) glbVarMenuParams = CInt(appMenu.menuElement(2, 4))

If nextForm = "frmMenu" Then
glbVarMenuNode = glbVarMenuParams
Me.Controls("btnMenuExit").SetFocus
Form_Reset
appMenu.nodeTarget = nextParam
appMenu.getMenuItems
menuTitle.Caption = appMenu.menuTitle
Form_Display
Else
Me.Visible = False
DoCmd.OpenForm nextForm

End If
End Sub

Note in the above code that this is a method for the second menu button on the Menu form [see method name] and that the first index of the referenced array is "2". I appreciate that this is likely crude, but this is how I map between the (max 20) menu option buttons on the menu screen and the (max 20) target functions. Each target function can either be "frmMenu" (in which case we are either navigating up or down our hierarchical menu tree), or a different form name, in which case we are branching out to a leaf node and actually getting to operational functions.

Next, the code that I use in the activity form to return to the Menu form:-

Private Sub btnPWRcancel_Click()
Me.frmACSCurrentPassword.SetFocus
Me.frmACSNewPassword1.Value = ""

Me.frmACSNewPassword2.Value = ""

Me.frmACSFamilyName.Value = ""

Me.frmACSInitial.Value = ""

Me.frmACSGivenName.Value = ""

Me.frmACSUserID.Value = ""

Me.Visible = False

DoCmd.OpenForm "frmMenu"

End Sub

You may be able to ascertain from the variable names in this second code sample that the function provided by this form is "Password Reset" - i.e. it is intended to allow a user to change their own password, after first validating their identity by re-entering their existing password.

The purpose of the "if" Statement in the first function drives the navigation system. I was asked to write this application based on a simple navigation form that uses buttons to select access to different forms. However, this menu itself is entirely dynamic - all the menu descriptions are held in a table and drawn in as required [into the array appMenu.menuElement, which is contained in an appMenu object]. So, when the user clicks a presented button, this can result in either a new menu being displayed [i.e. if the value of menuElement(2,3) is "frmMenu", or the actual Form required.

Then, on each destination Form, I always have a cancel button that returns the user to their respective Menu. In the above sample "Cancel" function, you can see that I try to tidy up the form before my exit.

This works, but then when I re-enter the Form it is clear that my Load method does not get invoked [i.e. I am not loading this Form, merely re-displaying it. My original question was trying to ask "what is the method called when a from is re-displayed?" because, largely from ignorance, I figured that I could use that method to re-insert my data.

Having gone to the trouble of setting this out, I now wonder if I could solve the problem simply by not "clearing down" the data elements from the Password reset form before I exit... [ I will try that and report back].

For what it's worth, my "Form_Load" method on my Password Reset form looks like this:-


Private Sub Form_Load()
menuTitle.Caption = "Change User Password"Set appuserPWR = New acsUser
appuserPWR.getUserByName (glbVarUserName)

If appuserPWR.userName = glbVarUserName Then

Me.frmACSUserID = appuserPWR.userName
Me.frmACSUserID.Locked = True

Me.frmACSGivenName = appuserPWR.userFirstName

Me.frmACSGivenName.Locked = True

Me.frmACSInitial = appuserPWR.userMiddleInitial

Me.frmACSInitial.Locked = True

Me.frmACSFamilyName = appuserPWR.userLastName

Me.frmACSFamilyName.Locked = True

Me.frmACSCurrentPassword.SetFocus

Else
MsgBox ("A problem occured - we cannot find the record for the Authenticated User...")
End If
End Sub

Now, all of this has worked beautifully up until now... But, as soon as I got to a more comprehensive test suite, I spotted this issue when I try to return to a previously activated form. Obviously this has happened because my Cancel feature does not delete/destroy the form object, it merely changes it's state to "Me.Visible=False".

It seems pretty obvious that

DoCmd.OpenForm nextForm
(in the Menu Form) must have a degree of flexibility, so even though I am asking it to perform "OpenForm", what it is actually doing, under the covers, is check to see if the form I want is open but invisible, then simply making it visible if that is the case. If I'm right, then I'm hoping that there might be something like a "Form_Visible()" method I can populate with my setup code...

I don't think the expression, "A little knowledge is a dangerous thing" has ever been more applicable to something I've tried to do...

Thanks in advance for any advice you'd care to offer!

I figure that there has to be a form method .


 

ytene

Registered User.
Local time
Today, 11:59
Joined
Oct 22, 2015
Messages
20
My question is whether this non-load situation occurs if another form is opened next AND THEN the user says, "Oh, I really wanted to use that first form anyway." A quick search of the web doesn't show this as a common complaint.

Have you tried this with a breakpoint set in the first executable line of the Form_Load code? To be honest, I'd be very surprised to see that the second iteration doesn't trigger the breakpoint a second time.

Thank you for the question...

I put a break-point in the form_load() method of the called form and I find that it is triggered when I first launch it, but not on the second or subsequent attempts. Looking deeper [please see code posted in this thread] it might be because I am using "visible=false" to "hide" the form, rather than closing it or otherwise removing it from the run-time address space.

If there is a way that I can close the form, then perhaps that might solve my problem. Haven't found that yet, though...
 

Minty

AWF VIP
Local time
Today, 19:59
Joined
Jul 26, 2013
Messages
10,374
It appears your cancel button doesn't close the form, mearly makes it hidden. Therefore when your menu form tries to opens it , as it is already open it merely makes it visible again, you are not loading it, hence no Load_Event.

Edit - Sorry didn't see the bottom of your last post. Use DoCmd.Close acForm, "YourFormName"
 

ytene

Registered User.
Local time
Today, 11:59
Joined
Oct 22, 2015
Messages
20
It appears your cancel button doesn't close the form, mearly makes it hidden. Therefore when your menu form tries to opens it , as it is already open it merely makes it visible again, you are not loading it, hence no Load_Event.

Edit - Sorry didn't see the bottom of your last post. Use DoCmd.Close acForm, "YourFormName"

Minty, thanks so much... looks obvious with hind-sight. I will try this and report the results back...
 

ytene

Registered User.
Local time
Today, 11:59
Joined
Oct 22, 2015
Messages
20
It appears your cancel button doesn't close the form, mearly makes it hidden. Therefore when your menu form tries to opens it , as it is already open it merely makes it visible again, you are not loading it, hence no Load_Event.

Edit - Sorry didn't see the bottom of your last post. Use DoCmd.Close acForm, "YourFormName"

Minty,

Very happy to confirm that your solution works perfectly. Sorry to have to trouble everyone with such a trivial question - clearly I had hit the "can't see the wood for the trees" moment...

Thank you
 

Minty

AWF VIP
Local time
Today, 19:59
Joined
Jul 26, 2013
Messages
10,374
I can assure you that probably everyone on here has a moment of "Woods for Trees" at least once a week. I know I do daily :D
 

Users who are viewing this thread

Top Bottom