VB 6 Help with global variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davros
    New Member
    • Jan 2006
    • 1

    VB 6 Help with global variables

    I have 2 forms the main form + a search form which is loaded when the user selects search from the menu of the main form..

    in the code of the main form i have:

    Public intindex As Integer

    in my search form i have the following function :

    Private Sub LstList1_Click( ) 'GO BACK TO PARENT FORM
    intindex = LstList1.ListIn dex
    Unload Me
    frmCourseManage r.Visible = True
    End Sub

    when i compile the program i get the following error message :

    Compile Error Variable not Defined - intindex

    i know when i last ran the program in 2000 it worked fine but now i get the above error could this be a compatability problem ?
    it worked fine in 2000 with windows 98 but now im using windows xp

    thanks in advance.....
  • Ste The Fluffy
    New Member
    • Sep 2006
    • 10

    #2
    It has got something to do with compatibility, i had this problem myself.
    However, this was because i hadn't Dim'd the variables i had put into the script.

    As far as i can tell, this problem is caused because you defined Intindex on a different form to the one that accesses it.
    I found a way to fix this but can't for the life of me remember what it was.

    Aha! This is a possibility:
    When the form Unloaded itself, it may have lost the data it held (i.e Intindex)
    I would suggest TRYING Instead of using:

    Unload Me

    use

    SearchForm.Hide
    (presuming you replace search form with the name OF your search form :p

    I really hope this helps in some way :)
    Sorry if it doesn't, i hope you solve your problem

    Comment

    • Hemant Pathak
      Recognized Expert New Member
      • Jul 2006
      • 92

      #3
      Hi............. ........

      Define ur public variable in the Module.

      or remove the tag of Option Explicit in the top of ur form.

      Comment

      • mukeshkumartanwar79
        New Member
        • Sep 2006
        • 1

        #4
        Hi,

        From what I saw, you have declared a variable named intindex in lets say frmMain and are accessing it in the form named frmSearch.

        Since you have declared the variable in the form module that is frmMain, it is accessible to the procedures written in frmMain only even if it is declared as Public. Insert a Standard Module and then declare this variable as Public there. It will be available to all the forms in your project.

        Originally posted by davros
        I have 2 forms the main form + a search form which is loaded when the user selects search from the menu of the main form..

        in the code of the main form i have:

        Public intindex As Integer

        in my search form i have the following function :

        Private Sub LstList1_Click( ) 'GO BACK TO PARENT FORM
        intindex = LstList1.ListIn dex
        Unload Me
        frmCourseManage r.Visible = True
        End Sub

        when i compile the program i get the following error message :

        Compile Error Variable not Defined - intindex

        i know when i last ran the program in 2000 it worked fine but now i get the above error could this be a compatability problem ?
        it worked fine in 2000 with windows 98 but now im using windows xp

        thanks in advance.....

        Comment

        • dhananjay babu
          New Member
          • Oct 2006
          • 2

          #5
          Dear sir
          When i compile the visual form iam getting the "compile error variable defined" error, please give the solution for this error






          Thanks & Regards

          Dhananjay babu.

          Comment

          • ComputerWorks
            New Member
            • Nov 2006
            • 1

            #6
            when you decare a public variable in one form and use it in another form you must use FormName.VariableName to find the varialbe from another form.

            Comment

            • albertw
              Contributor
              • Oct 2006
              • 267

              #7
              Originally posted by ComputerWorks
              when you decare a public variable in one form and use it in another form you must use FormName.VariableName to find the varialbe from another form.
              therefore you should declare variables which are used in severals forms in a module.
              then make them 'public' or global (const)

              Comment

              • The Black Sheep
                New Member
                • Aug 2008
                • 1

                #8
                This is THE best solution ever!
                I had a different, yet similar problem with global variables.
                They lost their value after an unknown period of time, but I declared them inside the same module as all my code.

                My project got entirely undermined, since my global variable held the IRibbonUI reference to the WORD 2007 ribbon.
                By putting all global (public) variables in a seperate module, they are more easy to find and their behaviour is more stable.


                Thank you.

                Comment

                Working...