Home > Blockchain >  How to Find how many Letters does a Text Box's value has in VBA?
How to Find how many Letters does a Text Box's value has in VBA?

Time:01-26

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.

Also I the programming language is VBA and I do it in 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 INTERNET but I did not get A SINGLE RISULT. I Am a noob so someone help

CodePudding user response:

Use the AfterUpdate event of the textbox:

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

CodePudding user response:

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. For example:

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

MyLen = Len(MyString)    ' Returns 11.
  • Related