Results 1 to 11 of 11

Thread: [RESOLVED] Simple way to check lots of TextBoxes?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2015
    Posts
    103

    Resolved [RESOLVED] Simple way to check lots of TextBoxes?

    So, this is not urgent, since I have a code that will work, but I just curious. I want to check whether any of the textboxes in a form is empty or not. I can create an if clause with "OR", like this:

    if txt1.Text = "", OR txt2.Text = "", etc. then

    .........


    But i want to know if there is a better and simpler way to do this, since there are like 12 textboxes to check, and i dont want to write all of their name.

    What i tried so far :

    Code:
    Dim kontrol As Control
    
    If TypeOf kontrol is TextBox Then
         If kontrol.Text = "" Then
         MsgBox "Data is incomplete"
         End If
    End If
    But that code above does not work, and give me error in the second line.

    Any suggestion?
    Last edited by xboner; Jul 29th, 2017 at 03:00 AM.

  2. #2
    Fanatic Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    578

    Re: Simple way to check lots of TextBoxes?

    Code:
    Dim c As Control
    
    For Each c In Form1.Controls
        If TypeOf c Is TextBox and control.Text = "" Then
            MsgBox "Data is incomplete"
        End If
    Next

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Simple way to check lots of TextBoxes?

    I would suggest using control arrays where possible and then you can easily loop through them.
    Given the sample names of txt1 txt2 and such then an array would make sense.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2015
    Posts
    103

    Re: Simple way to check lots of TextBoxes?

    Quote Originally Posted by Steve Grant View Post
    Code:
    Dim c As Control
    
    For Each c In Form1.Controls
        If TypeOf c Is TextBox and control.Text = "" Then
            MsgBox "Data is incomplete"
        End If
    Next
    I think there is something wrong in the second line, it should be (cmiiw):
    If TypeOf c Is TextBox and c.Text = "" Then

    Anyway, i tried similar code with your code, and it gives me the messagebox 12 times cosecutively, which i dont want, and which is actually the reason why i ask my question in here in the first place.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2015
    Posts
    103

    Re: Simple way to check lots of TextBoxes?

    Quote Originally Posted by DataMiser View Post
    I would suggest using control arrays where possible and then you can easily loop through them.
    Given the sample names of txt1 txt2 and such then an array would make sense.
    control arrays?

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Simple way to check lots of TextBoxes?

    I would expect that the line below would throw an error when there are controls on the form that do not have a text property
    Code:
    If TypeOf c Is TextBox and c.Text = "" Then
    Using a nested If would get around that problem
    Code:
        If TypeOf c Is TextBox Then
               If c.Text = "" Then
                     MsgBox "Data is incomplete"
                     Exit For
               End If
         End If

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Simple way to check lots of TextBoxes?

    Quote Originally Posted by xboner View Post
    control arrays?
    Yes Control Arrays, You can create them by placing a control on a form then copy the control and paste it to the form. The IDE will ask if you want to create a control array and if you say yes it sets the index property so you get text1(0) text1(1) txet1(2) and so on

    To loop through them
    Code:
        For x=0 to 10
             If Text1(x).text="" then
                     MsgBox "Data is incomplete"
                     Exit For
              End If
         Next

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2015
    Posts
    103

    Re: Simple way to check lots of TextBoxes?

    Quote Originally Posted by DataMiser View Post
    I would expect that the line below would throw an error when there are controls on the form that do not have a text property
    Code:
    If TypeOf c Is TextBox and c.Text = "" Then
    Using a nested If would get around that problem
    Code:
        If TypeOf c Is TextBox Then
               If c.Text = "" Then
                     MsgBox "Data is incomplete"
                     Exit For
               End If
         End If
    Quote Originally Posted by DataMiser View Post
    Yes Control Arrays, You can create them by placing a control on a form then copy the control and paste it to the form. The IDE will ask if you want to create a control array and if you say yes it sets the index property so you get text1(0) text1(1) txet1(2) and so on

    To loop through them
    Code:
        For x=0 to 10
             If Text1(x).text="" then
                     MsgBox "Data is incomplete"
                     Exit For
              End If
         Next
    Thanks, i'll try them

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: Simple way to check lots of TextBoxes?

    Yes, arrays are usually the better way to do what you are attempting. DM's post is spot on.
    But, you might want to tell the user WHICH textbox is not completed (although it should be fairly obvious). Your code could be like this:

    Code:
    Private Sub Command1_Click()
    Dim x As Integer
    For x = 0 To 10
             If Text1(x).Text = "" Then
                     MsgBox "Data is incomplete in textbox # " & CStr(x + 1)
                     Exit For
              End If
         Next
    End Sub
    But because textboxes which are empty are obvious, you don't really need to tell the user, but, let's say you wanted to make sure all of the textboxes included only numbers, you'd use the same concept like this code:

    Code:
    Private Sub Command1_Click()
    Dim x As Integer
    For x = 0 To 10
             If Not IsNumeric(Text1(x).Text) Then
                     MsgBox "Please enter a NUMBER in textbox # " & CStr(x + 1)
                     Exit For
              End If
         Next
    End Sub
    Hope you see the value of arrays...and you can do them with other controls too, of course.

    Note the "x + 1" in those snippets...that is because arrays start with an index of 0. So, when you add a control to a form initially, and then copy/paste it to add more (answering 'yes' to the prompt to create an array), that original control will have an index of 0, the next one, 1, etc. Your array will run from 0 to 10 in those examples above, meaning you would have ELEVEN textboxes on your form---indices of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

    Hope this helps you understand arrays a bit more.

  10. #10
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: Simple way to check lots of TextBoxes?

    xboner

    A variation of DataMiser's method (post #7) to create a control array is to use the Load statement.

    In design mode
    1. Place a TextBox control on your form (in my case, I've named it Text3)
    2. Open the Properties window
    3. Set the Index property to 1

    Then, this code will "create" 9 more

    Code:
    Private Sub Command1_Click()
        '
        With Text3(1)
            .Top = 1000
            .Left = 500
        End With
        '
        oo = 100            ' vertical "offset"
        For ii = 2 To 10
            Load Text3(ii)
            With Text3(ii)
                .Top = Text3(ii - 1).Top + Text3(ii - 1).Height + oo
                .Visible = True
                .Text = "Text3(" & ii & ")"
            End With
        Next ii
        '
    End Sub
    Here is an image

    Name:  text10.png
Views: 99
Size:  4.7 KB

    The down-side is that you need to specify the position of each newly loaded TextBox
    The up-side is that if, for instance, you had 30 TextBoxes, you would not need to manually place them all on the form.

    Note that I specified the Text property. This was for display purposes only.
    You could also set it to Empty.

    Regarding setting Index to 1:
    ... Normally, a control array's 1st index is 0.
    ... But this can sometimes be confusing. So I just set it to 1.

    EDIT1:

    Sammi snuck his post in while I was composing mine ..
    His discussion regarding the Index 0 covers the issue more comprehensively that I did.

    HTH
    Spoo
    Last edited by Spooman; Jul 29th, 2017 at 07:28 AM.

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Simple way to check lots of TextBoxes?

    Just a note. If this is needed to verify/validate required information was provided by the user, simply checking whether a textbox is empty or not is generally not enough. Since a user can add/paste anything they want, it is typically required that validation checks the control for a valid entry (when applicable), not just any entry.

    One method of doing this is to use the Validate event of the control.

    Another option is to use a custom validation routine. In that routine, string that is concatenated with error messages as the controls are validated. At the end of the validation routine, if the error string is filled, it is displayed to the user. That error string identifies which controls failed validation and why.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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