If then else syntax problem?

dws

New member
Local time
Today, 02:16
Joined
Jan 2, 2007
Messages
3
I have a button to calculate trip information. I can't get the message box to pop up in the following code. Can anyone tell me why?
If [odometer end] and [odometer start] are null and I click the calculate button nothing happens, I want the message box to appear.

here is the code I am using:

If [odometer_start] = Null And [length_of_trip] = Null Then
MsgBox "Note: Two of the Three values are needed to calculate the mileage.", vbExclamation
Else
[odometer_end] = [odometer_start] + [length_of_trip]
End If

The else portion works as I would expect.
Thanks for your help
 
dws said:
I have a button to calculate trip information. I can't get the message box to pop up in the following code. Can anyone tell me why?
If [odometer end] and [odometer start] are null and I click the calculate button nothing happens, I want the message box to appear.

here is the code I am using:

If [odometer_start] = Null And [length_of_trip] = Null Then
MsgBox "Note: Two of the Three values are needed to calculate the mileage.", vbExclamation
Else
[odometer_end] = [odometer_start] + [length_of_trip]
End If

The else portion works as I would expect.
Thanks for your help

If odometer_start and length_of_trip are (I'm assuming)textboxes wouldn't they be blank ("") and not null if they are empty?

If you step through the code when they are blank what are the values?
 
If IsNull([odometer_start]) And IsNull([length_of_trip]) Then

Or as stated in the previous post, you can check
If (IsNull([odometer_start]) OR [odometer_start]="") And (IsNull([length_of_trip]) OR [length_of_trip]="")

Also, remember this will give the message ONLY if both are null. If you want the message if either one is null then you would need to use OR between them.
 
Thanks! I got it to work by changing the syntax to
If IsNull([odometer_start]) And IsNull([length_of_trip]) Then
as Bob suggested. Why is that? The odometer_start and length of trip returned null values when I stepped thru the code they are numeric fields.
Thanks again for your help!
 
a null value is NEVER equal to anything, even Null. therefor your check simply is invalid.

Using the Isnull() function is the proper way to do it.

In SQL you have a simular issue where you do not use "= Null" but "Is Null" to get around the Null.
 

Users who are viewing this thread

Back
Top Bottom