Link to home
Start Free TrialLog in
Avatar of Amit
AmitFlag for United States of America

asked on

avoiding global variables


Hi,

On one of the forms I have a button cmdMainMenu. On its click either of the two things should happen which are shown below. But I use a global variable  isForm1Hidden . Now this is the problem I can imagine that I will have to hide form1 at so many places in future and then I will have to set  "isForm1Hidden" at those places. Similarly when I will bring up the form again I will have to set it again.

This is a major problem , if I forget this somewher I will run into some other problems. How can I avoid this ?  
I am working on an old code with a very poor design and it has so many global variables for cases like this. I will appreciate if someone can help me with this



Private Sub cmdMainMenu_Click()
   
    Unload Form5
   
    If isForm1Hidden Then
     strBuildSQL = SelectQueryBuilder("Build_Number", "LU_Build")
     Set oBuildRS = ExecuteQuery(strBuildSQL)
     Form1.cboBuild.Clear
     Call GeneralCodes.PopulateComboBox(Form1.cboBuild, oBuildRS, "Build_Number")
     Form1.Show
     isForm1Hidden = False
     errorinForm1 = False
    Else
   
    Form4.Show
     
    End If
   
End Sub
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just out of curiosity, have you figured out why this app deals with a hidden form?  I've had to inherit lots of sub-standard apps where forms were used to store values in textboxes that could be retrieved throughout the app; when this could have been handled more gracefully, and without the form open-not open dependance, using global variables or read-writing to a table.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Amit

ASKER

Thanks for all your comments, I will award the points soon.

Now reagrding jimhorn's comments

Well this project has tons of global variables. Even a variable like

Global form1previousButtonClicked As Integer

Its a collection of 16 forms and many actions require navigation between different forms. In that case it needs to hide a form and bringup another form. I myself is not an experienced visual basic programmer. But I am currently maintaining it for our another project. But I still know that global variables (that too plenty of them) are a recipe for disaster. It also has hiddent text boxes. I will appreciate any suggestions from you guys and example would be great to tell me how  

"forms were used to store values in textboxes that could be retrieved throughout the app; when this could have been handled more gracefully, and without the form open-not open dependance, using global variables or read-writing to a table"

this can be avoided. If you want me start another thread then let me know
>I will appreciate any suggestions from you guys and example would be great to tell me how  
I am currently supporting one VB6 and one Excel VBA app that also has a gazillion global variables, and sadly, no documentation explaining them.

>Global form1previousButtonClicked As Integer
Wow, that's harsh.  This appears to be purely a form event, where 'form1' doesn't say anything about what the form is/does other than its name.  Ug-ly.
If the developer that created this is in your immediate area, I'd recommend a flogging.

>If you want me start another thread then let me know
Since my part of this thread has gone somewhat off-topic, if Hit_MN has answered your question, I'd accept his answer, and then start another thread.
Avatar of Amit

ASKER

Hello all,

Thanks for all your help.

Jimhorn
so with what subject do you want me to start a new thread