Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Initialising Global Variable

Posted on 2002-04-09
10
Medium Priority
?
177 Views
Last Modified: 2010-05-02
hi everybody,

I have a little problem with the vb. I have defined two sort of global variables. One global variable is defined in main module while one is defined in a form.

I like to know, how to initialise these variable. or in other word how to destroy these variable from the memory.

I have tried to set the form to nothing. but the variable still remains.

Please help me urgently

Sajid
0
Comment
Question by:sajicmajic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 6928060
You can't remove them from memory, the variable defined in the module will always exist, and in VB5 onwards (I think)  the one in the form will always exist as well. You will be able to use the form variable irregardless of whether the form is loaded.

Why do you need to do this?
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 400 total points
ID: 6928183
>I have tried to set the form to nothing. but the variable still remains.

Once the form is unloaded and set to nothing, the variable will not persist.  Here is a simple experiment you can try to prove this.  New project, two forms, three buttons on Form1.  Form1 code:

Option Explicit

Private Sub Command1_Click()
    MsgBox Form2.gX
End Sub

Private Sub Command2_Click()
    Form2.gX = "Hello"
End Sub

Private Sub Command3_Click()
    Unload Form2
    Set Form2 = Nothing
End Sub

Private Sub Form_Load()
    Command2.Caption = "Set form2.gx"
    Command1.Caption = "Show form2.gx"
    Command3.Caption = "Unload Form2"
End Sub


Click the Set form2.gx button.  Now click the show form2.gx button.  It should say "Hello".  Now click the unload button and click the show form2.gx button.  It will show a blank message box.  Once the form is unloaded, any reference to a variable on the form will re-load the form (you can show this by trapping the Form_Load event for this experiment.)

Bottom line:  Unloading a form will destroy all public variables on that form.  There is no way to destroy a public variable in a module, however.  Use of dynamic arrays can help if you are having memory problems, because you can use Erase to clear them from memory.
0
 
LVL 18

Expert Comment

by:deighton
ID: 6928236
in my vb version

Form 1

Option Explicit

Private Sub Command1_Click()
Form2.Show
End Sub

Private Sub Command2_Click()
MsgBox Form2.x
Unload Form2
End Sub

Private Sub Command3_Click()
MsgBox Form2.x
End Sub


Form2

Option Explicit

Public x As Long

Private Sub Form_Load()

x = 5

End Sub



  - sequence of msgboxes = 1,2,3  I get command 2 shows '5' as expected - command 3, still shows 5 even though the form is unloaded

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 43

Expert Comment

by:TimCottee
ID: 6928253
Don't forget Deighton that you reference to Form2.x will reload the form even when it is unloaded. So it will always be thus.
0
 
LVL 18

Expert Comment

by:deighton
ID: 6928458
Tim,

In that case I changed the form2 code to

Private Sub Form_Load()

x = x + 5
MsgBox "Hello!"


End Sub

 - in actual fact clicking on command 3 then simply displays the vaue '5'  - the load event is not fired by referencing the public variable in the form.  This is VB5 I'm using at the moment.  Does VB6 differ?

0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6928478
Yes, and if this wasn't here:

Private Sub Form_Load()

x = 5

End Sub

Then x would be empty (assuming variant) when 3 is clicked.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6928526
Deighton, try setting the form = nothing after you unload it.
0
 
LVL 18

Expert Comment

by:deighton
ID: 6928543
..but I dont get the messagebox

in full, the form2 code is

Option Explicit

Public x As Long

Private Sub Form_Load()

x = 5
MsgBox "Hello!"


End Sub
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6928555
No deighton, you are right.  The variable x is made empty, but the Form_Load doesn't run for command3.  This happens in VB 6 as well.

0
 
LVL 2

Expert Comment

by:corvanderlinden
ID: 6931270
Referencing a global variable in a form or a (selfdefined) property, implicit loads the form's data and code but not the form itself, so no Load event.

PaulHews is right : To get rid of data and code segment unload form and then set form to nothing.

Remember using Form2 you are using the (hidden) global Form2 instance

I prefer using :

dim x as Form2

set x = new Form2

'do the job and let x go out of scope, which is an implicit set x = nothing

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question