+ Reply to Thread
Results 1 to 8 of 8

Textbox loop check to validate entry

Hybrid View

D_N_L Textbox loop check to... 07-29-2014, 09:08 AM
nathansav Re: Textbox loop check to... 07-29-2014, 09:20 AM
D_N_L Re: Textbox loop check to... 07-29-2014, 09:24 AM
nathansav Re: Textbox loop check to... 07-29-2014, 09:29 AM
D_N_L Re: Textbox loop check to... 07-29-2014, 10:31 AM
nathansav Re: Textbox loop check to... 07-29-2014, 10:42 AM
D_N_L Re: Textbox loop check to... 07-29-2014, 10:46 AM
mikerickson Re: Textbox loop check to... 07-29-2014, 10:49 AM
  1. #1
    Forum Contributor
    Join Date
    08-23-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    404

    Textbox loop check to validate entry

    Sorry - rubbish title.

    I'm looking for some advice. I have 50 Textboxes on a userform and I wish to prevent a user from entering info in each one that begins with an 'I'. My code works fine but naturally I don't wish to have it repeated 50 times. How would I incorporate it into a loop and what would I call the Sub?

    Private Sub Textbox1_Change()
    
        If Left(Textbox1.Text, 1) = "I" Or Left(Textbox1.Text, 1) = "i" Then
        
            MsgBox "Codes cannot begin with 'I'", vbOKOnly, "Sorry"
            Textbox1.Text = ""
        
        End If
    
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Textbox loop check to validate entry

    You can do whats called event sinking, how are you with VBA?
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Contributor
    Join Date
    08-23-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    404

    Re: Textbox loop check to validate entry

    Hi Nathan,

    Out of 10? Probably a 5.

    Six on a good day!

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Textbox loop check to validate entry

    Ok, i'll tell you what to do and send a sample.

    You need the following in your userform

    Private colCustomTBs As Collection
    
    Private Sub UserForm_Initialize()
    
    Dim ctl As Control
    Dim clsCustomTB As clsCustomTextBox
    
    Set colCustomTBs = New Collection
    
    For Each ctl In Me.Controls
        If TypeName(ctl) = "TextBox" Then
            Set clsCustomTB = New clsCustomTextBox
            Set clsCustomTB.CUSTOMTB = ctl
            colCustomTBs.Add clsCustomTB
        End If
    Next ctl
    
    End Sub
    Then you need a class, called clsCustomTextBox, which has the code

    Private WithEvents customTextBox As MSForms.TextBox
    
    Public Property Set CUSTOMTB(objIn As MSForms.TextBox)
        Set customTextBox = objIn
    End Property
    
    Private Sub customTextBox_Change()
        If Mid(customTextBox.Name, 1, 1) = "I" Then
        
            '   Your code
        
        End If
    End Sub
    What this does is pass every textbox on the form initialisation and diverts it to be our customtextbox, which is inherited from MsForms.Textbox, we just mess about with the events.

    HTH

  5. #5
    Forum Contributor
    Join Date
    08-23-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    404

    Re: Textbox loop check to validate entry

    I see, thank you very much.

    I have a few questions if I may?
    • The class part. I have inserted a class module – is this what you mean?
    • Will your code apply to all textboxes? I have other textboxes where it’ll be ok for the user to start their TB entry with ‘I’. Although the ones I want it to apply to all share a similar naming pattern – could I use that? So instead of:


    If TypeName(ctl) = "Textbox" Then
    Could I use:

    If TypeName(ctl) = "MyTextBoxName" Then
    • Lastly I receive the following error upon compile (user type not defined):

    Dim clsCustomTB As clsCustomTextBox

  6. #6
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Textbox loop check to validate entry

    1 Yes it does, you will need to put after the if Typename line,
    if ctl.name<>"control1" AND ctl.name<>"control2" then
    this will then omit the sinking for control1 and control2. Typename is the type name not the name.

    2 Have you named your class clsCustomTextbox?

  7. #7
    Forum Contributor
    Join Date
    08-23-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    404

    Re: Textbox loop check to validate entry

    That's great. Thanks a lot Nathan!! : )

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Textbox loop check to validate entry

    You could put this event code in the class module
    Private Sub customTextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        Dim newString As String
    
        With customTextBox
            newString = Left(.Text, .SelStart) & Chr(KeyAscii) & Mid(.Text, .SelStart + .SelLength + 1)
        End With
    
        If newString Like "I*" Or newString Like "i*" Then
            MsgBox "Can't start with i"
            KeyAscii = 0
        End If
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA to validate entry?
    By D_N_L in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-25-2014, 11:46 AM
  2. [SOLVED] VBA Validate textbox value to have alphanumeric characters
    By hydz1213 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-12-2013, 11:28 AM
  3. Check if textbox entry is in hh:MM format
    By johncassell in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-15-2009, 06:01 AM
  4. Validate a textbox
    By Digitborn.com in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-14-2007, 03:46 PM
  5. Validate textbox entry
    By Stuart in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-13-2005, 03:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1