Results 1 to 7 of 7
  1. #1
    Phil H is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Dec 2019
    Location
    Phoenix, AZ
    Posts
    19

    Variable defined in Public Sub does not appear in another Private Sub

    In a Public Sub, I Dimension a String variable called Course and place a value in it. I confirmed the value is actually there via the debugger. When I refer to the variable in another Private Sub it recognizes the variable name OK, but the field is empty. What have I done wrong???


    Thanks for any help...Phil H

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,965
    Variable is declared in module header?
    Do you have Option Explicit in every module header?

    If you want to provide db for analysis, follow instructions at bottom of my post.
    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
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,010
    I *thought* the Sub or Function was Public?, not the variables dimmed within them?
    I put any I need Public at the top of a module, right after the Option statements.
    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

  4. #4
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,656
    You may want to look around for articles that explain "Scope"

    here's one to get you started https://docs.microsoft.com/en-us/off...and-visibility
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  5. #5
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,424
    For a public variable it's

    Public course as string

    not

    Dim course as string


    Careful! If you Dim the variable again in a local procedure, the procedure will use that variable instead of the public one.
    Last edited by davegri; 04-22-2022 at 06:53 PM. Reason: added careful note

  6. #6
    Phil H is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Dec 2019
    Location
    Phoenix, AZ
    Posts
    19
    Quote Originally Posted by Phil H View Post
    In a Public Sub, I Dimension a String variable called Course and place a value in it. I confirmed the value is actually there via the debugger. When I refer to the variable in another Private Sub it recognizes the variable name OK, but the field is empty. What have I done wrong???
    Thanks for any help...Phil H
    I would mark this as "solved" but I can't figure out how to do that. Actually, I didn't get my problem solved, but I believe my problem has to do with the "Scope" issue and my lack of understanding of module versus procedure, etc. In the end, I found a work-around. But thanks to all.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,010
    Look at thread tools top right in the header of the thread.
    To help others, please post how you solved your problem.
    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

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

Similar Threads

  1. Public Functions and Private Subs
    By d9pierce1 in forum Programming
    Replies: 15
    Last Post: 11-28-2018, 09:25 AM
  2. Replies: 2
    Last Post: 07-16-2017, 05:48 PM
  3. Changing private sub dim to public sub dim
    By Stephenson in forum Programming
    Replies: 14
    Last Post: 10-05-2015, 02:59 PM
  4. Turning Private Function Into Public
    By g4tv4life in forum Programming
    Replies: 1
    Last Post: 02-04-2014, 05:31 PM
  5. Replies: 9
    Last Post: 12-20-2010, 08:05 PM

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