Page 3 of 3 FirstFirst 123
Results 81 to 100 of 100

Thread: How to restrict users to enter first character as alphabet and rest as numeric.

  1. #81
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    I don't think seraphicmortal fully explored the class I uploaded. Since that allows you to change the popup menu that is used when you right click the textbox you can either remove the Paste command or implement your own Paste procedure in which you of course have full control of the Paste procedure. That means you can examine the clipboard content before you insert it and edit the content so you don't paste any illegal characters.

  2. #82
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    Sometimes I like annoying ppl }

    Ok, this is better then
    VB Code:
    1. Private Sub Text1_Change()
    2.     Text1.Text = TestFormat(Text1.Text)
    3.     Text1.SelStart = Len(Text1.Text)
    4. End Sub
    5.  
    6. Private Function TestFormat(ByRef Text As String) As String
    7.     Dim sAlpha As String
    8.     Dim lX As Long
    9.     Dim lMaxLen As Long
    10.    
    11.     If Len(Text) = 0 Then Exit Function
    12.     lMaxLen = 4 ' or how ever long you want it to be
    13.     sAlpha = "abcdefghijklmnopqrstuvwxyz"
    14.     Do While InStr(1, sAlpha, Left$(Text, 1), vbTextCompare) = 0
    15.         Text = Mid$(Text, 2)
    16.         If Len(Text) = 0 Then Exit Function
    17.     Loop
    18.     For lX = 2 To Len(Text)
    19.         If Not IsNumeric(Mid$(Text, lX, 1)) Then
    20.             Mid$(Text, lX, 1) = Chr$(0)
    21.         End If
    22.     Next
    23.     Text = Replace$(Text, Chr$(0), vbNullString)
    24.     If Len(Text) > lMaxLen Then
    25.         Text = Left$(Text, lMaxLen)
    26.     End If
    27.     TestFormat = UCase$(Text)
    28. End Function
    Last edited by longwolf; May 24th, 2005 at 10:25 PM.

  3. #83
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    Well the thread started has marked it as resolved so we could just stop posting . BTW baja_yu, thanks for the rating.

  4. #84

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    Hey Joacim, I'm still gona try ur suggestion though!! Thanks for the inputs. =)
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

  5. #85
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    @seraphicmortal: I got your PM but I don't understand what it is that gives you the error. Are you trying to assign a menu to the class?

  6. #86

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Question Re: How to restrict users to enter first character as alphabet and rest as numeric.

    I just pasted ur code to my program and change txtbox to the name of my textbox and placed the following in my global.bas:
    VB Code:
    1. Public mnuContext As New txtbox
    2. Public mErrorColor As Long

    then in my frm_field(form where my textbox are placed):
    VB Code:
    1. Private Sub txt_qty_Change()
    2. '    Call Validate_Entry(txt_qty)
    3.     Dim nSelStart As Long, nSelLen As Long
    4.     If Len(txt_qty.Text) Then
    5.         If (UCase$(txt_qty.Text) Like "[A-Z]" & String(Len(txt_qty.Text) - 1, "#")) Then
    6.             If UCase$(txt_qty.Text) <> txt_qty.Text Then
    7.                 nSelStart = txt_qty.SelStart
    8.                 nSelLen = txt_qty.SelLength
    9.                 txt_qty.Text = UCase$(txt_qty.Text)
    10.                 txt_qty.SelStart = nSelStart
    11.                 txt_qty.SelLength = nSelLen
    12.             End If
    13.             txt_qty.BackColor = vbWindowBackground
    14.         Else
    15.             txt_qty.BackColor = mErrorColor
    16.         End If
    17.     Else
    18.         txt_qty.BackColor = vbWindowBackground
    19.     End If
    20. End Sub
    21.  
    22. Private Sub txt_qty_KeyPress(KeyAscii As Integer)
    23.     Dim nSelLen As Long
    24.  
    25.     nSelLen = txt_qty.SelLength
    26.     If txt_qty.SelStart = 0 And txt_qty.SelLength = 0 Then
    27.         'This will ensure that the first character is replaced if
    28.         'the user has moved the text caret to the beginning of the text
    29.         txt_qty.SelLength = 1
    30.     End If
    31.     KeyAscii = Asc(UCase$(Chr$(KeyAscii))) 'capitilize
    32.     Select Case KeyAscii
    33.         Case vbKeyBack
    34.             txt_qty.SelLength = nSelLen
    35.         Case vbKeyA To vbKeyZ
    36.             If txt_qty.SelStart <> 0 Then
    37.                 KeyAscii = 0
    38.                 txt_qty.SelLength = nSelLen
    39.             End If
    40.         Case vbKey0 To vbKey9
    41.             If txt_qty.SelStart = 0 Then
    42.                 KeyAscii = 0
    43.                 txt_qty.SelLength = nSelLen
    44.             End If
    45.         Case Else
    46.             KeyAscii = 0
    47.             txt_qty.SelLength = nSelLen
    48.     End Select
    49. End Sub
    50.  
    51. Private Sub txt_qty_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    52.     If Not mnuContext Is Nothing Then
    53.         If Button And vbRightButton Then
    54.             'surpress the default context menu and show our custom menu instead
    55.             txt_qty.Enabled = False
    56.             txt_qty.Enabled = True
    57.             txt_qty.Parent.PopupMenu mnuContext
    58.         End If
    59.     End If
    60. End Sub

    then in my class(txtbox.cls):
    VB Code:
    1. Option Explicit
    2.  
    3. Private WithEvents txtbox As TextBox
    4. Private mnuContext As Menu
    5. Private mErrorColor As Long
    6.  
    7. Public Sub Init(TextBox As TextBox, _
    8.                 Optional ErrorColor As Long = vbRed, _
    9.                 Optional ContextMenu As Menu = Nothing)
    10.     Set mnuContext = ContextMenu
    11.     m_ErrorColor = ErrorColor
    12.     Set txtbox = TextBox
    13. End Sub
    14.  
    15. Public Property Get ErrorColor() As Long
    16.     ErrorColor = mErrorColor
    17. End Property
    18.  
    19. Public Property Let ErrorColor(nNew As Long)
    20.     mErrorColor = nNew
    21. End Property
    22.  
    23. Public Property Set TextBox(txt As TextBox)
    24.     Set txtbox = txt
    25. End Property
    26.  
    27. Public Property Get TextBox() As TextBox
    28.     Set TextBox = txtbox
    29. End Property
    30.  
    31. Public Property Set ContextMenu(mnu As Menu)
    32.     Set mnuContext = mnu
    33. End Property
    34.  
    35. Public Property Get ContextMenu() As Menu
    36.     Set ContextMenu = mnuContext
    37. End Property
    38.  
    39. Private Sub Class_Initialize()
    40.     mErrorColor = vbRed
    41. End Sub

    This is what I did(where I pasted your codes..). Can you tell me why it still had errors?
    Thank you so much.
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

  7. #87
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    This goes into frm_Field:
    VB Code:
    1. Option Explicit
    2.  
    3. Private oValidateText As CTextFormat
    4.  
    5. Private Sub Form_Load()
    6.     Set oValidateText = New CTextFormat
    7.     Set oValidateText.TextBox = Text1
    8.  
    9.     Call oValidateText.Init(Text1, vbYellow, mnuPopUp)
    10. End Sub


    Now, just Add the Class to your project (the attached file from page 2)




    Bruce.
    Last edited by Bruce Fox; May 25th, 2005 at 07:44 PM.

  8. #88
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    Form code (one textbox called oValidateText is on it):

    VB Code:
    1. Option Explicit
    2.  
    3. Private oValidateText As CTextFormat
    4.  
    5. Private Sub Form_Load()
    6.     Set oValidateText = New CTextFormat
    7.     Set oValidateText.TextBox = Text1
    8.  
    9.     Call oValidateText.Init(Text1, vbYellow, mnuPopUp)
    10.    
    11. End Sub

    Class module code (class is called CTextFormat):

    VB Code:
    1. Option Explicit
    2.  
    3. Private WithEvents txtBox As TextBox
    4. Private mnuContext As Menu
    5. Private m_ErrorColor As Long
    6.  
    7. Public Sub Init(TextBox As TextBox, Optional ErrorColor As Long = vbRed, Optional ContextMenu As Menu = Nothing)
    8.     Set mnuContext = ContextMenu
    9.     m_ErrorColor = ErrorColor
    10.     Set txtBox = TextBox
    11. End Sub
    12.  
    13. Public Property Get ErrorColor() As Long
    14.     ErrorColor = m_ErrorColor
    15. End Property
    16.  
    17. Public Property Let ErrorColor(nNew As Long)
    18.     m_ErrorColor = nNew
    19. End Property
    20.  
    21. Public Property Set TextBox(txt As TextBox)
    22.     Set txtBox = txt
    23. End Property
    24.  
    25. Public Property Get TextBox() As TextBox
    26.     Set txtBox = txtBox
    27. End Property
    28.  
    29. Public Property Set ContextMenu(mnu As Menu)
    30.     Set mnuContext = mnu
    31. End Property
    32.  
    33. Public Property Get ContextMenu() As Menu
    34.     Set ContextMenu = mnuContext
    35. End Property
    36.  
    37. Private Sub Class_Initialize()
    38.     m_ErrorColor = vbRed
    39. End Sub
    40.  
    41. Private Sub txtBox_Change()
    42.     Dim nSelStart As Long, nSelLen As Long
    43.     If Len(txtBox.Text) Then
    44.         If (UCase$(txtBox.Text) Like "[A-Z]" & String(Len(txtBox.Text) - 1, "#")) Then
    45.             If UCase$(txtBox.Text) <> txtBox.Text Then
    46.                 nSelStart = txtBox.SelStart
    47.                 nSelLen = txtBox.SelLength
    48.                 txtBox.Text = UCase$(txtBox.Text)
    49.                 txtBox.SelStart = nSelStart
    50.                 txtBox.SelLength = nSelLen
    51.             End If
    52.             txtBox.BackColor = vbWindowBackground
    53.         Else
    54.             txtBox.BackColor = m_ErrorColor
    55.         End If
    56.     Else
    57.         txtBox.BackColor = vbWindowBackground
    58.     End If
    59. End Sub
    60.  
    61. Private Sub txtBox_KeyPress(KeyAscii As Integer)
    62.     Dim nSelLen As Long
    63.    
    64.     nSelLen = txtBox.SelLength
    65.     If txtBox.SelStart = 0 And txtBox.SelLength = 0 Then
    66.         'This will ensure that the first character is replaced if
    67.         'the user has moved the text caret to the beginning of the text
    68.         txtBox.SelLength = 1
    69.     End If
    70.     KeyAscii = Asc(UCase$(Chr$(KeyAscii))) 'capitilize
    71.     Select Case KeyAscii
    72.         Case vbKeyBack
    73.             txtBox.SelLength = nSelLen
    74.         Case vbKeyA To vbKeyZ
    75.             If txtBox.SelStart <> 0 Then
    76.                 KeyAscii = 0
    77.                 txtBox.SelLength = nSelLen
    78.             Else
    79.                 KeyAscii = Asc(UCase$(Chr$(KeyAscii))) 'capitilize
    80.     End If
    81.         Case vbKey0 To vbKey9
    82.             If txtBox.SelStart = 0 Then
    83.                 KeyAscii = 0
    84.                 txtBox.SelLength = nSelLen
    85.             End If
    86.         Case Else
    87.             KeyAscii = 0
    88.             txtBox.SelLength = nSelLen
    89.     End Select
    90. End Sub
    91.  
    92. Private Sub txtBox_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    93.     If Not mnuContext Is Nothing Then
    94.         If Button And vbRightButton Then
    95.             'surpress the default context menu and show our custom menu instead
    96.             txtBox.Enabled = False
    97.             txtBox.Enabled = True
    98.             txtBox.Parent.PopupMenu mnuContext
    99.         End If
    100.     End If
    101. End Sub

  9. #89
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Manila, Philippines
    Posts
    486

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    VB Code:
    1. Public MyChar
    2. Function validity()
    3. If Len(Text1.Text) = 1 Then
    4.     If InStr(MyChar, Left(Text1.Text, 1)) = 0 Then
    5.     'KeyAscii = 0
    6.         MsgBox "Invalid format"
    7.         Exit Function
    8.     End If
    9.     Exit Function
    10. End If
    11. On Error GoTo kulitag
    12.     If Not IsNumeric(Right(Text1.Text, Len(Text1.Text) - 1)) Then
    13.         MsgBox "Invalid format"
    14.             KeyAscii = 0
    15.  
    16.     End If
    17. kulitag:
    18. End Function
    19.  
    20. Private Sub Form_Load()
    21.  
    22. MyChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    23.  
    24.  
    25. End Sub
    26.  
    27. Private Sub Text1_Change()
    28. validity
    29. End Sub

    just wnt to try!

  10. #90

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    Ugghhh... I think I'll just use the code of Bruce Fox instead of using this method, w/c uses class....it just isn't working (maybe it's my fault...I'm just a newbie)....
    Thank you so much Baja for answering my questions.. =)
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

  11. #91
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Manila, Philippines
    Posts
    486

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    ei sera have you tried mine?

  12. #92
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    Quote Originally Posted by seraphicmortal
    Ugghhh... ....it just isn't working (maybe it's my fault...I'm just a newbie)....
    Persist with it it's worth it.



    Start a new Project, in Form1 place the code identified, and 'Add' a Class Module and paste that code in (ensure the Class Module name is cTextFormat, now add a TextBox.


    Thats it


    Edit: Rem out: "Call oValidateText.Init(Text1, vbYellow, mnuPopUp)" just to test it (as you wount have the Menu).




    Bruce.

  13. #93

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    ooki dokie...will do as told Bruce! As for Kulitag, I think there's a bit error. Thanks for trying anyways.
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

  14. #94

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    Originally posted by Bruce Fox
    Edit: Rem out: "Call oValidateText.Init(Text1, vbYellow, mnuPopUp)" just to test it (as you wount have the Menu).
    Just did what you and Baja said.

    The ones in red is the one having error....same thing happened when I pasted it in my program and when I made a new project.

    mnuPopUp is variable not defined.
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

  15. #95
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    Quote Originally Posted by seraphicmortal
    Just did what you and Baja said.

    The ones in red is the one having error....same thing happened when I pasted it in my program and when I made a new project.

    mnuPopUp is variable not defined.

    mnuPopUp would be a menu item that you want to have 'popup' when the user clicks(or right-click) an item on your form.

    Check the VB help files for 'PopupMenu'

  16. #96
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    Quote Originally Posted by seraphicmortal
    Just did what you and Baja said.

    The ones in red is the one having error....same thing happened when I pasted it in my program and when I made a new project.

    mnuPopUp is variable not defined.
    To be able to assign a menu you have to create one. Start the menu editor and create a menu call mnuPopUp and create some sub menu items. Look at the attached Form for an example. If you don't want to assign a custom popup menu just call it like this instead:
    VB Code:
    1. Call oValidateText.Init(Text1)
    Or you could use this code which does the same thing.
    VB Code:
    1. Set oValidateText.TextBox = Text1
    Since you for some reason couldn't download ZIP files I've attached a form and the class. Create a new project and remove the empty form that is created. Now add this form and class instead.
    Attached Files Attached Files

  17. #97
    Lively Member
    Join Date
    May 2005
    Posts
    108

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    cvmicheal has embarrassed me to no end... thx cv as this one liner has saved me carpel tunnel.

    can i retract my reply?

  18. #98
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    What reply?

  19. #99

    Thread Starter
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Resolved Re: How to restrict users to enter first character as alphabet and rest as numeric.

    Finally, I got what Joacim's idea.... Thank you so much. Yah, I agree with Bruce his codes really is so worth it.. Thank you...Thank you...Thank you!
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

  20. #100
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How to restrict users to enter first character as alphabet and rest as numeric.

    In that case you are (finally) welcome

Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width