vba 打字时更新文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11986227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:19:56  来源:igfitidea点击:

Update textbox during typing

vbams-accesstextboxaccess-vbaauto-update

提问by Paolo Bernasconi

In Access, I have a form in which there are three textboxes. I am trying to update a textbox called tbxCombinedNamewith a combination of both:

在 Access 中,我有一个表单,其中包含三个文本框。我正在尝试使用两者的组合更新名为tbxCombinedName的文本框:

  • textbox tbxLastName(person's Last Name)
  • textbox tbxFirstName(person's First Name)
  • 文本框tbxLastName(人的姓氏)
  • 文本框tbxFirstName(人的名字)

My question is: what textbox property do I use, so that as I am typing text in tbxLastName, the CombinedName textbox is updated immediatelyand thereafter saved in the table Contacts.

我的问题是:我使用什么文本框属性,以便当我在tbxLastName 中输入文本时,CombinedName 文本框会立即更新,然后保存在表Contacts 中

On Microsoft's website, I have found that the step processes when typing in a textbox are as follows:

微软的网站上,我发现在文本框中输入时的步骤过程如下:

KeyDown → KeyPress → BeforeInsert → Change → KeyUp

KeyDown → KeyPress → BeforeInsert → Change → KeyUp

I've tried using the OnChange and OnKeyDown properties, but to no avail. Which property, combined with what code, will allow the update-as-you-type action to work?

我试过使用 OnChange 和 OnKeyDown 属性,但无济于事。哪个属性与哪个代码相结合,将允许按您键入操作进行更新?

This is what I wrote earlier, which didn't work:

这是我之前写的,但不起作用:

Private Sub tbxLName_change()

Dim lastName As String
Dim nameCode As String

lastName = tbxLName.Value
Debug.Print lastName
nameCode = tbxNameCode.Value
nameCode = lastName
Debug.Print nameCode

End Sub

Thanks for all your help in advance.

提前感谢您的所有帮助。

回答by Fionnuala

This is one of the few cases where you should refer to the .text property.

这是您应该参考 .text 属性的少数情况之一。

In the Change event:

在 Change 事件中:

lastName = tbxLName.Text

The .text property is only available when a control has focus and it refers to the visible contents of the control.

.text 属性仅在控件具有焦点并且引用控件的可见内容时可用。

However, this is a database and the general rule is that you no not store calculated fields. The full name can easily be obtained from a query.

但是,这是一个数据库,一般规则是您不能不存储计算字段。可以从查询中轻松获得全名。

回答by ray

Just a couple of notes:

只是一些注意事项:

You may want to go with KeyPress because it provides the ability to change or negate the key the user pushed.

您可能希望使用 KeyPress,因为它提供了更改或取消用户按下的键的能力。

In the example below, only letters are allowed and lower case letters are upper cased:

在下面的例子中,只允许使用字母,小写字母大写:

Private Sub tbxLName_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        Const ASCII_LOWER_RANGE = 65
        Const ASCII_UPPER_RANGE = 122
        Const ASCII_LOWER_A = 97
        Const ASCII_LOWER_Z = 122
        Const UPPER_MODIFIER = -32
        Const ASCII_CANCEL_CODE = 0

        Select Case KeyAscii
            Case ASCII_LOWER_RANGE To ASCII_UPPER_RANGE
                If KeyAscii >= ASCII_LOWER_A And KeyAscii <= ASCII_LOWER_Z Then
                    KeyAscii = KeyAscii + UPPER_MODIFIER
                End If
            Case Else
                KeyAscii = ASCII_CANCEL_CODE  'Cancel Key Press
        End Select
End Sub