简体   繁体   English

如何查找文本框的值在 VBA 中有多少个字母?

[英]How to Find how many Letters does a Text Box's value has in VBA?

I Wish there is a way to get the amount of the characters of a Text Box Value so I can limit it to be the amount that I wanted.我希望有一种方法可以获取文本框值的字符数量,这样我就可以将其限制为我想要的数量。 so like when someone entered 134131323 as the Year of Date , I can limit it to 4 characters .所以就像有人输入134131323作为日期年份时,我可以将其限制为4 个字符

Also I the programming language is VBA and I do it in Microsoft Access .我的编程语言也是VBA ,我在Microsoft Access中进行。

I Did try to get it working with doing txtTest.Value.Characters idk but that did not work obviously and I DID SEARCH THE WHOLE INTE.NET but I did not get A SINGLE RISULT.我确实尝试通过执行txtTest.Value.Characters idk 来让它工作,但这显然不起作用,我确实搜索了整个 INTE.NET,但我没有得到一个单一的结果。 I Am a noob so someone help我是菜鸟所以有人帮忙

You can use the Len function available in VBA which returns a long containing the number of characters in a string or the number of bytes required to store a variable.您可以使用 VBA 中可用的Len function ,它返回一个长整数,其中包含字符串中的字符数或存储变量所需的字节数。 For example:例如:

Dim MyString, MyLen
MyString = "Hello World"    ' Initialize variable.

MyLen = Len(MyString)    ' Returns 11.

Use the AfterUpdate event of the textbox:使用文本框的AfterUpdate事件:

Private Sub YourTextbox_AfterUpdate()

    Const MaxLength As Integer = 4
    Dim Text        As String
    
    Text = Nz(Me!YourTextbox.Value)
    
    If Len(Text) > MaxLength Then
        Me!YourTextbox.Value = Left(Text, MaxLength)
    End If

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM