By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,510 Members | 1,437 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,510 IT Pros & Developers. It's quick & easy.

Using the value of a boolean variable in two forms without automatic reset to false

P: 10
I have a subform1 and it has a nested subform2. there is a button in subform1 which will set the value of a boolean variable (say the variable name is MyVar) to true. Thats the task for the button on subform1.

Now, in the before update event of the Source Object(which is a form) of subform2 I want to check the value of MyVar. If MyVar is true then set it to false and do something if not then do some other thing.

My objective is to check if the user has clicked the button on subform1 or not. And do the task ahead according to that.


Hope you have got it.
Access bosses please help me on this.
Dec 20 '17 #1

✓ answered by NeoPa

Let me see if I can outline some possibilities for you then :
  1. Global variables - preferably declared in standard modules.
    Different versions of Access (or maybe it's the Expression Service which * may * actually be in Jet or ACE.) allow, or don't allow, such variables to be referenced from within QueryDefs.
    It's possible to return such values via a Function interface if not accessible directly.
    If the project is reset using the End statement or by resetting from within the VBA IDE then this value is lost in spite of Access still continuing.
    Probably not ideal for you.
  2. TempVars.
    Introduced in ccess 2007 so not available for earlier versions.
    Not VBA variables but objects attached to the database session.
    Not possible to declare a type so are all Variants I believe.
    Last from being added to the TempVars Collection until the database is closed or they are removed from the Collection explicitly.
  3. Controls on a Form/Report that's Left Open.
    Requires a Form (or Report) object is left open throughout the session.
    Lost once the Form/Report object is closed.
    Typically used along with hiding the Form/Report object and capturing and canceling any attempts to close it except via your proscribed method.
  4. Database Properties.
    Probably the best solution available but accessing via the Expression Service may not work well.
    May need to create a Function interface to set and/or return the values.
    Can be typed (String, Boolean, Long etc.)
    Don't even get destroyed automatically when the database is closed.
I hope you find one or other of these ideas suitable for your needs.

Share this Question
Share on Google+
7 Replies


P: 10
Well, I am doing a lot of trial and error on this. It worked once then again the same. The variable must keep the value unchanged set by the button. Problem is it always changes to false automatically no matter I set the value with the button or not.


Please reply someone.
Dec 20 '17 #2

PhilOfWalton
Expert 100+
P: 1,430
I think you are not getting any replies because I am not sure you are using the word Subform correctly.

A subform is a control on a form (possibly looking similar to a listbox) which happens to also be a form.

It is NOT a form opened by pressing a button on some sort of navigation or menu type form.

I apologise if I have misunderstood, but neither question makes sense to me as they stand.

Phil
Dec 20 '17 #3

Rabbit
Expert Mod 10K+
P: 12,316
You're running into scope issues. Where is this variable? Where's your code? You haven't given us enough info to go on.

If this variable exists only in the function, then you can't access it from anywhere else. If you try to access it anywhere else, it creates a different variable with the same name with the default value.

If you want to access it from multiple locations, you need to store the variable in a global location. Either at the module level, or in a table, or the TempVars collection.

But this is just guessing because you haven't told us where this variable is and what your code looks like.
Dec 20 '17 #4

NeoPa
Expert Mod 15k+
P: 31,186
It seems to me you've tried something in your project which isn't working as you'd like it to.

So, you ask what's wrong with what you've done, but at no point do you share what it is that you have done. Does that sound about right? If so then you need to stop and think what's wrong with the above and rectify the thread so that we have a single question that it's possible for us to help you with.
Dec 20 '17 #5

P: 10
Thank you all for your replies.
It seems i couldn't state the situation properly. Sorry for that. Here is some more info:
I just want to know a way to declare a variable which will not be reset to default until i close the ms access program. It must hold its value assigned by any operation(i.e. by a code) and if i use that variable anywhere in my database it will give me that value.
I am giving a simple example:
Suppose, I have 2 forms- form1 & form2
For a control in form 1 i have the following code:
Code1: Private Sub Button1_Click()
Dim MyVar as Boolean
MyVar= True
End Sub

Now for another control on form2 the code is:
Code2: Private Sub Button2_Click()
If (MyVar=True) Then
MsgBox "Bingoo! Someone clicked button1 in form1 before clicking button2."
MyVar=False
Else
MsgBox "No one clicked on button1 in form1"
End Sub

If the value of MyVar is set to false automatically after executing code1 i will never get a true value for the if argument. I know that the above code will not work and I need a global variable. Hope you got it now.
Dec 21 '17 #6

NeoPa
Expert Mod 15k+
P: 31,186
Let me see if I can outline some possibilities for you then :
  1. Global variables - preferably declared in standard modules.
    Different versions of Access (or maybe it's the Expression Service which * may * actually be in Jet or ACE.) allow, or don't allow, such variables to be referenced from within QueryDefs.
    It's possible to return such values via a Function interface if not accessible directly.
    If the project is reset using the End statement or by resetting from within the VBA IDE then this value is lost in spite of Access still continuing.
    Probably not ideal for you.
  2. TempVars.
    Introduced in ccess 2007 so not available for earlier versions.
    Not VBA variables but objects attached to the database session.
    Not possible to declare a type so are all Variants I believe.
    Last from being added to the TempVars Collection until the database is closed or they are removed from the Collection explicitly.
  3. Controls on a Form/Report that's Left Open.
    Requires a Form (or Report) object is left open throughout the session.
    Lost once the Form/Report object is closed.
    Typically used along with hiding the Form/Report object and capturing and canceling any attempts to close it except via your proscribed method.
  4. Database Properties.
    Probably the best solution available but accessing via the Expression Service may not work well.
    May need to create a Function interface to set and/or return the values.
    Can be typed (String, Boolean, Long etc.)
    Don't even get destroyed automatically when the database is closed.
I hope you find one or other of these ideas suitable for your needs.
Dec 22 '17 #7

P: 10
Thank you all for your nice cooperation. This is a great community. Sorry that I have to choose one answer as the best. You all are great people.
Dec 22 '17 #8

Post your reply

Sign in to post your reply or Sign up for a free account.