Solved Problem with TempVars (1 Viewer)

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 21:55
Joined
Apr 1, 2019
Messages
712
People, I have a main Navigation Form called "Main_Menu" (I couldn't think of a better name), I use the onclick event of a button to open the next form. At That Event I also assign 'TempVars!PreviousForm="Main_Menu", then go on to open form 'CompaniesFRM' . Conversely, on 'CompaniesFrm' I have a button that opens 'TempVars!PreviousForm (which may be = Main_Menu, or not depending upon from where It's been initiated) and close 'CompaniesFRM'. The reason I do this is that depending upon which form I have come from dictates the controls that are visible on the form to be opened (Maybe for instance, it's a single form as a result of a hyperlink where I don't want to display nav buttons).

This seems to work well in most cases, except I have a bound subform on 'CompaniesFRM'. If opened from the 'Main_Menu' option button, I do not wish to display the Close Button on that subform. I thought I'd assign TempVars!PreviousForm = "CompaniesFRM" during the onload event of 'CompaniesFRM', then check the value of TempVars!PreviousForm in the onload event of the subform & if ="CompaniesFRM", then hide the close button.

However, it appears that when I open 'CompaniesFRM' that the value is not passed to the subform, in fact when adding debug.print It always displays 'Main_Menu'. I thought that the onLoad event of 'CompaniesFRM' would assign the said value. It seems that upon opening the 'CompaniesFRM' the subform 'OnLoad event' precedes the Parent form Onload Event. So the subform never sees the TempVars!PreviousForm = "CompaniesFRM" , but rather still sees 'TempVars!PreviousForm="Main_Menu". To test this, I've temporarily changed the value of 'TempVars!PreviousForm to ='CompaniesFRM' in the onload event of the subform & all works properly.

I've explained this to the best of my ability, It's difficult to put into words. I hope I've made at least some sense.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:55
Joined
May 7, 2009
Messages
19,169
check the Tempvar variable on the Current event of the subform?
on the subform add a form-wide variable, so it only check the tempvar once
on the current event:

on the subform:
Code:
Option Explicit

dim bolFormChecked As Boolean

Private Form_Current()
If bolFormChecked = False Then
    If Tempvars!!PreviousForm.Value="Main_Menu"
         'disable the close button here
    End If
    bolFormChecked = True
End If
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 21:55
Joined
Apr 1, 2019
Messages
712
@arnelgp , thanks will give it a try.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:55
Joined
Oct 29, 2018
Messages
21,357
Maybe you could also consider using two TempVars: PreviousForm and ShowNav
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 03:55
Joined
Feb 28, 2001
Messages
26,996
Since subforms are opened before their parent forms, and (it appears) you are using the parent form's events to control the TempVars data, you have a timing issue. Perhaps you might want to look at the contents of Me.Parent.Name from the sub to see if that helps you to figure out where you are? I forget off hand exactly what is in Me.Parent for a form opened as a direct launch from Access itself (i.e. an "opening form" or from the navigation pane) but I would bet that it is not the same as something launched via DoCmd, and CERTAINLY would be different for a sub-form where the launch of the sub-form is a derivative launch due to being a sub-form as opposed to a "main-level" form.
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 21:55
Joined
Apr 1, 2019
Messages
712
@The_Doc_Man , yes i believe it's a timing issue too. I've proven via inserting msg boxes into the onload routine of both parent & subform to display the contents of the TempVar & it's clear that the subform opens first. I would never have thought this!. So the subform never sees the correct TempVar variable generated by the mainform. I'll have a look at using me.parent.name.

@theDBguy , will i not have the same problem, just with a different variable name if i assign a value to tempvars!shownav upon loading the mainform then look for it on the onload event of the subform? Or am i missig some logic?

Appreciate it gents.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:55
Joined
Oct 29, 2018
Messages
21,357
@theDBguy , will i not have the same problem, just with a different variable name if i assign a value to tempvars!shownav upon loading the mainform then look for it on the onload event of the subform? Or am i missig some logic?
Well, if I understand it correctly, you have a menu form where you assign the value "main menu" to the tempvars and then open Form1, correct? Now, in the Open event of Form1, you want to adjust the value of tempvars, so that you can show or hid the navigation buttons in the subform, right? However, your problem is that you're using the subform's Load event to check the value of that tempvar to control those navigation buttons.

If so, what I am suggesting goes something like this: On you menu form, assign the value "main menu" to tempvar1 and the value "false" to tempvar2 before opening Form1. Of course, when Form1 opens, it uses tempvar1 to go back to the previous form. However, in the Load event of the subform, it can check the value in tempvar2 to hide or show the navigation buttons. In the Open event of Form1, you don't have to change the value of tempvar1 at all.

Of course, the above assumes you would know ahead of time whether the subform needs to show the navigation buttons or not, so that you can assign the correct value to tempvar2 from the menu form.

Hope that makes sense...
 

HillTJ

To train a dog, first know more than the dog..
Local time
Yesterday, 21:55
Joined
Apr 1, 2019
Messages
712
@theDBguy , I understand, yes I'd expect that approach to work. As it stands, I just used the me.Parent.Name.Value on the load event of the subform as suggested by @The_Doc_Man. This works fine. I check the Parent Name and if on the correct 'Parent form' I then hide the appropriate buttons. All Good Guys. Thanks.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:55
Joined
Oct 29, 2018
Messages
21,357
@theDBguy , I understand, yes I'd expect that approach to work. As it stands, I just used the me.Parent.Name.Value on the load event of the subform as suggested by @The_Doc_Man. This works fine. I check the Parent Name and if on the correct 'Parent form' I then hide the appropriate buttons. All Good Guys. Thanks.
Sounds good. Good luck with your project!
 

Users who are viewing this thread

Top Bottom