Results 1 to 11 of 11
  1. #1
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Public Strings (VB 6)

    I must be missing something regarding public variables. I want to access a string array from several forms, so I set up a BAS module in which I have something like the following:

    'Declare public variables
    Public strA(10)

    ;Setup Strings

    Public SetStrings()
    strA(0) = "String A"
    strA(1) = "String B"

    etc.

    From within a form I first execute a call to SetStrings, but when I try to print the string (debug.print strA(0)) it doesn't appear.

    What am I missing here? Or is there a better way to set up the 10 public strings?

  2. #2
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Public Strings (VB 6)

    The first thing I see is that there is no Sub or Function statement for SetStrings. Therefore, I don't know how you could call it.
    Legare Coleman

  3. #3
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Public Strings (VB 6)

    I wasn't very clear, here is the sub

    'Setup Strings

    Public Sub SetStrings()
    strA(0) = "String A"
    strA(1) = "String B"
    etc.
    End Sub

    In the On Load code in my form I simply enter

    SetStrings

    and I expect to have the above string values available in the code for the form.
    etc.

  4. #4
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Public Strings (VB 6)

    I don't have VB6, but I put your code into Excel VBA and it seems to work fine. See the attached file.
    Legare Coleman

  5. #5
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Public Strings (VB 6)

    One more thought. Are you by any chance defining the Public variable in the module behind the form. I do not think that will work. I think the variable has to be defined in a normal module.
    Legare Coleman

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Public Strings (VB 6)

    SetStrings can be Public in a separatre module, or it can be Private within the Form.

    Only other requirement is Public strA(10) has to be in a separate module.

    If you are defining a local Dim stra() anywhere that is called, the Public vesion gets overridden by the Dim.

    Ditto for using a Private strA() in the Form.

  7. #7
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Public Strings (VB 6)

    Thanks for the advice, but I must apologize as the problem wasn't with the public function. The problem was with a dumb programmer. I had the string array dimensioned in the public routine, but I had also dimensioned it in my form -- that caused all the conflict.

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Public Strings (VB 6)

    That's what I thought.

    Most of us have been there before.
    Easy error to make, e.g., when moving code among forms/modules/classes, or when renaming variables.

  9. #9
    4 Star Lounger SteveH's Avatar
    Join Date
    Jan 2001
    Location
    Chelsea, Gtr London, United Kingdom
    Posts
    587
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Public Strings (VB 6)

    One way of avoiding that problem is to prefix your variable name with a scope indicator e.g. pstrA for a public variable and mstrA for a module level variable.
    Steve H
    IT Lecturer/Access Developer
    O2K SR3/O2010; Win7Pro

  10. #10
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Public Strings (VB 6)

    You got it exactly right. I moved the code to the module and forgot to delete the array definintion in the source form.
    Changing the name sounds like a very good idea. Thanks.

  11. #11
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Public Strings (VB 6)

    depends on what your are doing.

    if were counting on values being used outside of the sub, then you need to either use the pulic array, or pass the public array to the sub as a differently named array.
    if you use a local array, then do you really need the public array.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •