Consulting

Results 1 to 6 of 6

Thread: Accessing variable in one form from another?

  1. #1

    Accessing variable in one form from another?

    Example:
    *frm1 has a Public string variable str1 and has a button that launches frm2
    *frm2 is 'modal' and 'Pop-up', has a textbox 'txt1' and a button 'btnClose' that closes the form and sets 'str1' to txt1's value

    If I launch Form2, enter something in the textbox, then hit the button, and try executing '!Forms!frm1!str1 = txt1' from within the btnClose's subroutine, then MS Access will tell me that it cannot find 'str1'.

    I only see 2 courses of action here:
    1) create a table to store this value
    2) use an invisible form control like a textbox or label in frm1 (because something like 'Forms!frm1!invisibleLabel.caption = txt1' would work 100%)

    Both of the solutions are awkward and I'm hoping you can help me come up with something better.

  2. #2
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    deadbeat, welcome to the Forum.
    You have fallen in to a very common trap that Access doesn't make clear, the Public Variable has to be Declared in a Module for it to be accessible to all forms.
    There are also a couple of other ways to pass variables between forms, one is the OpenArgs used in the Opening command line.
    The other is to pass them directly form field to field,
    ie. forms![frm1]![txt1] = me.str1.
    But of course if you have closed Form1 you can't pass the data back, whereas with the Public variable declared in a Module you can use and pass avalue to it at any time.

  3. #3
    Quote Originally Posted by OBP
    deadbeat, welcome to the Forum.
    You have fallen in to a very common trap that Access doesn't make clear, the Public Variable has to be Declared in a Module for it to be accessible to all forms.
    Not necessarily. I had some of the syntax wrong. Mainly having a '!' instead of a '.'

    Quote Originally Posted by OBP
    But of course if you have closed Form1 you can't pass the data back, whereas with the Public variable declared in a Module you can use and pass avalue to it at any time.
    Yeah, I think I'll use this particular approach more often. Very convenient. Thanks.

  4. #4
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    I recommend that you keep the use of Public variables to a minimum. If there are lots of them (and I've seen examples with dozens!) it's very difficult to debug when something goes wrong.

    Another method you can use is OpenArgs, which enables you to pass a value as you open the form. As soon as the form is closed, that variable is discarded.

  5. #5
    Quote Originally Posted by geekgirlau
    I recommend that you keep the use of Public variables to a minimum. If there are lots of them (and I've seen examples with dozens!) it's very difficult to debug when something goes wrong.

    Another method you can use is OpenArgs, which enables you to pass a value as you open the form. As soon as the form is closed, that variable is discarded.
    That's very good advice. I just recently started using OpenArgs more. It's a lot more contained then.

  6. #6
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    A trick if you need several variables: create a string with a delimiter, open the form with your new concatenated variable as OpenArgs, then use Split to separate the variables again.

Posting Permissions

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