How can I SWITCH the contents of 2 textboxes by clicking on a command button?
I tried to solve it this way :

Private Sub Command1_Click()
Text1.Text = Text2.Text
Text2.Text = Text1.Text
End Sub

But it didn't work out and it only changes the content of text1
Thanks in advance :)

Recommended Answers

All 4 Replies

That's because by the time Text2.Text = Text1.Text executes, Text1.Text has already been changed. Try:

Dim Txt as String
Txt = Text1.Text
Text1.Text = Text2.Text
Text2.Text = Txt

You need an extra variable. After you say: Text1.Text = Text2.Text text1 and text2 will both have the same text (from text2), so this Text2.Text = Text1.Text is useless.

Use something like:

tmp = Text1.Text
Text1.Text = Text2.Text
Text2.Text = tmp

[edit]
Crossposted with scbwv

Thank you SCBWV
Your answer is correct :)

Actually, you don't need to declare a variable. You can use the tag property for something like this...

Option Explicit

Private Sub Command1_Click()
Text1.Tag = Text2.Text
Text2.Text = Text1.Text
Text1.Text = Text1.Tag
End Sub

Read up on it...

Good Luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.