Consulting

Results 1 to 2 of 2

Thread: passing string from userform to public module variable

  1. #1

    Arrow passing string from userform to public module variable

    Im trying to pass a string from a userform textbox to a public variable defined in a module. Cant for the life of me figure out whats happening. My code looks like this:

    Module code

    Option Explicit
    
    
    Public test As String
    
    
    Sub btn_showuserform()
    
    userform1.Show
    MsgBox test   
    
    End Sub
    Userform code

    Option Explicit
    
    Sub btnOk_Click()
    
    test = userformtextbox.Value
    Unload Me
    
    End Sub
    For some strange reason it works if I show the message box. But If i dont show the messagebox the value isnt passing to the variable??
    Also if i dont show the messge box but run the code from within in the userform code, the value does pass??

  2. #2
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello kieran989,

    Qualify the variable with the module name in the UserForm code. If the variable Test is in Module1 then you would use...
    Option Explicit 
     
    Sub btnOk_Click() 
         
        Module1.test = userformtextbox.Value 
        Unload Me 
         
    End Sub
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

Posting Permissions

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