PDA

View Full Version : max value of another textbox



av8tordude
02-06-2011, 11:03 AM
If I enter $42.87 in textbox1, how can I restrict the value in textbox 2 to not exceed the vaue in textbox1?

Thank you

mikerickson
02-06-2011, 11:25 AM
Perhaps something like

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
With TextBox1
.Text = Format(NumFromCur(.Text), "$#.00")
If NumFromCur(.Text) < NumFromCur(TextBox2.Text) Then
TextBox2.Text = vbNullString
End If
End With
End Sub

Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
With TextBox2
.Text = Format(NumFromCur(.Text), "$#.00")
Cancel = NumFromCur(TextBox1.Text) < NumFromCur(TextBox2.Text)
If Cancel Then
MsgBox "TextBox2 must not exceed TextBox1"
.SelStart = 0
.SelLength = Len(.Text)
End If
End With
End Sub

Function NumFromCur(aString As String) As Double
NumFromCur = Val(Replace(aString, "$", vbNullString))
End Function

av8tordude
02-06-2011, 11:44 AM
Thank you Mike. Works like a charm! :beerchug: