Results 1 to 13 of 13

Thread: textboxes not empty

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2002
    Location
    Holland
    Posts
    24

    textboxes not empty

    I have 18 textboxes on my form. The user is not allowed to exit form without filling all the textboxes. At the moment I am using:
    Code:
               If txtKlant_nummer.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtKlant_nummer.SetFocus
                ElseIf txtKlantnaam.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtKlantnaam.SetFocus
                ElseIf txtPostadres.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtPostadres.SetFocus
                ElseIf txtWoonplaats.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtWoonplaats.SetFocus
                ElseIf txtPostcode.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtPostcode.SetFocus
                ElseIf txtLand.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtLand.SetFocus
                ElseIf txtBTW_nummer.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtBTW_nummer.SetFocus
                ElseIf txtTelefoon.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtTelefoon.SetFocus
                ElseIf txtTelefoon2.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtTelefoon2.SetFocus
                ElseIf txtFaxnummer.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtFaxnummer.SetFocus
                ElseIf txtFaxnummer2.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtFaxnummer2.SetFocus
                ElseIf txtMobieletelefoon.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtMobieletelefoon.SetFocus
                ElseIf txtEmail_adres.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtEmail_adres.SetFocus
                ElseIf txtNotitie_factuur.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtNotitie_factuur.SetFocus
                ElseIf txtNotitie_factuur_2.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtNotitie_factuur_2.SetFocus
                ElseIf txtOpslagpercentage.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtOpslagpercentage.SetFocus
                ElseIf txtCommissie.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtCommissie.SetFocus
                ElseIf txtDagen.Text = Empty Then
                    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
                    txtDagen.SetFocus
                Else
     BlaBla
    
    end if
    At the moment it isn't working because after the msgbox the sub doesn't go to the blabla but exits the sub. And isn't there a cleaner way??

    Thanks,
    Brian
    Homer no function beer well without.
    --Homer Simpson

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Use the validate event and set CausesValidation to True on each text box and the command button for exit. That should ensure that the textboxes have values.
    Last edited by leinad31; May 5th, 2003 at 02:52 PM.

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    You might want to add Len(Textbox.Text) = 0 and Isnull() to the condition in case its not empty or is Null. I don't think strings can be IsEmpty() but I might be wrong.
    Last edited by leinad31; May 5th, 2003 at 03:00 PM.

  4. #4
    Member
    Join Date
    Feb 1999
    Location
    Michigan
    Posts
    56
    just add "exit sub" right after each .setfocus


    example

    If txtKlant_nummer.Text = Empty Then
    Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")

    txtKlant_nummer.SetFocus

    exit sub

    ElseIf txtKlantnaam.Text = Empty Then
    .....
    .....



    this way, the users can enter the info after the message is prompt when the text box is empty. When the box is not empty, it will carry on and check the next box.



  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Originally posted by ting2000
    just add "exit sub" right after each .setfocus
    If there's no code after the gigantic IF structure then there's no need for Exit Sub. If there's code after the IF struct that he needs to skip over then he'll need the Exit Sub. Anyway, he can do away with the IF ELSEIF struct if he makes use the Validate event instead. He can even call it locally in the form if he wants to.
    VB Code:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2.    Call Textbox1_Validate(True)
    3. End Sub

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    I don't understand why your code doesn't do what you want. It seems that it should work fine. It checks each textbox if it is empty and if there is one that is empty it will tell the user and set focus to that textbox. ELSE (none of the textboxes are empty) it goes to blabla. What is the problem?


    Has someone helped you? Then you can Rate their helpful post.

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Maybe its initialized to a zero length string and not Empty

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2002
    Location
    Holland
    Posts
    24
    Dear manavo11

    I don't know why it isn't working, to be honest i copied and paste it from a early project of mine and it works in there but for some reason it doesn't in this project. maybe leinad31 is right I have to check that.
    I tried with the F8 and running the program but as soon as gives the first msgbox it leaves the sub.
    But I was looking for a shorter way.
    But thanks everybody,
    Brian
    Homer no function beer well without.
    --Homer Simpson

  9. #9
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    If there aren't any other textboxes on the form you can do something like this (I haven't tested it) :

    VB Code:
    1. Private Sub Command1_Click()
    2.  Dim ctl As Control
    3.  
    4.  For Each ctl In Form1
    5.   If TypeOf ctl Is TextBox Then
    6.    If ctl.Text = "" Then
    7.     ctl.SetFocus
    8.     MsgBox "EMPTY!"
    9.     Exit Sub
    10.    End If
    11.   End If
    12.  Next
    13.  MsgBox "ALL ARE FULL!"
    14. End Sub
    15.  
    16. Private Sub Form_Load()
    17.  Text1.Text = ""
    18.  Text2.Text = ""
    19.  Text3.Text = ""
    20. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  10. #10
    Junior Member
    Join Date
    Feb 2003
    Posts
    17
    This SHOULD work.

    VB Code:
    1. Private Sub Form_Unload(Cancel As Integer)
    2. If txtKlant_nummer.Text = "" Then
    3.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    4.                 txtKlant_nummer.SetFocus
    5.             ElseIf txtKlantnaam.Text = "" Then
    6.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    7.                 txtKlantnaam.SetFocus
    8.             ElseIf txtPostadres.Text = "" Then
    9.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    10.                 txtPostadres.SetFocus
    11.             ElseIf txtWoonplaats.Text = "" Then
    12.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    13.                 txtWoonplaats.SetFocus
    14.             ElseIf txtPostcode.Text = "" Then
    15.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    16.                 txtPostcode.SetFocus
    17.             ElseIf txtLand.Text = "" Then
    18.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    19.                 txtLand.SetFocus
    20.             ElseIf txtBTW_nummer.Text = "" Then
    21.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    22.                 txtBTW_nummer.SetFocus
    23.             ElseIf txtTelefoon.Text = "" Then
    24.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    25.                 txtTelefoon.SetFocus
    26.             ElseIf txtTelefoon2.Text = "" Then
    27.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    28.                 txtTelefoon2.SetFocus
    29.             ElseIf txtFaxnummer.Text ="" Then
    30.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    31.                 txtFaxnummer.SetFocus
    32.             ElseIf txtFaxnummer2.Text =""Then
    33.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    34.                 txtFaxnummer2.SetFocus
    35.             ElseIf txtMobieletelefoon.Text = "" Then
    36.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    37.                 txtMobieletelefoon.SetFocus
    38.             ElseIf txtEmail_adres.Text = "" Then
    39.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    40.                 txtEmail_adres.SetFocus
    41.             ElseIf txtNotitie_factuur.Text = "" Then
    42.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    43.                 txtNotitie_factuur.SetFocus
    44.             ElseIf txtNotitie_factuur_2.Text = "" Then
    45.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    46.                 txtNotitie_factuur_2.SetFocus
    47.             ElseIf txtOpslagpercentage.Text = "" Then
    48.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    49.                 txtOpslagpercentage.SetFocus
    50.             ElseIf txtCommissie.Text = "" Then
    51.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    52.                 txtCommissie.SetFocus
    53.             ElseIf txtDagen.Text = "" Then
    54.                 Call MsgBox("Alle velden invullen.", vbExclamation + vbDefaultButton1, "Let Op!")
    55.                 txtDagen.SetFocus
    56.             Else
    57.  BlaBla
    58.  
    59. end if
    60. End Sub


    add an exit sub after each message box call.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    May 2002
    Location
    Holland
    Posts
    24

    stupid me

    I think I have found my problem
    I have placed this code in the form_unload when I code "exit sub" it stil does form unload.
    I am going to make a button which ends the form.
    And I like the simplicity of the answer of manavo11 with the loop thrue the textboxes.
    Thanks everybody,
    Brian
    Homer no function beer well without.
    --Homer Simpson

  12. #12
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    If you want to avoid the form from unloading in the form_unload event all you have to do is set the cancel value to something (anything) other than zero. Like this (you can hit the X button all you want or a button with unload me in it and it won't close the form. It will if you put "End" in it.) :

    VB Code:
    1. Private Sub Form_Unload(Cancel As Integer)
    2.  Cancel = 3
    3. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  13. #13
    Hyperactive Member
    Join Date
    May 2003
    Posts
    401

    Lightbulb

    That code will definitely not go to blabla coz after the messagebox u are using a setfocus statement and so the control is being transferred to another part of the form..check that out...

    You can as well simplify the code as follows:

    For each c in form.control
    if typeof c.controls is textbox then
    if c.text="" then
    'give your messagebox here. a common one would benefit.
    end if
    end if
    next c

    may be this would help u out..check it...

    Enjoy!!!
    Aparna

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