PDA

View Full Version : Pass variable from module to the userform.



alex009988
07-15-2019, 10:30 AM
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...?

Paul_Hossler
07-15-2019, 11:02 AM
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

Bob Phillips
07-15-2019, 01:33 PM
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

alex009988
07-16-2019, 03:22 AM
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.

Paul_Hossler
07-17-2019, 07:32 PM
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

Kenneth Hobs
07-17-2019, 08:33 PM
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

Bob Phillips
07-18-2019, 05:48 AM
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.