Results 1 to 9 of 9
  1. #1
    Alphix is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2014
    Posts
    61

    How to use Global Variables and what other way to pass a variable to one Form to another Form???

    I am not able to solve how to pass a variable from one Form to another Form.



    I do not understand how to use global variable functions.

    I would also like to know if there is second way to do this.

    Any explanation on this would be appreciative.

    Please see attachment.

    Thank you.
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,753
    Options:

    Global variable - variable is declared in module header. If a general module then the variable is available anywhere in the db, if in a form or report module then available to that module.

    TempVars - let you research that

    OpenArgs - pass value to form or report when it opens.
    DoCmd.OpenForm "form name", , , , , , {this is the OpenArgs argument}

    Directly populate a control on a form
    Forms!formname.controlname = "some 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.

  3. #3
    Alphix is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2014
    Posts
    61
    Thanks for the quick response.
    I still do not understand. I apologize for being a newbie at this.

    I would like to have a declared variable available anywhere in the database.

    How do I fit your explanation in my code???

    Thank you.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,753
    Options:

    1. create a VBA general code module and declare the variable in the module header
    Be aware that variables lose their value if code execution is interrupted.
    Also, they cannot be referenced by Access queries or in textboxes.
    http://msdn.microsoft.com/en-us/libr...ffice.15).aspx
    http://www.ehow.com/how_6637019_ms-a...-tutorial.html

    2. TempVars - I've never used them - search Access Help or web
    Advantages are they don't lose value if code is interrupted and can be referenced in queries or textboxes.
    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.

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,016
    Those TempVars that June7 mentioned, available from 2007 forward, seem especially good to me because:

    • Unlike Global Variables, they retain their values, even if an Error occurs
    • They can be created in any Module, not just in a Standard Module, and still be available anywhere in the database.


    Some Selected Syntax Examples

    To Create a TempVars and Assign it a Value:

    Code:
     TempVars.Add "strCharacterName", "Bullwinkle Moose"
    where 'strCharacterName' is the name of the TempVars and 'Bullwinkle Moose' is the value being assigned to it.

    To Create and Assign Values for use as Text, Numbers and Booleans:

    Code:
     TempVars.Add "strCharacterName", "Bullwinkle Moose"
    
     TempVars.Add "lngClientID", 123456789
    
     TempVars.Add "boolApplyVAT", True

    To Set or Re-set the Value of a TempVars after it has been created, elsewhere:

    Code:
    TempVars!strCharacterName = "Rocky J. Squirrel"

    To Use the Value (in this case displaying it with a MessageBox)

    Code:
     MsgBox TempVars!strCharacterName

    To Delete a TempVar:

    Code:
    TempVars.Remove "strCharacterName"

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  6. #6
    Alphix is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2014
    Posts
    61

    Can a TempVars store a variable from a varialble? (Need help)

    Can TempVars store a varialble from another variable?

    For example:

    Code:
     TempVars.Add "strCharacterName", "www"
    strCharacterName being the new variable to store the value in variable (www)

    Can this be done and how?

    I need to pass a variable to another new variable.

    Reason for this I need to capture the user's data in one Form and carry it to another form or any other Forms as needed.

    Thank you.

  7. #7
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,016
    I'm not sure I understand what you're saying. Why capture the data in one Variable only to transfer it to another Variable? Simply define the TempVars when it is first needed, say in Form1, and it will automatically be available in any Form, Report, etc. That's the whole reason for using TempVars.

    But to answer your question...yes and no!

    The 'yes:'You can pass a standard Variable to a TempVars, like this:

    Code:
    Dim FName As String
    
    FName = "Missinglinq"
    
    TempVars.Add "strCharacterName", FName

    Notice that you don't use Quotes around the name of standard Variable, you simply use FName. Now, using code like

    MsgBox TempVars!strCharacterName

    the Messagebox will display

    Missinglinq

    But, as to the 'no:' You cannot assign one TempVars variable to another TempVars variable, and as I suggested above, why would you bother to?

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  8. #8
    Alphix is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2014
    Posts
    61
    It worked...Thanks a bunch!!!

  9. #9
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,016
    Glad we could help!

    Good luck with your project!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

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

Similar Threads

  1. Cannot create Global Variables
    By Paul H in forum Programming
    Replies: 3
    Last Post: 05-20-2014, 11:27 AM
  2. Global Variables?
    By futurezach in forum Reports
    Replies: 4
    Last Post: 06-20-2013, 03:45 PM
  3. Setting global variables
    By Remster in forum Programming
    Replies: 1
    Last Post: 08-24-2011, 08:47 AM
  4. Replies: 15
    Last Post: 04-21-2011, 02:50 PM
  5. Pass Variable Values From One Form to Another
    By Nokia N93 in forum Forms
    Replies: 3
    Last Post: 03-07-2011, 11:47 AM

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