Validation Message Box

Arg81

Well-known member
Joined
Mar 11, 2005
Messages
949
Location
Midlands, UK
Programming Experience
1-3
I've put together some basic validation for when a user adds a record to my database;

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] ValidData() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sMessage [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] txtCustomer.Text = "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]sMessage = "'Customer Name' required."
lblSelect.Focus()
[/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] txtFAO.Text = "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]sMessage = "'For the Attention Of' required."
lblSelect.Focus()
[/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] dtpRequestDate.Text = "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]sMessage = "'Date of Request' required."
dtpRequestDate.Focus()
[/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] cboShipBy.Text = "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]sMessage = "'Ship By' required."
cboShipBy.Focus()
[/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] cboAccountManager.Text = "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]sMessage = "'Account Manager' required."
cboAccountManager.Focus()
[/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] cboRequestedBy.Text = "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]sMessage = "'Requested By' required."
cboRequestedBy.Focus()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] sMessage = "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]ValidData = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]ValidData = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2]MessageBox.Show(sMessage, "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]


So basically if the user has forgot to input in one of the fields, a messagebox shows telling them.
However, if there are two fields without data, only one messagebox is shown.

Is it possible to have each smessage on a seperate line of the same messagebox, i;e;

The following needs to be entered:

Customer Name
For Attention Of
Account Manager

{OK}

I've seen how to do this somewhere, I've checked all my books but can't find it, and now I'm going mad!!!!:rolleyes:
Thanks for any help on this.

Regards,
Luke
 
write something like this:
VB.NET:
[/COLOR]
[COLOR=#0000ff]Dim[/COLOR][SIZE=2] sMessage [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String = [COLOR=blue]String[/COLOR][COLOR=black].Empty[/COLOR]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] txtCustomer.Text = "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]sMessage &= "'Customer Name' required." & [COLOR=black][COLOR=black]System[/COLOR].Enviroment.NewLine[/COLOR]
lblSelect.Focus()
[/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] txtFAO.Text = "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]sMessage &= "'For the Attention Of' required." & [COLOR=black][COLOR=black]System[/COLOR].Enviroment.NewLine[/COLOR]
lblSelect.Focus()[/SIZE]
[COLOR=blue]End If[/COLOR]
[COLOR=#0000ff][/COLOR] 
[COLOR=#0000ff]If[/COLOR][SIZE=2] sMessage = [COLOR=#0000ff]String[/COLOR][COLOR=black].Empty [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]ValidData = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]ValidData = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2]MessageBox.Show(sMessage, "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If

[/COLOR][/SIZE][SIZE=2][COLOR=black]

[/SIZE]
 
it has to work. try putting "" insted of string.empty or other way around, or check lenght of the input text (if it is 0 then add text to message string)

sorry, try to break if...elseif...else..endif into separate if...endif.
 
Last edited by a moderator:
basically:
VB.NET:
Dim sMessage As String = ""
If txtCustomer.Text = "" Then sMessage &= "'Customer Name' required." & ControlChars.NewLine
If txtFAO.Text = "" Then sMessage &= "'For the Attention Of' required."
 
If sMessage = "" Then
  ValidData = True
Else
  ValidData = False
  MessageBox.Show(sMessage, "Input Required", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
 
Back
Top