Select Case statement and data validation - vb.net

I am trying to validate data ranges using the select case statement. I am having issues with the other nested select cases. Is this possible or am i wishfull thinking? Or should i separate the select case statements to be stacked?
For instance this is my code in vb:
Select Case intyear
Case 2000 To 2025
Select Case intmonth
Case 1 To 12
BlnDateValid = True
End Select
Select Case intDay
Case 1 To 31
BlnDateValid = True
End Select
Select Case intHours
Case 0 To 23
BlnDateValid = True
End Select
Select Case intAddDays
Case 0 To 60
BlnDateValid = True
End Select
Select Case intAddHours
Case 0 To 23
BlnDateValid = True
End Select
Case Else
BlnDateValid = False
End Select
If blnDatevalid = false then
MessagebBox.Show("Please check all fields and enter valid
data", "Invalid data", MessageBoxButtons.OK)

Unfortunately, the indenting making sense doesn't help the code make sense. The whole point of Select Case is to neatly select one of multiple cases. A Select Case with one case is bad code and you should be using an If statement instead. You should especially be using a If statement in this case because you can replace all those Select Case statements with a single If statement.
If Not (intyear >= 2000 AndAlso intyear <= 2025 AndAlso
intmonth >= 1 AndAlso intmonth <= 12 AndAlso
intDay >= 1 AndAlso intDay <= 31 AndAlso
intHours >= 0 AndAlso intHours <= 23 AndAlso
intAddDays >= 0 AndAlso intAddDays <= 60 AndAlso
intAddHours >= 0 AndAlso intAddHours <= 23) Then
MessagebBox.Show("Please check all fields and enter valid data", "Invalid data", MessageBoxButtons.OK)
End If

All the Select or If checks will still leave you vulnerable to non-sensical values like February 30. Better to actually attempt to create a DateTime value.
Dim d As DateTime
Dim t As TimeSpan
Try
d = New DateTime(intYear, intMonth, intDay, intHours, 0, 0)
t = New TimeSpan(intAddDays, intAddHours, 0, 0)
If t > (New TimeSpan(60, 23, 0, 0)) Then Throw New ArgumentOutOfRangeException()
Catch
MessagebBox.Show("Please check all fields and enter valid data", "Invalid data", MessageBoxButtons.OK)
End Try
or you can create a string and try parsing it:
If Not DateTime.TryParse($"{intYear}-{intMonth}-{intDay} {intHours}:00:00")
MessagebBox.Show("Please check all fields and enter valid data", "Invalid data", MessageBoxButtons.OK)
End If

Related

VBA Select case 1 to 100 only taking 1

I'm trying to make a select case that identifies if a number is lower than 0, 1 to 100 or greater than 100, the thing is that is just doesn't work. Here's my code:
If IsNumeric(TxtTemp.Text) Then
Select Case TxtTemp.Text
Case Is <= 0
TxtEstado.Text = "Solid"
Case 1 To 100
TxtEstado.Text = "Liquid"
Case Is > 100
TxtEstado.Text = "Gas"
End Select
Else
TxtEstado.Text = ""
End If
I know that this is an easy thing to do, the thing is that the select case returns liquid only if the number received is equal to 1. If it is lower or equal to 0 it returns solid, but if it is equal or greater to 2, it returns gas. I don't understand what I'm doing wrong.
Maybe it is easier to use a function for this kind of conversion
Function chText(txt As String) As String
On Error GoTo EH
Dim resTxt As String
If IsNumeric(txt) Then
Select Case CDbl(txt)
Case Is <= 0
resTxt = "Solid"
Case 1 To 100
resTxt = "Liquid"
Case Is > 100
resTxt = "Gas"
End Select
Else
resTxt = ""
End If
chText = resTxt
Exit Function
EH:
chText = "Error"
End Function
Sub Tester()
Debug.Print chText("101")
' TxtEstado.Text = chText(TxtTemp.Text)
End Sub

How to compare only month and year? [VB]

Its quite simple, i just want to compare two dates using month and year, if the input date (mont and year only) are above or below that current date (month and year).
The problem is , when i compare two strings
Dim dDate as DateTime
If Not (DateTime.TryParse(txtBox.Text, dDate)) Then
MessageBox.Show("check date.")
Else
txtBox.Text = dDate.ToString("MM/yyyy")
end If
IF dDate.ToString("MM/yyyy") < DateTime.Now.ToString("MM/yyyy")
MessageBox.Show("Below") ' Problem: 03/2024 saying is below than 08/2019
Else
MessageBox.Show("Above")
End If
Any help?
UPDATE
I CHANGED THE CASE TO
If (dDate.Month AndAlso dDate.Year) < (DateTime.Now.Month AndAlso DateTime.Now.Year) Then
'input: 07/2019
'date expired
Else
'the problem is here
'saying 07/2019 is not < than 08/2019
End If
I'd avoid using strings.
Dim dDate As DateTime
If Not (DateTime.TryParse(txtBox.Text, dDate)) Then
'bad date
MessageBox.Show("check date.")
Else
Select Case dDate.Year
Case Is < DateTime.Now.Year
MessageBox.Show("Below")
Case Is > DateTime.Now.Year
MessageBox.Show("Above")
Case Else 'years are equal,check month
Select Case dDate.Month
Case Is < DateTime.Now.Month
MessageBox.Show("Below")
Case Is > DateTime.Now.Month
MessageBox.Show("Above")
Case Else 'equal months
MessageBox.Show("SAME") '????
End Select
End Select
End If
Using date values is probably best, but if string comparisons must be made.
Dim dDate as DateTime
If Not (DateTime.TryParse(txtBox.Text, dDate)) Then
MessageBox.Show("check date.")
Else
txtBox.Text = dDate.ToString("yyyyMM")
End If
If dDate.ToString("yyyyMM") < DateTime.Now.ToString("yyyyMM") Then
MessageBox.Show("Below")
ElseIf dDate.ToString("yyyyMM") > DateTime.Now.ToString("yyyyMM") Then
MessageBox.Show("Above")
Else
MessageBox.Show("Same")
End If
Thank you everyone, but the final solutions is made with
If (dDate.Year < DateTime.Now.Year Or (dDate.Year = DateTime.Now.Year And dDate.Month < DateTime.Now.Month)) Then
'something
Else
'something
End If
Convert the year and month to Integer values, which are easy to compare less-than or greater-than. For example, if you had two DateTimePicker controls on a form, named dtpDate1 and dptDate2, and a button btnTest:
Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click
Dim date1 As Integer = (dtpDate1.Value.Year * 100) + dtpDate1.Value.Month
Dim date2 As Integer = (dtpDate2.Value.Year * 100) + dtpDate2.Value.Month
MsgBox("Date1 = " & date1.ToString & vbCrLf & "Date2 = " & date2.ToString)
If date1 = date2 Then MsgBox("Equal!")
If date1 < date2 Then MsgBox("date1 is less than date2")
If date1 > date2 Then MsgBox("date1 is greater than date2")
End Sub
The dates are converted to integers in a YYYYMM format. This is a data warehouse technique to convert dates to integers for better query performance when date is used often in a WHERE clause.

How to compare two dates in text boxes?

I want to compare two dates in text boxes.
Public Function CourseStatus(ByVal RefDate2 As Variant) As String
Dim Description As String
If Len(RefDate2) > 0 And IsDate(RefDate2) Then
Select Case DateDiff("d", Date, RefDate2)
Case Is > 60
CourseStatus = "In Date"
Case Is > 0
CourseStatus = "Expiring"
Case Is = [ParticipationDate]
CourseStatus = "Not Refreshed"
Case Else
CourseStatus = "Expired"
End Select
Else
CourseStatus = "Please Book"
End If
End Function
If [ParticipationDate] & [RefDate2] match return "Not Refreshed" as CourseStatus.
I need to do this before running the rest of the code to give "in Date" "Expiring" "Expired" and if none of this applies display "Please Book".
e.g
ParticipationDate 1/1/19
RefDate2 1/1/19
CourseStatus "Not Refreshed"
Sorry I didn't read your code super accurately.
Your datediff function returns a integer value not a date. You should use
Case is 0
to check for a date match.
You might use something like this:
If Len(RefDate2) > 0 And IsDate(RefDate2) Then
Select Case DateDiff("d", Date, RefDate2)
Case Is > 60
CourseStatus = "In Date"
Case Is > 0
CourseStatus = "Expiring"
Case Else
If DateDiff("d", RefDate2, [ParticipationDate]) = 0 Then
CourseStatus = "Not Refreshed"
Else
CourseStatus = "Expired"
End If
End Select
Else
CourseStatus = "Please Book"
End If

VBA Select Case number to number greater than and less than

Sub ss()
Dim a As Double
a = 6.99999999
Select Case a
Case 0 To 7:
MsgBox "ok"
Case Else:
MsgBox "no"
End Select
End Sub
The Case 0 to 7 results in a check for a >= 0 and a <= 7. But what I want is a >= 0 and a < 7.
I also tried Case Is >=0, Is < 7.
How can I do this in a Select Case?
a = 6.99999999 should result in "ok"
a = 7 should result in "no"
select case true
case a >= 0 and a < 7
MsgBox "ok"
case else
MsgBox "no"
end select
But, unless you have more than two conditions, I would suggest you use an If instead.
Sub ss()
Dim a
a = 7
Select Case a
Case 7:
MsgBox "no"
Case 0 To 7:
MsgBox "ok"
Case Else:
MsgBox "no"
End Select
End Sub
Sub ss()
Dim a As Double
a = 6.99999999
Select Case a
Case 0 To 7:
If a = 7 Then
MsgBox "no"
Else
MsgBox "ok"
End If
Case Else:
MsgBox "no"
End Select
End Sub
I finally work out this solution. Thank you all!

Error 16: Expression Too Complex - Nested Select Case

I am getting a "Run-Time Error 16: Expression Too Complex" for the sencond code block. I read online that the maximum allowed number of nested expressions is 8, but if nested statements are defined by every "Select Case," then I haven't yet hit that. Is it possible it's because the Cases are dependent upon a variable? The first code block shows the possible values of egapend, while the second shows the code that is returning the error. Thanks in advance for your help.
First:
Select Case ge1e2
Case Is <= 0
Select Case ge2e3
Case Is <= 0
egap18a = 0
egap18b = 0
Case Is > 0
egap18a = 0
egap18b = ge2e3
egapend = Sheet1.[z1IE]
End Select
Case Is > 0
Select Case ge2e3
Case Is <= 0
egap18a = ge1e2
egap18b = 0
egapend = Sheet1.[z1CZ]
Case Is > 0
Select Case ge1e2
Case Is >= 180
egap18a = ge1e2
egap18b = ge2e3
egapend = Sheet1.[z1CZ]
Case Is < 180
egap18a = ge1e2
egap18b = ge2e3
egapend = Sheet1.[z1IE]
End Select
End Select
End Select
Second:
Dim e1length As Long
Dim e2length As Long
Dim cp121 As Boolean
e1length = DateDiff("d", Sheet1.[z1CZ], Sheet1.[z1DA])
e2length = DateDiff("d", Sheet1.[z1IE], Sheet1.[z1IF])
Select Case extendedgap
Case True
Select Case egapend
Case Sheet1.[z1IE] 'end of egap is start of E2
Select Case e2length 'was borrower employed by E2 for more than 6 months?
Case Is >= 180
cp121 = True
Case Is < 180
Select Case ge1e2 'if not, was there a gap between E1 and E2
Case Is > 1
cp121 = False
Case Else
Select Case DateDiff("d", Sheet1.[z1IE], Sheet1.[z1DA]) 'If not, was employment between E1/E2 6 mos?
Case Is >= 180
cp121 = True
Case Is < 180
cp121 = False
End Select
End Select
End Select
Case Sheet1.[z1CZ] 'end of egap is start of E1
Select Case DateDiff("d", Sheet1.[z1CZ], Sheet1.[z1DA]) 'was borrower employed by E1 for at least 6 mos?
Case Is >= 180
cp121 = True
Case Is < 180
cp121 = False
End Select
End Select
It's really just a big nested conditional structure. Don't use Select...Case for that. Use it e.g. when you're looking at some enum value:
Select Case MsgBox("Yes, no, or cancel?", vbYesNoCancel)
Case vbYes
'stuff
Case vbNo
'stuff
Case vbCancel
'stuff
End Select
Refactoring step 1: Turn all these 2-branch Select...Case blocks into If...Else...End If blocks. That should already take care of the "expression too complex" compile error.
Refactoring step 2: Implement Boolean assignments as such:
For example in one place two places you have:
Select Case DateDiff("d", Sheet1.[z1IE], Sheet1.[z1DA]) 'If not, was employment between E1/E2 6 mos?
Case Is >= 180
cp121 = True
Case Is < 180
cp121 = False
End Select
Replace that with:
cp121 = (DateDiff("d", Sheet1.[z1IE], Sheet1.[z1DA]) >= 180)
Refactoring step 3: Extract functions and procedures out of the If and Else branches where applicable; eliminate the duplicated branches, Don't Repeat Yourself.
Once you have something that works, take it to Code Review for further refactoring and simplification ideas.