Consulting

Results 1 to 7 of 7

Thread: Pass variable from module to the userform.

  1. #1

    Pass variable from module to the userform.

    Hello. How to pass variable from module to the userform, namely to texbox.
    In order to do that I use that userform code, namely commandbutton code.
    Option Explicit
    Private Sub CommandButton1_Click()
    addr = Form2.RefEdit1.Value
    n1 = Form2.TextBox1.Value
    TextBox2 = g1
    'Unload Me
    End Sub
    ....
    And mudule code
    Public g1 As Double
    Sub Sim()
    Form2.Show
    g1=5
    End Sub
    And user form gives an output as 0 instead of 5.
    Should I call sub sim somehow or...?

  2. #2
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,600
    Location
    Jus guessing, but I think it'd work better to put the 5 into g1 before showing the user form

    Public g1 As Double Sub Sim()
    g1=5
    Form2.Show End Sub
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master xld's Avatar
    Joined
    Apr 2005
    Posts
    25,080
    Location
    I would do it differently, setup a form variable and pass the value to it as a property

    Option Explicit
    
    Private mcg1 As Double
    
    Public Property Let g1(Value As Double)
        mcg1 = Value
    End Property
    
    Private Sub CommandButton1_Click()
        addr = Form2.RefEdit1.Value
        n1 = Form2.TextBox1.Value
        TextBox2 = mcg1
        'Unload Me
    End Sub

    Sub Sim()
    Dim frm As Form2
    
        Set frm = Form2
        With frm
        
            .g1 = 5
            .Show
        End With
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Thank you for your replies.
    With .g1 I get "method or data member not found".
    I have one userform for input and output at the same time.
    And the problem is that when I use that code for userorm
    Option Explicit
    Private Sub CommandButton1_Click()
    addr = Form2.RefEdit1.Value
    n1 = Form2.TextBox1.Value
    s2 = Form2.TextBox6.Value
    TextBox2 = g1
    TextBox3 = g2
    TextBox4 = g3
    TextBox5 = g4
    Debug.Print s2
    'Unload Me
    End Sub
    and for a module
    Public g1 As Double, g2 As Double, g3 As Double, g4 As Double
    .........
    Sub Sim()
    Form2.Show
        Dim rng As Range, s As Integer, r As Double, bm As Double, sum(), s1 As Integer
        ......
        Set rng = Range(addr)
        Set frm = Form2
        If Not rng Is Nothing Then
                a1 = Application.Transpose(rng.Value2)
        End If
        n = n1
        s1 = s2
        .........
    With frm
    g1 = 1 
    g2 = 1 
    g3 = 1
    g4 = 1
       '.Show
    End With
    End Sub
    I get the results from a previous start of sub sim. In other word, the first start gives 0,0,0,0 and the second right answer 1,1,1,1.

  5. #5
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,600
    Location
    Just the very basic approach. You'll have to expand and finish it if you decide to go this way


    In a standard module

    Option Explicit
    
    Sub Sim()
        With UserForm1
            .g1 = 1
            .Show
        End With
    End Sub

    In the User Form's code module

    Option Explicit
    
    Public Property Let g1(x As Long)
        Me.TextBox1.Value = x
    End Property
    
    
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,785
    Location
    In a Module:
    Public g1 As Double
    
    Sub Sim()
      g1 = 5
    End Sub
    In Userform1:
    Private Sub CommandButton1_Click()
      Sim
      TextBox1 = g1
    End Sub
    IF the goal was to load controls before showing, I would:
    Sub Sim2()
      Sim
      Load UserForm1
      UserForm1.TextBox1 = g1
      UserForm1.Show
    End Sub

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master xld's Avatar
    Joined
    Apr 2005
    Posts
    25,080
    Location
    Quote Originally Posted by alex009988 View Post
    Thank you for your replies.
    With .g1 I get "method or data member not found".
    I have one userform for input and output at the same time.
    And the problem is that when I use that code for userorm
    Option Explicit
    Private Sub CommandButton1_Click()
    addr = Form2.RefEdit1.Value
    n1 = Form2.TextBox1.Value
    s2 = Form2.TextBox6.Value
    TextBox2 = g1
    TextBox3 = g2
    TextBox4 = g3
    TextBox5 = g4
    Debug.Print s2
    'Unload Me
    End Sub
    and for a module
    Public g1 As Double, g2 As Double, g3 As Double, g4 As Double
    .........
    Sub Sim()
    Form2.Show
        Dim rng As Range, s As Integer, r As Double, bm As Double, sum(), s1 As Integer
        ......
        Set rng = Range(addr)
        Set frm = Form2
        If Not rng Is Nothing Then
                a1 = Application.Transpose(rng.Value2)
        End If
        n = n1
        s1 = s2
        .........
    With frm
    g1 = 1 
    g2 = 1 
    g3 = 1
    g4 = 1
       '.Show
    End With
    End Sub
    I get the results from a previous start of sub sim. In other word, the first start gives 0,0,0,0 and the second right answer 1,1,1,1.

    That is not how I did it, re-read my post.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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