Re: global variables



On Sun, 12 Feb 2006 19:32:17 -0000, "james" <ds@xxxxxxxxxxx> wrote:

Hi People,

Sorry, I'm a bit of a newbie and was wondering, if I have a program with a
lot of forms, which most have the same global variables such as:

Private Type OnePair
PairID As Integer
P1ID As Integer
P1Name As String * 30
P2ID As Integer
P2Name As String * 30
Score As Integer
Legible As Boolean
End Type

Dim Pair As OnePair
Dim NumberOfPairs As Integer
Dim Filename As String

Is there a way of shortening the code so that I don't need to add the above
code in every single form??

You can define the Type as Public in a standard bas module, then just
declare the variables on the form level.
I say this way, because the above doesn't make sense as "global",
since you are declaring 'Pair' etc.. as private variables for each
Form. Note that Dim creates a private var. So if you have it working
fine like that, they are not globals, but Form level variables.

If each form has it's own unique Pair variable, then you need to
declare those at the form level to restrict scope to each Form.

If on the other hand, Pair should be accessible from all forms, then
you can declare it Public in the bas module as well and that will make
it "global". Just remember that with this latter one, if Form2 changes
Pair, Form1 will see that change too.

--

Regards, Frank
.



Relevant Pages

  • Re: Querying a database
    ... Use My Database ... Declare @sCnt VarChar ... Set Nocount Off ... Dim pairsAdapter As New MWFNTableAdapters.MWFNTableAdapter ...
    (microsoft.public.vb.database)
  • Re: tab in userform locks program
    ... > I am a VBA newbie and have not been using DIM in my code so it is ... declare a variable at the top of a standard module using Private (module ... You may notice that I used Format$ instead of Format. ... but Format$ returns a String whereas ...
    (microsoft.public.excel.programming)
  • RE: Declarations Procedure and Duplicate Declarations
    ... I commented out the dim statement and the damn thing compiled. ... Sub Want_Info ... Otherwise within the procedure when you make reference to hserow are you ... I am getting a dupe declare compile error on hserow in the following: ...
    (microsoft.public.excel.programming)
  • RE: scope of public variables
    ... "Jim Thomlinson" wrote: ... If your globals are declared anywhere other than a standard code module ... is where did you declare your variable. ... Sub optCkforDupes_Click ...
    (microsoft.public.excel.programming)
  • Re: Tasks and Calendar
    ... ' Declare the variable. ... Dim oAppt as Outlook.AppointmentItem ... >> function returns an Items collection which you can loop through. ... >> For Each AnyObj in AnyCollection ...
    (microsoft.public.outlook.program_vba)

Loading