Results 1 to 4 of 4
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Variable Retention (XP; SR3)

    I would like to utilize a form to capture a UserId and Password, close the form and them be able to keep the variables for the UserId and Password. My issue is when I close the form; I lose both variables.

    Code of Form:
    Public mstrfUser, mstrPassword As String

    Private Sub CommandButton1_Click()
    If TextBox1.Value <> "" And TextBox2.Value <> "" Then
    mstrfUser = TextBox1.Value
    mstrfPassword = TextBox2.Value
    Unload PWord
    Else
    Response = MsgBox(Title:="Opps....", Prompt:="UserID or Password is blank...Try Again.")
    End If
    Application.Wait Now + TimeValue("00:00:02")
    End Sub

    Private Sub CommandButton2_Click()
    TextBox2.Value = ""
    Unload PWord
    End
    End Sub

    Private Sub UserForm_Initialize()
    TextBox2.PasswordChar = "*"
    End Sub


    Code from Module:
    Sub BeginRountin01()
    PWord.Show
    Msgbox mstrfUser & " - " & mstfrPassword 'a blank UserId and Password appear
    End Sub


    Thanks,
    John

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Variable Retention (XP; SR3)

    Put the declaration

    Public mstrfUser As String, mstrPassword As String

    in the module, not in the code behind the form.

    Note: you must specify the type for each variable separately. In the declaration as posted by you, the type for mstrUser is not specified, hence it will be a Variant.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Variable Retention (XP; SR3)

    Added note: please select Tools | Options... in the Visual Basic Editor and tick the check box "Require Variable Declaration".
    This will add a line Option Explicit to each new module.
    It is a good idea to add this line manually to existing modules.
    This would have alerted you to the fact that the variables are not known outside the userform if you declare them there.

  4. #4
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Variable Retention (XP; SR3)

    Hans,

    Your suggestion worked.

    Thanks,
    John

Posting Permissions

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