0

I am new to vb and I have been working on getting my brain around this if condition problem I am having in a windows forms in VB. This part of the project is a form writing back to SQL. I am looking to valdate input in textboxAm before I write the data back to the database:

I have combobox1 that has 3 outcomes: monday / friday / saturday

'if monday is chosen then the value in textboxAm can only be 8-12
'if friday is chosen then the value in textboxam can only be 12-24
'if saturday is chosen then the value in textboxam can only be 20 or more
' for each one of the 3 comboboxoptions on error messagebox.show(" blah")

' once one of the 3 criteria is meet move on.
' do i use:

' if combobox1.text = "moNday" andalso textboxAm.text >= 8 andalso textboxAm.text <=12 then
' elseif messagebox.show("monday min is 8 and max is 12...please correct and continue')
' endif
Thank you very much for your help

2
Contributors
4
Replies
7
Views
4 Years
Discussion Span
Last Post by NYCRAVEN
0
Dim hr As Integer = CInt(textboxAm.Text)
Dim msg As String = ""

Select Case Lcase(ComboBox1.Text)
    Case "monday"
        If hr < 8 Or hr > 12 Then 
            msg = "monday min is 8 and max is 12"
        End If
    Case "friday"
        If hr < 12 Or hr > 24 Then 
            msg = "friday min is 12 and max is 24"
        End If
    Case "saturday"
        If hr < 20 Or hr > 24 Then 
            msg = "saturday min is 12 and max is 24"
        End If
End Select

If msg <> "" Then
    MsgBox(msg & "...please correct and continue")
End If

or

    Dim hr As Integer = CInt(textboxAm.Text)
    Dim msg As String = ""

    Select Case LCase(ComboBox1.Text)
        Case "monday" : msg = IIf(hr < 8 Or hr > 12, "monday min is 8 and max is 12", "")
        Case "friday" : msg = IIf(hr < 12 Or hr > 24, "friday min is 12 and max is 24", "")
        Case "saturday" : msg = IIf(hr < 20 Or hr > 24, "saturday min is 12 and max is 24", "")
    End Select

    If msg <> "" Then
        MsgBox(msg & "...please correct and continue")
    End If

Edited by Reverend Jim

0

got it... I understand what is happening in the first example.

in the 2nd i have not seen IIF before nor the use of the ':' I will look it up now and see what is happening here. thank you for the enlightenment!

0

The colon allows you to put multiple lines of code on one line. It's usually frowned upon but I find there are certain circumstances where is compresses the number of lines of code and improves readability. IIF is a oneline If Then Else used in assignments where the syntax is

var = IIF(boolean expression, True value, False value)

In my example, if the hours value is outside the acceptable range then assign an error string, else assign an empty string.

0

RJ
Thanks for today's class on 'select case', IIF... not to mention the LCase pop quiz.
everything is working as advertised.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.