Results 1 to 10 of 10
  1. #1
    jackkent is offline Novice
    Windows Vista Access 2007
    Join Date
    Sep 2010
    Posts
    12

    Declare a Global Variable and Use it on Forms

    I thought this would be a simple process but I'm having trouble with it:
    I set up the following in a module:

    Option Compare Database
    Global gbl_user_name As String
    Public Function get_global(gbl_user_name As String) As Variant
    gbl_user_name = "Jack Sprat"
    End Function

    When I try to set the contents of a text box (on a form) to gbl_user_name



    Me.txt_user_name.Value = gbl_user_name

    I get the following error message.

    Expected variable or procedure, not module.

    Please help if you can.

    Thanks,

    jackkent

  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,521
    Edit: misread the question. Where is that declared? I do this type of thing in a standard module:

    Public lngResNum As Long
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jackkent is offline Novice
    Windows Vista Access 2007
    Join Date
    Sep 2010
    Posts
    12

    Module

    Is in a standard module (not a class module).

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Oh, and make sure the module itself isn't named the same as either the function or the variable.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    jackkent is offline Novice
    Windows Vista Access 2007
    Join Date
    Sep 2010
    Posts
    12
    Still having a problem... Renamed module to mod_user_name

    Option Compare Database
    Global gbl_user_name As String
    Public Function get_global(gv_user_name As String) As Variant
    gv_user_name = "Jack Sprat"
    End Function

    No error message but blank text box.

    On form:
    Me.txt_user_name = gv_user_name

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You would have to execute that "get_global" function at some point to populate the variable; are you? Also, unless it's a typo, you have two different function names there.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    jackkent is offline Novice
    Windows Vista Access 2007
    Join Date
    Sep 2010
    Posts
    12

    Global Variable

    Sorry, I'm a novice at Access... Not sure what you mean by executing the function. Can you offer a suggestion on how to code/execute? BTW, love the Who is John Galt. I've read everything I could get my hands on by AR. Huge fan of Objectivism.

  8. #8
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    put a line of code in the form_load event:
    private sub form_load()
    gbl_user_name="jack Sprat"
    end sub

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    In your code you have this function that sets the value of the variable:

    Public Function get_global(gv_user_name As String) As Variant
    gv_user_name = "Jack Sprat"
    End Function

    That function will not execute unless you tell it to. Is there a reason you're using it? As written, there's no reason for the input parameter. Maybe you can clarify the overall goal here.

    I like most of AR's writing, but Atlas Shrugged is probably my favorite. Sadly, it's probably more relevant now than ever.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    jackkent is offline Novice
    Windows Vista Access 2007
    Join Date
    Sep 2010
    Posts
    12

    Global Variables

    Thanks all. I believe I found better way of handling (i.e. Temp
    Vars). This way the global variable is never destroyed in the case of an error. Again, thanks all.

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

Similar Threads

  1. Replies: 2
    Last Post: 06-23-2010, 06:37 PM
  2. Refering to variable form names inside a variable
    By redpetfran in forum Programming
    Replies: 2
    Last Post: 05-21-2010, 01:39 PM
  3. Help Please - Global Vars
    By graviz in forum Programming
    Replies: 3
    Last Post: 02-18-2010, 10:36 AM
  4. using forms control as report variable
    By Seven in forum Reports
    Replies: 3
    Last Post: 12-19-2009, 04:04 PM
  5. Macros for global search
    By joypanattil in forum Access
    Replies: 2
    Last Post: 11-28-2009, 04:30 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