Petr Kateřiňák
Petr Kateřiňák

Reputation: 107

VBA Textbox's change affect second textbox and vice versa

On my form I have more textboxes, but two of them are special. It's textbox with name tbVolume and textbox with name tbWeight.

If I change tbVolume, by other parameters is computed value for tbWeight, but when I added same procedure to tbWeight (to compute value for tbVolume), it creates some kind of cyclically link, because change of volume changes weight, change of weight changes volume, etc...

Does it exist some argument of tbVolume_Changed() / tbWeight_Changed() which can tell to procedure if value is changed by user or by application?

Or do you have another idea how to solve this twin-textbox problem?

Upvotes: 0

Views: 348

Answers (2)

Petr Kateřiňák
Petr Kateřiňák

Reputation: 107

Oh.. Finally it was so easy, only two new booleans and everything works fine:

Dim editingVolumeByApp As Boolean
Dim editingWeightByApp As Boolean

Private Sub tbVolume_Change()
  If editingVolumeByApp = False Then
    '...
    editingWeightByApp = True
    tbWeight.Value = finalVolume * CDbl(tbMatDensi.Value)
    editingWeightByApp = False
    '...
  End If
End Sub

Private Sub tbWeight_Change()
  If editingWeightByApp = False Then
    '...
    editingVolumeByApp = True
    tbVolume.Value = finalVolume * CDbl(tbMatComplCoef.Value)
    editingVolumeByApp = False
    '...
  End If
End Sub

And it works fine :-)

Upvotes: 2

Irithyll
Irithyll

Reputation: 90

Instead of tbWeight_Change() or tb_Volume_Change() you could use the following:

Private Sub tbWeight_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    ' do everything you want to do
End Sub

and of course:

Private Sub tbVolume_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    ' do everything you want to do
End Sub

You change the value of the textbox, exit it via tab or mouse click and the other value of the textbox will change.

If you want a parallel output while writing in the textbox you can use an extra textbox just for displaying the calculated value.

Upvotes: 0

Related Questions