Results 1 to 10 of 10
  1. #1
    azizrasul is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2019
    Posts
    4

    Keeping a variable in scope


    Hi, This is my first post on this forum.

    If I run some code triggered by say pressing a command button, call it cmd1, and I have a variable that takes on a value when the function comes to an end.
    How do I retrieve this value from the same variable when I run a different code behind cmd2?
    In other words, how do I retain the variable value in scope when I run different functions triggered by separate events?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    global variables exist everywhere, all forms , all code modules.
    GLOBAL gsUserID as string

    local variables exist only in the sub/function and nowhere else.
    modular vars exist in the entire form or vb module if you delare above all the subs: which is what you want

    Code:
    'internal form code
    private msName as string
    
    sub cmd1_click()
      msName = "Bob"
    end sub
    
    
    sub cmd2_click() msgbox msName end sub

  3. #3
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    How long do you want the variable scope to last?

    Everywhere whilst the db is running - declare it as a Public in a separate module, note this will not be set if your code crashes.
    Just while the form is open - declare it before any other code in the forms module - Directly below Option Explicit
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    azizrasul is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2019
    Posts
    4
    When I run the code on a user form with 2 command buttons and declare the variable outside of the functions as mentioned, then it's fine. However when I do the same by running the 2 functions separately via test modules, then the second function doesn't remember the value when the first function was run.

    I want the variable to last when the second function finishes.

  5. #5
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    That sounds like you are declaring the variable in the forms module.

    Make a new Module - call it modGlobals

    Declare your Public variables in that.
    Save it, and restart you database.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Is this related to this thread https://www.access-programmers.co.uk...22#post1617022 by any chance?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  7. #7
    azizrasul is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2019
    Posts
    4
    It is Yes.

    How would you declare the variable in modGlobals? The variable name is

    Code:
    Public MyDict As Dictionary


    I have tried replacing Public with Global, but that doesn't make a difference.

  8. #8
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I'm also going to guess that this might be in Excel then ?
    I'm not sure you can declare a Dictionary like that.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  9. #9
    azizrasul is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2019
    Posts
    4
    Yes, the code is written in Excel.

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    maybe look here
    https://support.microsoft.com/en-ca/...r-applications

    or any one of thousands of places where this is well documented?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 6
    Last Post: 10-21-2015, 03:08 PM
  2. Replies: 3
    Last Post: 05-28-2013, 12:53 PM
  3. Replies: 0
    Last Post: 08-10-2011, 11:59 AM
  4. Need some help on the variable scope
    By blueraincoat in forum Forms
    Replies: 3
    Last Post: 05-26-2011, 01:37 AM
  5. Refering to variable form names inside a variable
    By redpetfran in forum Programming
    Replies: 2
    Last Post: 05-21-2010, 01:39 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