Keeping veriables

This is I am sure very basic but I don't know it so...  I am asking, I have multiple variables that need to be available (The answers that is) at any given time.  The problem is that if I call on any function or other sub the variable is lost.  I have been using checkboxes and stuff to save the info because they aren't affected when calling outside of the sub that is running.  But it’s ridiculous because I have like 20 different things that save the different variables.  I know there is an easier way but I can't seem to get it.  I think that I need to use global variables but I don't know how to do that.  Any help would be appreciated.  Thank you!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Either make them public by putting them in a bas module, e.g.,

Public MyVariable As Integer   'available throughout whole project

or add them at the top of your form, e.g.,

Option Explicit

Private MyVariable As Integer  (make public if you want to access the variable from other forms, etc.)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alternatively... consider the following....

As a rule it is considered bad practice to simply make variables global / public (i.e. can be read/written by absolutely any procedure in the project) or modular (i.e. can be read/written by absolutely any procedure contained within the same module), as it can lead to confusion and lack of control as to how and when variable values get changed.  Making variables global / public or modular is normally only done when no other method is available.

For the reason stated above, you may wish to consider passing the value of the variable into a function or sub procedure.

For example:

Private Sub txtQuantityPerPack_Change()
    Dim lngNewTotal As Long

    lngNewTotal = val(txtQuantityPerPack) * 50

    'Call our function to check if the new total is valid
    If IsTotalValid(lngNewTotal) = True Then
        txtTotal = lngNewTotal
        txtTotal = "err."
        msgbox "Invalid Quantity Per Pack!"
    End If
End Sub

Private Function IsTotalValid(Value As Long) As Boolean
    If Value>=0 And Value<=10000 Then
        IsTotalValid = True
        IsTotalValid = False
    End if
End If
In a sub main

Global  your varaible as string(interger, etc)

You will use it whereever and whenever you want
it keeps for the life of the program

If you code very strictly and are organized, you will have no trobule with the global variables.  I use em, never had a problem with em
>>In a sub main

If you actually put it inside your Main subroutine

1) You cannot declare it as global
2) It is not global to the project

It must be declared outside all procedures at the top of your module as global (or public)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.