• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

pop up message if information not enter properly in textbox

I have a text box where you can only enter 12 alphanumerical characters. please see the below code.

If by any change the user doesn't input the 12 characters and go to a different textbox, I would like the user to receive a message saying "12 Characters required". I would like for the user not to be able to leave the textbox unless completed.

Code:
'Alphanumerical
Public Function AlphaNumericOnly(strSource As String) As String
    Dim i As Integer
    Dim strResult As String

    For i = 1 To Len(strSource)
        Select Case Asc(Mid(strSource, i, 1))
            Case 48 To 57, 65 To 90, 97 To 122: 'include 32 if you want to include space
                strResult = strResult & Mid(strSource, i, 1)
        End Select
    Next
    AlphaNumericOnly = strResult
 
End Function


'Character limit
Private Sub SerialNumberTextBox_Change()

' limit amount of characters
Me.SerialNumberTextBox.MaxLength = 10
'Characters uppercase
SerialNumberTextBox.Value = UCase(AlphaNumericOnly(SerialNumberTextBox.Value))

End Sub
 
Something like this ...
Code:
Public Function AlphaNumericOnly(strSource As String) As String
    Dim i As Integer
    Dim strResult As String
    Do
        Len_strSource = Len(strSource)
        For i = 1 To Len_strSource
            Select Case Asc(Mid(strSource, i, 1))
                Case 48 To 57, 65 To 90, 97 To 122: 'include 32 if you want to include space
                   strResult = strResult & Mid(strSource, i, 1)
            End Select
        Next i
        Len_strSource = Len(strResult)
        If Len_strSource <> 12 Then
            MsgBox ("12 Characters required")
            strSource = InputBox("InPut 12 Characters:", "Next try", strSource)
        End If
    Loop Until Len_strSource = 12
    AlphaNumericOnly = strResult
End Function
... couldn't test
 
Last edited:
Check this.

Code:
Private Sub SerialNumberTextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If (KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8 Or KeyAscii = 27 Or KeyAscii = 127 Then
    KeyAscii = KeyAscii
Else
    KeyAscii = 0
End If
End Sub
Private Sub SerialNumberTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(Me.caf.Value) <> 12 Then MsgBox "Pls input a 12 didgit value.", vbCritical: Cancel = True
End Sub
 
Hi

vletm - for some reason the code is not working

Deepak - I tried your code and I'm only able to enter numbers and I receive the Compile Error: Method or data member not found. Please see attachment..

I'm also having problem with the code for the comment Textbox. it suppose to make the first letter of the first word uppercase and after a dot.

Could you please help?
 

Attachments

  • Primary - Blue Print .xlsm
    119.3 KB · Views: 2
Hi !
Code:
Sub Demo()
    Dim SP$(), ST$()
    S$ = Application.Trim("hello newbie.   this is a comment.")
    SP = Split(S, ".")
For C& = 0 To UBound(SP) + (SP(UBound(SP)) = "")
                   ST = Split(SP(C))
    ST(-(ST(0) = "")) = Application.Proper(ST(-(ST(0) = "")))
                SP(C) = Join$(ST)
Next
     S = Join$(SP, ".")
    MsgBox S
End Sub
Do you like it ? So thanks to click on bottom right Like !
 
@eddyrcabrera79
How did You tested that?
...and I cannot even Show 'DinnerPlannerUserForm'.
The 1st error marked 'CurrentTimeTextBox.Value = Format(Time, "h:mm AM/PM")'
If modify that ... next error will be 'Format' ... this needs time...
 
working

Code:
' Serial number

Private Sub SerialNumberTextBox_Change()
' limit amount of characters
    Me.SerialnumberTextBox.MaxLength = 10
'Characters uppercase
End Sub
Private Sub SerialNumberTextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
        Case 48 To 57, 65 To 90, 97 To 122, 8, 27, 127: 'include 32 if you want to include space
            KeyAscii = KeyAscii
        Case Else
            KeyAscii = 0
    End Select
End Sub

Private Sub SerialNumberTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(Me.SerialnumberTextBox.Value) > 0 And Len(Me.SerialnumberTextBox.Value) < 10 Then MsgBox "Pls input a 10 digit value.", vbCritical: Cancel = True
End Sub
 
Thank you all guys. The code of Deepak is working for me. I'm not an excel VBA expert


Deepak - How can I make all the characters of the serial number Uppercase while typing

For the Comments Textbox. how can I make the entry like a paragraph, first letter of the first word uppercase and after a dot.

Thanks
 
How can I make all the characters of the serial number Uppercase while typing

change this

Code:
 Case 48 To 57, 65 To 90, 97 To 122, 8, 27, 127: 'include 32 if you want to include space
          KeyAscii = Asc(UCase(Chr(KeyAscii)))

For the Comments Textbox. how can I make the entry like a paragraph, first letter of the first word uppercase and after a dot.

You have already over looped the @Marc L solution.
 
Back
Top