Results 1 to 8 of 8
  1. #1
    system243trd is offline Novice
    Windows 2K Access 2003
    Join Date
    Nov 2011
    Posts
    27

    Using variables in another object

    I have created several variables in a form



    Public variable1, variable2 as Double

    I have then assigned values to these variables based on what the user has entered in a text box on form 1. There is a button form1 that will open form2.

    I want to be able to use these variables in form2, how can I do this.

    thanks

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    I would declare them in a standard module, not behind a form. By the way, you've declared a double and a variant.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    As long as form1 is still open, you can reference those variables from Form2 with Forms!Form1.variable_name.

    You can also reference the textbox directly with Forms!Form1![textbox_name]

  4. #4
    system243trd is offline Novice
    Windows 2K Access 2003
    Join Date
    Nov 2011
    Posts
    27
    I am unable to reference the original form as form 1 closes when form 2 opens.

    If I were to declare the variables and assign them in a module how would I assign the values to form 2?

    e.g. I would call the procedure form1 and assign several values to variables from text boxes on form1. How would these values be assigned to text boxes on form 2?

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    In that case, you have to do as pbaldy has suggested, i.e. declare those variables as public in a standard code module (i.e. not in a form). They are then accessible from anywhere, including form code.

    In Form2 you just do the reverse of what you did in form1:

    If in Form1 you have Variable1 = me![textbox1],

    then in form2 you would have me![textbox2] = Variable1.

    Variable1 would be declared for example with Public Variable1 as Integer in a standard code module.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    An issue with public variables is that they lose value if code execution is interrupted (runtime errors not handled with error handling code). This can be annoying during development testing and debugging.

    TempVars do not lose value.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Two other ideas:

    1) You could also "push" the values into Form2 controls before Form1 closes.

    2) You could pass values from Form1 to Form2 using the "OpenArgs" argument of the "Docmd.OpenForm" command.
    (Docmd.OpenForm(FormName, View, FilterName, WhereCondition, DataMode, WindowMode, OpenArgs) )
    Form2 would have code to handle putting the values from the "OpenArgs" into the correct controls.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    An example using OpenArgs:

    http://www.baldyweb.com/OpenArgs.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 07-02-2013, 08:41 AM
  2. VBA Optimization - Forms in Object Variables
    By GeekInOhio in forum Programming
    Replies: 1
    Last Post: 09-18-2012, 02:28 PM
  3. Replies: 1
    Last Post: 09-03-2011, 07:01 PM
  4. Replies: 3
    Last Post: 11-02-2010, 10:14 AM
  5. Replies: 1
    Last Post: 08-05-2010, 12:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums