1
votes

In my MS Access continuous form I have two text boxes, TextBox1 and TextBox2. In TextBox1 I get the value from a table field of database and in TextBox2 I want to show the value of the first textbox and I want to do it using VBA program.

What I am currently doing, on form load

Private Sub Form_Load()
Dim val As String
val = Me.TextBox1 
TextBox2.Value = val
End Sub

But this show only the first element of the TextBox1 value in all TextBox2 value. TextBox2 value do not change with the change of TextBox1 value.

For example, Currently my output is like this

TextBox1  TextBox2
Tom       Tom
Tommy     Tom
Sam       Tom
Sammy     Tom

But I want the output like this

TextBox1  TextBox2
Tom       Tom
Tommy     Tommy
Sam       Sam
Sammy     Sammy

That is I want to take the value of TextBox1 and put it in TextBox2 dynamically and I want to do it using code builder.

I want to do it using vba code. Because I have to do lot of calculation on the value of TextBox1 and then show a result in TextBox2. How can I do this?

3

3 Answers

1
votes

If TextBox2 should display an exact duplicate of TextBox1, the simplest method is to bind it the same way as TextBox2 - using the same ControlSource.

1
votes

It sounds like TextBox2 is an unbound text box (its Control Source is blank). Therefore the continuous form displays multiple copies of that text box, all with the same value. When you change its value, that new value appears in all the copies.

Since you clarified you don't want TextBox2 to contain the same value as TextBox1 (despite what you asked for in the question), use the TextBox2 Control Source to apply your calculation. It could be an expression which computes your result based on the value of TextBox1. Or you could create a custom VBA function to do the calculation and apply the function to the value of TextBox1:

=Round([TextBox1] / 100, 2)
=YourCustomFunction([TextBox1])
0
votes

put this in the textbox1's change event:

me.textbox2.value = me.textbox1.value

This is probably all you need to set box2 the same as box1

Just a note, the form load event will only fire once(when the form first loads) where as everytime the textbox changes the change event will fire. When in debug mode you could put break points around and then you will know when events fire, that is if you dont know.