I'm trying to specify that multiple conditional statements need to be met before saving. If either of the statements are not met, then their respective error messages will pop up. The problem I'm having is I cannot get the error messages to show independantly. Am I on the right track or where do I go from here?

If the event number or person boxes are not blank and event assoc2 box is not equal to stolen then
         'call save
else if event number or person are blank
            ' error message
or if event assoc2 is equal to stolen and original value is blank then
                ' error message



 If Not (EVENT_NUMBER.Text = "" Or person.Text = "") And EVENT_ASSOC2.Text <> "Stolen" Then

                        'Call CREATE_EXCEL_FILE(outputdirectory) to save
                        Else

                        End If

                    Elseif  EVENT_ASSOC2.Text = "Stolen" And ORIGINAL_VALUE.Text = "" Then
                        MessageBox.Show("Property listed as stolen requires a value", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Else

                        MessageBox.Show("Person and Event Number fields need to be completed", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Stop)

                    End If

Dani AI

Generated

The root cause is the branching style: using an ElseIf/Else chain makes the routine pick the first true branch and skip the rest, so only one message can ever appear. As noted, that explains the observed behavior; and offered valid alternative structures (a single big If block or a Select Case True), but those still execute mutually exclusive branches unless each validation is evaluated independently.

A simple, maintainable pattern is a validation-first pass that gathers every failing condition, then acts based on the collected results. This makes it easy to show all relevant messages at once (or focus the first problem) and keeps the save-call isolated to the “no errors” path.

Example approach (VB.NET):

Dim errors As New List(Of String)

If String.Equals(EVENT_ASSOC2.Text, "Stolen", StringComparison.OrdinalIgnoreCase) AndAlso
   String.IsNullOrWhiteSpace(ORIGINAL_VALUE.Text) Then
    errors.Add("Item marked as stolen requires an original value.")
End If

If String.IsNullOrWhiteSpace(EVENT_NUMBER.Text) OrElse String.IsNullOrWhiteSpace(person.Text) Then
    errors.Add("Event number and person fields must be completed.")
End If

If errors.Count > 0 Then
    MessageBox.Show(String.Join(Environment.NewLine, errors), "Validation error(s)",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning)
    Exit Sub 'prevent save
End If

' Proceed to save here

Notes and troubleshooting: prefer AndAlso/OrElse for short-circuiting and String.IsNullOrWhiteSpace to catch whitespace-only input. Use case-insensitive comparison for fixed values (or normalize with ToLowerInvariant). If validation still seems to be skipped, verify the save handler runs (breakpoints/logging) and confirm control names match. This pattern keeps logic clear, allows multiple independent error messages, and separates validation from the actual save operation.

Recommended Answers

All 5 Replies

How are you verifying the data in the textboxes? Your code should work. If EVENT_ASSOC2.Text = "Stolen" And ORIGINAL_VALUE.Text = "" then you should get the first error message. The second error message will only show if EVENT_NUMBER.Text = "" Or person.Text = "" and the first error message doesn't show

I think you want this:

IF not(EVENT_NUMBER.Text ="" Or person.Text ="") And Event_ASSOC2.Text <> "Stolen" then
    'Call your create excel file
ElseIf Event_ASSOC.Text ="Stolen" And ORIGINAL_VALUE.Text =""  then
    messagebox.show("Property listed as stolen requires a value", "Warning", Messageboxbuttons.ok, MessageBoxIcon.Error)
ElseIF EVENT_NUMBER.Text ="" Or person.Text ="" then
    messagebox.show ("Person and Event Number Fields ned to be completed", "Error!", Messageboxbuttons.ok, MessageBoxIcon.Stop)
Else
    'if Event_Number and Person are filled in and Event_Assoc2.text ="Stolen" but Orignal_Value.Text <>""
    'You'd end up here...
End if

I don't see the benefit in going to a case block. You'd still end up with nested ifs:

SELECT CASE Event_Assoc.text
    Case "Stolen"
        if ORIGINAL_VALUE.Text ="" Then
            'Messagebox
        Else
            'do what?
        End if
    Case Else
        if EVENT_NUMBER.Text ="" OR person.Text ="" then
            'messagebox
        else
            'call your Excel file
        end if
End Select

So why not just use a big If block?

If Event_Assoc.Text ="Stolen" Then
    IF ORIGINAL_VALUE.Text ="" then
        'messagebox
    End if
ELSE
    if EVENT_NUMBER.Text ="" OR person.Text ="" then
            'messagebox
    else
           'call your Excel file
    end if    
End if

I find a convenient way to handle complex logic is to use this form of the Select

Select Case True
    Case <conditional>
        code
    Case <conditional>
        code
    Case <conditional>
        code
    Case Else
        code
End Select

Normally in a Select you would put the conditional in the Select clause. With Select Case True you can put any conditional in the Case clauses. They don't have to be related. Just put them in the order you would code them with Else If statements. Also, if you want to remove parentheses in compound conditionals then

Not (a Or B)

is equivalent to

Not a And Not B

or to use the above example

not(EVENT_NUMBER.Text ="" Or person.Text ="") And Event_ASSOC2.Text <> "Stolen"

is equivalent to

EVENT_NUMBER.Text <> "" And person.Text <> "" And Event_ASSOC2.Text <> "Stolen"
commented: interesting use of Select +0

Thanks Jim,
this is a true life saver!
"Select Case True", noboby tells you these things!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.