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

Refer to another form's global variable?

Expert 100+
P: 1,287
Question: Can I set a form's variable from another form?

Details of my problem:

In my Form1, I have
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  4. 'whether each tab has been updated, so they aren't recalculated unneccesarily
  5. Dim tab0 As Boolean
  6. Dim tab1 As Boolean
  7. Dim tab2 As Boolean
On a subform on a tab control on Form1, I have a button that opens Form2. When it's click, I also want to set the tab1 variable to False, so that when Form2 is done and calls my Update function on Form1, it will know to update the recordsource of the subform on the second page of the tab control on Form1.

Is there any way to set tab1 from the code in the subform? I tried stuff like

Expand|Select|Wrap|Line Numbers
  1.     If CurrentProject.AllForms("View Projects Manager").IsLoaded Then
  2.         Forms![View Projects Manager].Form!tab1 = False
  3.     End If
but no luck. I don't see any way to Dim the variable as Public either.
Dec 11 '08 #1
Share this Question
Share on Google+
5 Replies

Expert 2.5K+
P: 3,072
Why not use the tab control to "force" a sequence.
When you hide the tabs (see the properties) and add [Next] and [Previous] buttons you can control the sequence the user has to enter the data like the Access wizards.

Getting the idea ?

Dec 11 '08 #2

Expert 2.5K+
P: 2,653
I think this stuff might be helpful.

Dec 11 '08 #3

Expert 100+
P: 1,287
Thanks Nico, but I'm not actually using the tabs as a sequence for entering data, they are for viewing data only, and contain continuous subforms.

FishVal, I had considered using textboxes to store the variables so I could reference them from the subform, but I was wondering if it was really necessary. I decided to just make a public function in Form1 to set the variables, and call it from the subform. It accomplishes the goal of setting the variables at least.
Dec 11 '08 #4

Expert 2.5K+
P: 2,653
ChipR, if I've understood your correctly, code invoked in Form2 should initiate requiry of certain subform in Form1. Decision about what subform has to be required is made according to variables state (I suppose that the variables depend on Form1 state, maybe Form1.tabcontrol state - so I could guess they are actually redundant).

There are at least 3 general approaches:
  • Open Form2 as modal and let code in Form1 make a job as soon as Form2 is closed.
  • Form2 is allowed to make requiry. For that pass a referense to Form object of relevent Form1 subform control to Form2 via property.
  • Form2 is allowed to notify Form1 to make requiry. For that declare event in Form2 module and raise it when needed, get reference to Form2 object as soon as Form2 has been opened and store it in Form1 module as global object variable declared with events as Form_Form2. Form1 will listen to Form2 events.

The last two methods you could find in article I've suggested you.


You could conviniently access form global variable via property.
Dec 11 '08 #5

Expert 5K+
P: 8,634
Hello ChipR, I'm not sure that this will really help you in your case but Declaring a Variable As Public in a Form's Class Module essentially makes it a Global Property of the Form. Case in point:
  1. Declare a LONG INTEGER in Form1's Code Module:
    Expand|Select|Wrap|Line Numbers
    1. Public lngReallyBigNum As Long
  2. Initialize lngReallyBigNum anywhere within the context of Form1:
    Expand|Select|Wrap|Line Numbers
    1. lngReallyBigNum = 1987234567
  3. You can now retrieve or set the value of lngReallyBigNum from anywhere within your Application via:
    Expand|Select|Wrap|Line Numbers
    1. Debug.Print Forms!Form1.lngReallyBigNum        'retrieve
    2. Forms!Form1.lngReallyBigNum = 987652            'set
Dec 12 '08 #6

Post your reply

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