vbams-access

Find how many characters in a Text Box's value in VBA


Is there a way to get the amount of the characters of a Text Box value so I can limit it to what I want?

If someone enters 134131323 as the Year, I can limit it to 4 characters.

The programming language is Microsoft Access VBA.

I tried txtTest.Value.Characters.


Solution

  • 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