?
Solved

Variables Getting Reset After Displaying Form from another Form

Posted on 2003-12-05
10
Medium Priority
?
460 Views
Last Modified: 2010-05-01
Situation: I have a main form, a menu option displays a second form.  2nd form contains a listbox of items, selecting an item from the listbox allows a button to be pressed which displays a 3rd form.  Upon closing the 3rd form the focus is returned to the 2nd form.  

Everything works fine when you've selected 1 item from the listbox, but on the second item (no matter which item in the list that may be), by display/unloading the 3rd form variables held in the 2nd form are reset, i.e. a disconnected recordset I hold from having say 10 items suddenly has 0.

I initially thought it was maybe because I was displaying both forms 2 and 3 as modal, but I've tried it with modeless and the same thing happens.  I'm confused why values held in the 2nd form should be effected by another form, in addition not all variables are reset making it even more confusing, any help/suggestions would be great.

Running VB6 SP5 on Windows 2000 Pro

Thanks,

James
0
Comment
Question by:JEGBIE2
[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
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 1

Expert Comment

by:carpe-diem
ID: 9882293
Have you made the variables global?
Add a module to your project and declare the variables in the module using
Public Var1 As String
etc.
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 9882316
you should make a Property Get Set in  a Module

Private m_form2Vars as whatever

Public Property form2Vars() As Integer
        Get
            form2Vars = m_form2Vars
        End Get
        Set(ByVal Value As Integer)
            m_form2Vars  = Value
        End Set
   End Property

And you can set or get the value


form2Vars =  
0
 

Author Comment

by:JEGBIE2
ID: 9882375
Thanks, while I accept what you propose will work, what I still don't understand is why the private variables on the 2nd form get reset at all.  Form 2 is basically an object, by displaying a 3rd modal form you are not unloading the 2nd form or reinitialising it, it should therefore retain the same state+values it had before you displayed the 3rd form when you unload the 3rd form, if it didn't it would make displaying more than 1 form at a time useless.

Also the fact it does retain values the first time but then loses them the 2nd time makes me think there's something else causing the problem.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:EDDYKT
ID: 9882405
Is any reason you unload the form2 and restart?

0
 
LVL 48

Expert Comment

by:Mikal613
ID: 9882409
The Form in .NET is a Class when you Load it it just makes an Instance not the actual Form. Soin essence the data is lost
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9882421
I meant you may unload the form2 and restart in your code?

Show us some code

You can debug from debug menu, add watch with your variable name and

select break in value change on watch type
0
 
LVL 4

Accepted Solution

by:
PBuck earned 1000 total points
ID: 9883403
Just out of curiousity, are you performing init or load procedures/functions from your Form_Activate routine on your Form2 ?

If so, that would possibly explain why when closing form3 your vars get dumped or reset.  Obviously, the form_activate gets called when form3 is unloaded whether it is modal or not.
0
 
LVL 2

Expert Comment

by:diarmaid
ID: 9883583

Are you just using Form2.show to load the form or are you creating an object of Form2 and displaying that.
0
 

Author Comment

by:JEGBIE2
ID: 9902965
I think I was having one of those working too long and hard on a tight deadlined project and I needed someone to state the obvious!  The problem was there was code in the form activate event which did reset variables, however, I had these covered by a flag which is why I couldn't understand it, but it appears due to a mistake on my part this flag was reset at a point in error so after the first time the form activate code was run, DOH DOH DOH!  Thanks everybody who commented, I'm off to raid the fridge for a forbidden donut :)
0
 
LVL 4

Expert Comment

by:PBuck
ID: 9904583
LOL.  Glad it was something simple - thanks for grade.

See ya around more on EE!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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

770 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