Log in

View Full Version : [SOLVED:] Userform, if combobox & text fields - notify user when field blank



folkstorm
05-24-2017, 11:16 AM
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

SamT
05-24-2017, 11:57 AM
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

folkstorm
05-25-2017, 04:43 AM
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.

SamT
05-25-2017, 06:03 AM
I just want to force user to add all mandatory fields before start another sub.

What exactly do you mean.

folkstorm
05-25-2017, 07:21 AM
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.

SamT
05-25-2017, 01:29 PM
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

SamT
05-25-2017, 01:33 PM
Alternate version


Sub OK_Click()
If Len(txtName) * Len(txtSurname) * Len(txtTown) = 0 Then
Msg
Else
Email
End If
End Sub

folkstorm
05-25-2017, 02:01 PM
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.

SamT
05-25-2017, 02:21 PM
Combine my post#7 into my post #2.

folkstorm
05-26-2017, 09:05 AM
Thx SamT everything fine after combining Case & If Len.