Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn And TypeOf Me.ActiveControl Is TextBox Then
NextTextBox Me.ActiveControl
End If
End Sub
Private Sub NextTextBox(tbx As TextBox)
Dim ctl As Control
Dim tb As TextBox
Dim FoundIt As Boolean
Dim curindex As Integer
curindex = tbx.TabIndex
[COLOR=black cyan]' Look for the TextBox with the next highest TabIndex Value[/color]
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox And ctl.TabIndex > tbx.TabIndex Then
If FoundIt Then
If tb.TabIndex > ctl.TabIndex Then
Set tb = ctl
FoundIt = True
End If
Else
Set tb = ctl
FoundIt = True
End If
End If
Next
[COLOR=black cyan]' If you didn't find one then look for the TextBox with the
' smallest TabIndex value.[/color]
If Not FoundIt Then
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Then
If Not FoundIt Then
Set tb = ctl
FoundIt = True
ElseIf tb.TabIndex > ctl.TabIndex Then
Set tb = ctl
FoundIt = True
End If
End If
Next
End If
If FoundIt Then tb.SetFocus
End Sub