• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

how to get the value of textbox on form 1 to the another form

mahaveer

Member
i have form 1 which have two text box and one command button..

and i have form 2 too with one text box only.

now my problem is that i want to put the value in textbox 1 and textbox 2 on the form1 and when i press the command button on form 1 then the total value of textbox1 and textbox2 should go to text box1 on form2. but how?


in short i just want the total value of textbox1 and textbox2 on form2.
 
Hi Mahaveer,


You can put the below code in click event of commandbutton on userform1.


UserForm2.TextBox1.Value = CInt(UserForm1.TextBox1) + CInt(UserForm1.TextBox2)

Unload Me

UserForm2.Show


Hope this may help to resolve your problem.


Please let me know if you find any issue.


Thanks & Regards,

Anupam Tiwari
 
Hi Anupam,


I think using 'CInt' will cause the value to show only the integer portion and may give incorrect results.

[pre]
Code:
Sub Test()
Dim a1 As Single, a2 As Single, a3 As Single, a4 As Single, a5 As Single
a1 = 2.3
a2 = 3.2
a3 = a1 + a2
a4 = CDec(a1) + CDec(a2)
a5 = CInt(a1) + CInt(a2)
MsgBox a3 & vbCrLf & a4 & vbCrLf & a5
End Sub
[/pre]

If a Type converter needs to be used then cDec or cDbl would be better.
 
Hi shrivallabha,


Thanks for correcting me.


I have tweaked my above code to below that could be much better now.


1. Code to put in click event of commandbutton on userform1.

[pre]
Code:
If IsNumber(UserForm1.TextBox1.Value) = False Or IsNumber(UserForm1.TextBox2.Value) = False Then
UserForm1.TextBox1.Value = ""
UserForm1.TextBox2.Value = ""
MsgBox "The value in text box is not a valid number.", , "invalid number"
Exit Sub
Else
UserForm2.TextBox1.Value = CDbl(UserForm1.TextBox1) + CDbl(UserForm1.TextBox2)
Unload Me
UserForm2.Show
End If
2. Code to put in a module:

Function IsNumber(txt As String)
a = 123456780.9
IsNumber = True
For i = 1 To Len(txt)
If InStr(1, a, Mid(txt, i, 1)) <= 0 Then
IsNumber = False
Exit Function
End If
Next
End Function
[/pre]
Please do let me know if is there any suggestions.


Thanks & Regards,

Anupam Tiwari
 
Anupam


When posting code put a single ` in front of and after the code so it maintains its indents

The ` is the character next to the 1 key (Top left of the keyboard) not the ' next to the Enter key
 
Hi Hui,


I was not sure out of the above two symbols which to use for indentation however thanks a lot for your help to clear my confusion.


I will remeber to use "`" while posting code.


Thanks & Regards,

Anupam Tiwari
 
Back
Top