Question Logic for IF() statment

jaksel

Member
Joined
Sep 3, 2008
Messages
9
Programming Experience
Beginner
I have a program that involves two dates. Date1, Date2 as follows:

Dim Date1 as Date
Dim Date2 as Date

as it turns out, it is also possible that Date1 could equal "NA" (I read the date from MS Project).

So, the following two sets of statements should produce the same result:

IF ((Date1 <> "NA") And (Date1 <= Date2)) Then
'Do Something
End If

The above code does not work. But this code does:

If Date1<>"NA" Then
If Date1<=Date2 Then
'Do Something
End If
End If

Can someone explain the error in my logic? My understanding is that it will test the first condition of the "And" ... if that condition fails it will leave the IF uncompleted becuase there is no sense to make the second test.
 
How are you managing to get "NA" into a Date variable. You should be getting an error that it's not valid to convert from String "NA" to type Date.

Take a look into Date.TryParse.

VB.NET:
		Dim s As String = "NA"
		Dim Date1 As Date
		Dim Date2 As Date = Date.Today()

		If Date.TryParse(s, Date1) = True AndAlso Date1 <= Date2 Then
			MessageBox.Show("Conditions Met")
		Else
			MessageBox.Show("conditions Not Met")
		End If

I'm not sure how you're pulling the data out of Project but I'd see about replacing those NAs with a control date like 1/1/1900 that you won't see in your program but can check against.
 
"And" will make the second test, if you use the "AndAlso" then it will test the first condition if that is True then it will test the second condition. If the first condition fails then it leaves the IF.


IF (Date1 <> "NA") AndAlso (Date1 <= Date2) Then
'Do Something
End If
 
I will try that, thanks.

For the other poster (Matt). Technically, MSProject sets Date1 and Date2 as Objects and then only allows a valid date or "NA" (per local language settings) to the field.

So, yes, NA is not a date. Good catch
 
Back
Top