设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 3166|回复: 6
打印 上一主题 下一主题

How to control Tabbing and "Enter" key movement in and out of a s

[复制链接]

点击这里给我发消息

跳转到指定楼层
1#
发表于 2005-8-15 19:04:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
标题: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.
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2005-8-15 20:59:00 | 只看该作者
翻译:如何用Shift键和Tab键实现光标在主窗体和子窗体控件的自由跳转.

原作者:ATTAC 咨询组织

   主窗体当前光标所在控件的下一个按件是子窗体时,这时用户按Tab键或回车键,光标会自动跳到子窗体的第一个控件。但是, 如果用户按住Shift键和然后按Tab键(想返跳回主窗体), 不是跳回主窗体,而是光标会跳到所在子窗体最后的控件(光标在第一记录的第一个控件时),或跳到上一条记录最后一个控件。同样, 如果光标在子窗体最后一个控件时,按回车键, 他们会跳到所在子窗体的下一条记录。而不能跳回主窗体的下一个控件。

    这个问题可以在子窗体的第一条记录的第一个控件和最后一条记录的最后一个控件使用“键按下事件”,实现实现主窗体和子窗体的自由跳转。代码如下。 (注意:子窗体引用主窗体时可以使用parent这个属性):

1.在模块的声明页里,写入下面代码:

Public Const Key_Tab = &H9                  '在这里记得要加上Public,否则窗体无法调用

Public Const Key_Return = &HD

Public Const SHIFT_MASK = 1

2.在子窗体第一个控件的键按下事件里:写入以下代码:

ShiftDown = (Shift And SHIFT_MASK) > 0

   If KeyCode = Key_Tab Then

        If ShiftDown Then

            Me.Parent!某控件.SetFocus  

            KeyCode=0

       End If

   End If

在子窗体最后一个控件的键按下事件里:写入以下代码:

   ShiftDown = (Shift And SHIFT_MASK) > 0

    If KeyCode = Key_Tab Then

        If ShiftDown = 0 Then

            KeyCode = 0

            Me.Parent!某控件.SetFocus

        End if

    ElseIf KeyCode = Key_Return Then

           KeyCode = 0

           Me.Parent!某控件.SetFocus

    Else Exit Sub

    End If

如果是直接跳到下一条记录而不想跳回主窗体,只要去掉“Me.Parent!某控件.SetFocus”这行即可。

[em01][em01][em01]

[此贴子已经被作者于2005-8-15 17:19:08编辑过]

点击这里给我发消息

3#
发表于 2005-8-16 05:27:00 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
4#
发表于 2005-8-19 18:11:00 | 只看该作者
今天发现一个正常的现象,按Tab键可以从跳到子窗体的第一个控件,继续按Tab键,到最后一控件时,又可以跳回主窗体接着的下一控件...如果是这样的话,我们就不需要像上面操作了................纳闷中...可能版本不同原因吧....[em01][em01][em01]
5#
发表于 2005-9-23 01:48:00 | 只看该作者
I don't know too!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-3 05:09 , Processed in 0.081252 second(s), 28 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表