标题: How to control Tabbing and "Enter" key movement in and out of a s [打印本页] 作者: tmtony 时间: 2005-8-15 19:04 标题: How to control Tabbing and "Enter" key movement in and out of a sub-Form an 标题:How to control Tabbing and "Enter" key movement in and out of a sub-Form and a Main Form.
原作者:ATTAC Consulting Group
When a user presses the tab key or the enter key when they are in the last field on a main form, where the next field is a sub form, this will automatically result in the cursor moving to the first control in the subform. However, if the user then holds down the Shift key and presses the tab key, (to move backward in the form,) rather than re-entering the main form, the cursor will either move to the last field on the sub form (if on the first record of the sub form,) or to the previous record in the sub form. Similarly, if the user is on the last control in the sub form and presses the enter key, they will be taken to the next record in the sub form, rather than to next control in the main form. This behavior can be modified to progress directly between the main form and current record of the sub form by using event procedures in the "On Key Down" event of the sub-Form's first and last controls. To do this use the code below. (Note the "arent" property of the sub form refers to the main form):
1. On the Declarations page of the Form's module enter the following lines: Const Key_Tab = &H9
Const Key_Return = &HD
Const SHIFT_MASK = 1
2. In the "On Key Down" event of the first control on the sub-form create an event procedure and enter the following code: ShiftDown = (Shift And SHIFT_MASK) > 0
If KeyCode = Key_Tab Then
If ShiftDown Then
Me.Parent!SomeControl.SetFocus
KeyCode=0
End If
End If
3. In the "On Key Down" event of the last control on the sub-form create an event procedure and enter the following code: ShiftDown = (Shift And SHIFT_MASK) > 0
If KeyCode = Key_Tab Then
If ShiftDown = 0 Then
KeyCode = 0
Me.Parent!SomeControl.SetFocus
End if
ElseIf KeyCode = Key_Return Then
KeyCode = 0
Me.Parent!SomeControl.SetFocus
Else Exit Sub
End If
To stop the user from re-entering the main form, without moving to the next record, simply remove the lines of code which set the focus on a control of the Main Form.
Access 95 and 97: If you need to trap key actions and movement in Access 95 and 97, especially if it involves multiple controls and/or sub-forms (e.g. sub-forms on a tab control,) you can save time and coding by creating a single function for the form by setting the Key Preview property of the form to "Yes", and writing a single function in the OnKeyDown event of the form. You can use a select case routine to test the CurrentControl.name and set your form movement from there. 作者: 爱情插班生 时间: 2005-8-15 20:59
翻译:如何用Shift键和Tab键实现光标在主窗体和子窗体控件的自由跳转.