Textbox character counr before submit

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,744
Office Version
  1. 2007
Platform
  1. Windows
Evening,
I have a userform where TextBox2 needs to have 17 characters entered.
It must be 17 or show Msgbox.


I am not sure if the code should be on that Textbox so when i leave it i see the Msgbox Or when i press the command button to transfer userform values to worksheet.
I also paste into Textbox 2 should you need to know.
Below i have something which kind of works on the Textbox but as soon as i strart to type 1 letter the msgbox is shown.

What does you advise.
Basically if Textbox2 isnt 17 characters then no values should be transfered to worksheet

Code:
Private Sub TextBox2_Change()
If TextBox2.Value <> 17 Then
   MsgBox "VIN MUST BE 17 CHARACTERS", vbCritical, "VIN CHARACTER COUNT MESSAGE"
   TextBox2.Value = ""
Else
Exit Sub
    TextBox2 = UCase(TextBox2)
    End If
End Sub

Userform code.

Code:
Private Sub CommandButton1_Click()
    Dim i As Integer
    Dim X As Long
    Dim ControlsArr(1 To 8) As Variant
    
    
    For i = 1 To 8
        If i > 2 Then
            With Me.Controls("ComboBox" & i)
                If .ListIndex = -1 Then
                    MsgBox "YOU MUST COMPLETE ALL FIELDS", vbCritical, "MC LIST TRANSFER"
                    TextBox1.SetFocus
                    Exit Sub
                Else
                    ControlsArr(i) = .Value
                End If
            End With
        Else
            ControlsArr(i) = Me.Controls("TextBox" & i).Value
        End If
    Next i
        
        Application.ScreenUpdating = False
        
        With ThisWorkbook.Worksheets("MC LIST")
            .Range("A8").EntireRow.Insert Shift:=xlDown
            .Range("A8:I8").Borders.Weight = xlThin
            .Cells(8, 1).Resize(, UBound(ControlsArr)).Value = ControlsArr
            

        End With
        Range("B8").Select
        Range("A8").Select
        ActiveWorkbook.Save

        Application.ScreenUpdating = True
        MsgBox "Database Has Been Updated", vbInformation, "SUCCESSFUL MESSAGE"
        
       With ThisWorkbook.Worksheets("MC LIST")
      
       If .AutoFilterMode Then .AutoFilterMode = False
            X = .Cells(.Rows.Count, 1).End(xlUp).Row
            .Range("A7:I" & X).Sort Key1:=Range("A8"), Order1:=xlAscending, Header:=xlGuess
            .Range("B8").Select
            .Range("A8").Select
        End With
        
        Unload McListForm
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
1. Test this by creating a NEW userform containing only TextBox1 and Texbox2

2. Place this code in the userform code window
VBA Code:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(Me.TextBox1.Text) < 17 Then Cancel = True
End Sub

Private Sub TextBox1_Change()
    Me.TextBox1.Value = Left(Me.TextBox1.Value, 17)
End Sub

3. Run the userform and
- enter abc into TextBox1, click on Textbox2 the code prevents user from leaving TextBox1
- keep adding characters def .... etc , click on Textbox2 the code prevents user from leaving TextBox1
- when 17 characters are in the textbox user is able to leave the TextBox1
- try entering 18 characters in TextBox1 the code prevents more than 17 characters being entered

4. If it does what you want, incorporate this method into your code
 
Upvote 0
Hi,
Yes it works but not being rude might be a bit extreme ?
Im thinking it might be best to only allow transfer to worksheet so check for 17 characters when command button pressed
 
Upvote 0
It should be simple enough by using the following but placing it in different locations im getting all kind of errors.

VBA Code:
If TextBox2.Value <> 17 Then
   MsgBox "VIN MUST BE 17 CHARACTERS", vbCritical, "VIN CHARACTER COUNT MESSAGE"
 
Upvote 0
Yes it works but not being rude might be a bit extreme ?
From that response I can only infer that you have never been an accountant! ;)

This is what you wrote in post#3
Im thinking it might be best to only allow transfer to worksheet so check for 17 characters when command button pressed

Perhaps this combination provides what you want

VBA Code:
Private Sub CommandButton1_Click()
    If Len(Me.TextBox2.Value) = 17 Then
        'CODE TO UPDATE WORKSHEET VALUES GOES HERE
    Else
        MsgBox "TextBox2 needs 17 characters" & vbCr & "worksheet was not updated", vbExclamation, "OOPS!"
    End If
End Sub

Private Sub TextBox2_Change()
'prevent user entering more than 17 characters
    Me.TextBox2.Value = Left(Me.TextBox1.Value, 17)
End Sub
 
Upvote 0
Morning,

I have only used the large code as the small code wasnt needed.

Thanks
 
Upvote 0
glad you solved your problem
thanks for your feedback
 
Upvote 0

Forum statistics

Threads
1,223,919
Messages
6,175,368
Members
452,638
Latest member
Oluwabukunmi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top