PDA

View Full Version : [SOLVED:] Transfering Values From Textbox to Textbox



jarekmos
09-30-2019, 09:50 AM
Hi to all,
I am looking if someone can help me with figuring out a code for this. I don't know a lot about VBA, so it would be nice if someone could help.

So, I would type in a value in textbox 1 (for an example the number 10) and once I press the enter key, the number 10 would then transfer into textbox 2, then the textbox will automatically clear. And then again, if I typed the number 30 into textbox 1 then it would transfer into the second textbox again and it would add a plus sign in between. And then again with the number 5 and so on and so on. As you can see in the picture below.


25182

Thank you for any advice or help.

paulked
09-30-2019, 10:06 AM
Option Explicit


Private Sub TextBox1_AfterUpdate()
If TextBox2 = "" Then
TextBox2 = TextBox1
Else
TextBox2 = TextBox2 & "+" & TextBox1
End If
TextBox1 = ""
End Sub

Abd if you want to stay in Textbox1 add



Private Sub TextBox2_Enter()
TextBox1.SetFocus
End Sub

SamT
09-30-2019, 10:30 AM
To keep a running total

Option Explicit

Dim RunningTotal As Double

Private Sub TextBox1_AfterUpdate()
If TextBox2 = "" Then
TextBox2 = TextBox1
Else
TextBox2 = TextBox2 & "+" & TextBox1
End If
RunningTotal = RunningTotal + CDbl(TextBox1)
TextBox1 = ""
End Sub
nb: If you put a negative number in TB1, (-5,) TB2 will show "10+30+-5". but RunningTotal will be 35

jarekmos
09-30-2019, 10:37 AM
Thank you for the quick response, it's close to what I imagined, but it isn't quite there yet.
There are still two problems.
The first problem is that when I press enter the cursor won't set focus into the textbox 1 unless I press textbox 2.
The second problem is that if I were to want to change some of the numbers in textbox 2 that I already entered, it won't allow me to, and instead if I wanted to change some of the numbers a plus sign would show up at the end.

Do you have any advice or solutions for this problem.

paulked
09-30-2019, 10:42 AM
You didn't ask for that, but if youwant to enter tb2 then remove the tb2_enter sub.

What are you trying to do?

jarekmos
09-30-2019, 10:59 AM
Hi Paul,

I am entering a lot of numbers sometimes and if I make a mistake, I want the ability to fix those mistakes in TB2 instead of starting over again.
If I remove the "enter sub" then I would have to manually move the cursor, I wanted to make it more automatized

So what I wanted was.
1. I want to automatically set focus into TB1
2. The ability to correct numbers in TB2
3. I wanted TB2 to sum the numbers just like in Sam's post above.

jarekmos
09-30-2019, 11:01 AM
Sam,

Thanks for your reply.
That is what I was planning on doing in TB2, but I also want to multiply and divide, is this possible.

paulked
09-30-2019, 11:42 AM
Option Explicit


Private Sub TextBox1_AfterUpdate()
If TextBox1 = "" Then Exit Sub
If TextBox2 = "" Then
TextBox2 = TextBox1
Else
TextBox2 = TextBox2 & "+" & TextBox1
End If
TextBox1 = ""
End Sub




Private Sub TextBox2_Change()
Dim rt As Variant, t As Long, x As Long
On Error Resume Next
rt = Split(TextBox2, "+")
For x = 0 To UBound(rt)
'Debug.Print rt(x)
t = t + Val(rt(x))
Next
TextBox3 = t
End Sub


Private Sub TextBox3_Enter()
TextBox1.SetFocus
End Sub




Add another tb (Textbox3)
Set TabStop to false for Textbox2

paulked
09-30-2019, 11:44 AM
That is what I was planning on doing in TB2, but I also want to multiply and divide, is this possible.

That's new!

jarekmos
09-30-2019, 03:10 PM
Everything looks good.
I am able to do almost everything and even change some of the numbers in textbox 2, but there is still a small issue with the setfocus to TB1, after I press enter I have to manually click on TB1 to have the cursor show up. That is one thing, but I still have one more question.
Is it possible to add subtraction, multiplication and division to the code?

Thanks Paul

paulked
09-30-2019, 03:28 PM
Add another tb (Textbox3)
Set TabStop to false for Textbox2

If you follow the instruction in my post it works, see attached.

To do other mathematical functions will take a lot more programming, it would probably be easier to transfer the data to a sheet and do it there. Have a play!

25183

jarekmos
09-30-2019, 04:28 PM
After I looked into your attachment I realized that the reason why the cursor isn't setfocusing onto TB1 is because I have a button and it automatically setsfocus onto the button.
I tried it out with your attachment as well and it also setsfocus onto the button.

Is there a way to fix this, or can you tell me how to fix it. I also tried to change the TabIndex in the properties window, but it doesn't work either way.

Thank you

paulked
09-30-2019, 04:37 PM
You could do the same as textbox2 with the button and set tabstop to false.

In the meantime I had a play around and came up with the attached.

Very simple, but after entering your number in the tb you then press the function you want. Again, have a play and you could adapt this to suit your needs.

25184

jarekmos
10-01-2019, 08:18 PM
Thanks for helping, the code is simple and it works.
I will make a couple of changes to it to make it work for me, so thanks again.

Best Regards

paulked
10-01-2019, 10:49 PM
Glad to have helped. Please mark it solved (Thread Tools, top right)