Results 1 to 14 of 14
  1. #1
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93

    Global Variable disappears on Event Sub Exit

    I created a Public variable:



    Code:
    Option Compare Database
    Option Explicit
    
    
    Public NumApplicantID As Integer
    In the Current Event of a form, I set the variable:

    Code:
    Sub Form_Current()    
    
    NumApplicantID = Me.ApplID  ' Set the Applicant ID
    
    End Sub

    Upon exit from this sub, the Public variable is no longer in scope and disappears. What am I doing wrong? I need to use that variable in another Event on a Button control on this same form.
    I expect that it will be visible across the entire application.

  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,652
    Where is it declared? I would have it in a standard module, not behind a form.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    It's declared in a form. It just seems to me a Public variable is a Public variable, irrespective of where it is declared. I'll move it to a standard module and see what happens.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    Works for me?
    Public in Form module and MSGBOX in Current event?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    Created a separate module. I got past where I was. However, in the same form, but in a button OnClick event, when trying to execute

    Code:
    DoCmd.RunSQL "insert into [tblFormerEmployers] ([numApplID]) values(numApplicantID);"
    (Shortened for brevity but still syntactically correct)

    It asks for a "Parameter" "numApplicantID", as if that Public variable is not visible at all in this OnClick event.

  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,652
    You have to concatenate the value. Since it's numeric you don't need delimiters:

    DoCmd.RunSQL "insert into [tblFormerEmployers] ([numApplID]) values(" & numApplicantID & ");"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    By the way, if that doesn't work you may want to use a variable for the SQL so you can see what it's resolving to:

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

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    NOT syntactilly correct at all.
    You need to concatenate the value into the sql statement.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    309
    Tempvars is pretty stable, I think you would be safe using it instead of a global variable.
    Please click on the ⭐ below if this post helped you.


  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    Quote Originally Posted by Edgar View Post
    Tempvars is pretty stable, I think you would be safe using it instead of a global variable.
    Still need to concatenate correctly though?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    Quote Originally Posted by pbaldy View Post
    You have to concatenate the value. Since it's numeric you don't need delimiters:

    DoCmd.RunSQL "insert into [tblFormerEmployers] ([numApplID]) values(" & numApplicantID & ");"
    That was it! I keep forgetting that non-intuitive syntax. Thanks!

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    That was it! I keep forgetting that non-intuitive syntax. Thanks!
    A bad workman always blames his tools.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    Oooooh

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Quote Originally Posted by pledbetter View Post
    That was it! I keep forgetting that non-intuitive syntax. Thanks!
    Happy to help! I would suggest it is intuitive once you understand what's happening. You're building an SQL string to send to the database engine. That engine has no knowledge of what the variable might contain, hence the parameter prompt.
    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: 26
    Last Post: 11-19-2021, 08:06 AM
  2. Global variable unavailable to sub-form
    By GraeagleBill in forum Forms
    Replies: 2
    Last Post: 06-28-2018, 07:54 AM
  3. Replies: 2
    Last Post: 12-20-2012, 03:06 PM
  4. How to end/exit a sub from its nested sub?
    By lookingforK in forum Programming
    Replies: 2
    Last Post: 12-10-2012, 02:13 PM
  5. Replies: 5
    Last Post: 02-05-2012, 07:58 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