Consulting

Results 1 to 10 of 10

Thread: Userform, if combobox & text fields - notify user when field blank

  1. #1

    Userform, if combobox & text fields - notify user when field blank

    I have user form with 1 combobox field and several text fields. Combobox 1 = type of application (7 different results) Text field like Name, Surname, Town, Post Code etc.


    After pressing Ok button, I would like prompt user if mandatory field is missing.


    Mandatory field for 1 selection is field 1, field2 and field3 for selection 2 only filed 1 and 2 for 3 only field 6 etc.


    Weird thing is that if I select something I didn't define in this sub, I selecting value 4 or 5 I still get message. What did I miss?

    Private Sub CommandButton1_Click()
    
    
    If UserForm2.ComboBox1T.Value = "1.New Application" _
    And TextBox1.Text = "" _
    Or TextBox2.Text = "" _
    Or TextBox3.Text = "" _
    Then
    MsgBox ("Fill in all mandatory Fields")
    
    
    
    
    Exit Sub
    End If
    
    
    
    
    If UserForm2.ComboBox1T.Value = "2.Old Application" _
    And TextBox1.Text = "" _
    Or TextBox2.Text = "" _
    Then
    MsgBox ("Fill in all mandatory Fields")
    
    
    Exit Sub
    End If
    
    
    If UserForm2.ComboBox1T.Value = "3.Somethingelse" _
    And TextBox1.Text = "" _
    Or TextBox2.Text = "" _
    Then
    MsgBox ("Fill in all mandatory Fields")
    
    
    
    
    Exit Sub
    End If

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Regarding TB's 2 and 3: The first (3 condition) IF-OR-THEN requires both the have a value,. If they pass this test, they will pass the other two IFTHEN's. If either fails, then show msg and Exit Sub

    If you get past that then...

    Regarding CB1: If no or wrong selection is made OR if TB1 has a value, then do NOT show msg


    Try using a Select Case to differentiate the seven eight possible CB1T choices.
    With Me
    Select Case .ComboBox1T
    Case "1.New Application"
    If .TB1 = "" Or .TB2 = "" Or .TB3 = ""  Then MsgBox 
    
    Case "2.old Application"
    If .TB1 = "" Or .TB2 = "" THen MsgBox
    
    Case "3.Other Application"
    If Bleh or Blah then Bluh
    
    Case "4."
    Case "5."
    Case "6."
    Case "7."
    Case Else 'No Selection made in ComboBox1T
    
    End Select
    End With
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Thank you Sam, case works great.

    Only problem I have now is that it works only one.
    If I press ok button again it goes straight to next sub.

    I thought I should add Exit Sub every time before End If but it didn't work.

    I just want to force user to add all mandatory fields before start another sub.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I just want to force user to add all mandatory fields before start another sub.
    What exactly do you mean.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    Text Fields in User form:

    *Name -
    *Surname -
    *Town -
    Country -
    OK BUTTON
    *- Mandatory fields.

    User adds information to field name. select OK, gets message "Fill in all mandatory Fields"
    User adds information to field surname , select ok, gets message "Fill in all mandatory Fields"
    User adds information to field town, select ok. A new email appears.

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Note that a Boolean False = 0 (Zero)

    Dim boName As Boolewan
    Dim boSurname as boolean
    dim botwon as booean
    Private Sub txtName_Exit()
    boName = True
    End Sub
    Private Subs Surname & Town _Exit(())
    boSurname & boTown = Trues
    End Subs
    Private Sub cbtOK_Click()
    
    If Not boName * boSurname * boTown Then 
    Msgbox"Idiot, fill em all in"
    Else do something
    End If
    
     ResetBooleans
    End Sub
    Sub ResetBooleans
    boName = False
    etc, et al
    End sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Alternate version
    Sub OK_Click()
    If Len(txtName) * Len(txtSurname) * Len(txtTown) = 0 Then
    Msg
    Else
    Email
    End If
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    I get it, but how can I apply it to my code?

    My combo box defines mandatory fields so in first case mandatory fields are TB1 & TB2 & TB3 but in second case TB2 & TB4 , in 3rd case TB6 & TB7 etc.

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Combine my post#7 into my post #2.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  10. #10
    Thx SamT everything fine after combining Case & If Len.

Posting Permissions

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