Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Jun 2001
    Posts
    146
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Variable Scope Problem (W2000 SR-1)

    Hi,

    I'm trying to work with a variable value in a form event procedure. The variable is declared globally:

    Option Explicit
    Public ActiveUserName As String 'Active User Name

    The event procedure I'm having trouble with is this:

    Private Sub cmdCnclUserName_Click() 'Cancel button

    ActiveUserName = Application.UserName 'Retains a value during cancel operation
    frmUserName.Hide 'Hides form
    Unload frmUserName 'Unloads form
    End 'Ends processing

    End Sub

    The main problem is that ActiveUserName keeps coming up empty, despite the fact that it has been assigned a value in an earlier procedure.

    Thanks,
    Bob

  2. #2
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Variable Scope Problem (W2000 SR-1)

    Bob,

    Did you declare ActiveUserName in a standard module or the Form's Class module?

    If it was the Form's class module, then that's your problem. If you declare a variable as public, it is available only as a property of the form. To access the value use:

    debug.print UserForm1.ActiveUserName

    Now if this is in a standard module, are you certain that Application.UserName has a value?
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  3. #3
    2 Star Lounger
    Join Date
    Jun 2001
    Posts
    146
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Variable Scope Problem (W2000 SR-1)

    The standard module, I believe. It's entered before the procedure that brings up the form. That is:

    Option Explicit

    Public ActiveUserName As String 'Active User Name

    Sub ChangeUserName()

    code code code

    Load frmUserName

    code code code

    End Sub

    frmUserName is where the code in question appears.

    Application.UserName seems to have a value in the non-form code until I press the button that triggers the event in question.

    Thanks,
    Bob

  4. #4
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Variable Scope Problem (W2000 SR-1)

    OK, if it is in a standard module, then something is resetting the value.

    Try this and see if you can see what is resetting it:

    Open the module that has the Public ActiveUserName As String 'Active User Name line in it.

    Click on ActiveUserName

    Select Add Watch...

    In the context frame, change Procedure to {All Procedures} and Module to {All Modules}

    In the Watch Type frame select Break When Value Changes.

    Click OK and run the code again.

    What this will do for you is, when the value of ActiveUserName changes the code will stop and highlight the line after the one that changes the value.

    This should allow you to track down the line of code that is changing the value. Failing that, can you post your doc, with the form and modules required to the Lounge, so we can have a look at it.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •