I did this code to check for the value in a set of textbox control array with the value in another set of textbox control array. But this works only for the Enter key in the keyboard. Can this be working when we use the mouse to move to the next textbox?
Private Sub txtnewHr_KeyPress(Index As Integer, KeyAscii As Integer)
Dim TempVal As Long
TempVal = 0
If KeyAscii = 13 Then
If Index + 1 <= txtnewHr.Count Then
If T > Val(txtnewHr(Index).Text) Then
txtoldhr(Index).ForeColor = &H80000008
txtoldhr(Index).Text = Val(txtoldhr(Index).Text) + (T - Val(txtnewHr(Index).Text))
txtnewHr(Index + 1).SetFocus
ElseIf T <= Val(txtnewHr(Index).Text) Then
If Sgn(Val(txtoldhr(Index).Text)) = 1 Or Sgn(Val(txtoldhr(Index).Text)) = 0 Then
txtoldhr(Index).Text = txtoldhr(Index).Text - (Val(txtnewHr(Index).Text) - T)
txtoldhr(Index).ForeColor = &H80000008
If Index + 1 < txtnewHr.Count Then
txtnewHr(Index + 1).SetFocus
Else
cmdAdd.SetFocus
End If
End If
If Sgn(Val(txtoldhr(Index).Text)) = -1 Then
txtoldhr(Index).ForeColor = &HFF&
MsgBox "Exceeded Available Hours!", vbExclamation, "Alert!"
txtnewHr(Index).SetFocus
SendKeys "{Home}+{End}"
Exit Sub
End If
End If
End If
End If
End Sub
Having difficulties understanding how the textboxes are processed... one is to one for txtnew and txtold? And what's the data they are handling? A time range?
Yes, one is to one. The data entered is hours. But the data type is number. So the issue is checking for the exceed in number between the txtnew and txtold.
T is a variable that stores the value that was there in the textbox when it gets the focus, that is T gets the value when the GotFocus event is triggered. The purpose is as follows:
There are 2 sets of textboxes called the txtnewhr and txtoldhr. There will always be values in the txtoldhr which is read from a table. When the user enters a value in the txtnewhr for the first time, it should always be lesser than the value in the txtoldhr. The next time the user picks that record, the values are shown in the txtnewhr and txtoldhr. Now, when the user wants to modify the value in the txtnewhr, it checks for T and if the value is reduced, the equivalent value is added to the txtoldhr and if the value is increased, the equivalent value is reduced from the txtoldhr. This is how it should work. So once the user enters a value and presses the Enter Key, the above code is executed. I hope it is clear now.
I will add the zip of the project.