• Variable Scope Problem (W2000 SR-1)

    Author
    Topic
    #381871

    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

    Viewing 0 reply threads
    Author
    Replies
    • #645373

      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?

      • #645393

        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

        • #645401

          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.

    Viewing 0 reply threads
    Reply To: Variable Scope Problem (W2000 SR-1)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: