Results 1 to 5 of 5
  1. #1
    MiniG is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2019
    Posts
    4

    How to pass a value to a form, store it in a variable and use in all Sub of the form

    I trying to pass a value from one form to another, store it in a variable and be able to use the variable in all of the Sub of the form.


    I am calling my form using
    Code:
    DoCmd.OpenForm "ViewCompany", , , , , , Me!CompanyCombo.Value()
    In Form_ViewCompany, I am retrieving this value in Private Sub Form_Load():
    Code:
    CompanyID = CInt(Me.OpenArgs)
    Now this works if I declare CompanyID as a global variable in a module
    Code:
    Public CompanyID As Integer
    I want to avoid using global variables as it might create problems and conflicts later in my form workflow.

    If I declare CompanyID in Private Sub Form_Load(), I cannot use this value in other Private Sub associated with the form, for example, with an On_Click() event.

    I would like to know how to declare a variable (and set its value) that can be used in all Sub associated with a form, but cannot be accessed from other forms. I believe this is a "scope" problem and I am trying to figure out the best way to do it. Some people online are suggesting storing the passed value in a hidden Text Box on the form and use its .Value in the other Sub, but I am sure there must be a better way to do this.

    Thanks

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    just put it in a text box,...

    docmd.openform "frm1"
    forms!frm1!txtBox = forms!frmMyForm!txtBox2

    or
    forms!frm1!txtBox = 12

    then everything in the form can see the textbox.

  3. #3
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    There's also TempVars.

  4. #4
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Add a read-only property to the form.

    Click image for larger version. 

Name:	propertyget.JPG 
Views:	16 
Size:	28.5 KB 
ID:	40138
    Now, for the outer procedures, you have a read-only member of the form ("Company") that refers to the private variable "mCompanyID" and only the owner's procedures can change it's value, as seems above in Form_Load() event.

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    IIRC, if the variable is placed in the Declarations section of the form module it is a form level variable that is available to all procedures behind the form. It cannot be accessed by other modules, regardless of whether or not you declare it Public or Private. That seems to be what you're asking about re scope. If you create a variable in a standard module, you can restrict its scope to the module (Private) or make it available to the whole project (Public).
    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. Pass a variable to a Pass Through Query
    By violeta_barajas in forum Access
    Replies: 2
    Last Post: 01-26-2017, 07:59 PM
  2. Replies: 5
    Last Post: 04-27-2015, 02:40 PM
  3. Replies: 8
    Last Post: 10-08-2014, 06:08 PM
  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

Tags for this Thread

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