If Statement

T

Trini Gal

Hello,

I have the following code and it works fine:

Private Sub L_READING_AfterUpdate()

Dim DISTANCE As Integer
DISTANCE = 10

If L_DISTANCE = DISTANCE Then
READING = L_READING
ElseIf L_DISTANCE < DISTANCE Then
READING = ((L_DISTANCE / DISTANCE) * L_READING)
ElseIf L_DISTANCE > DISTANCE Then
READING = ((L_DISTANCE / DISTANCE) * L_READING)
Else
MsgBox "DISTANCE IS BLANK, PLEASE REVISE."
End If

End Sub

I want to tweek it a little to say:

If reading > 1300 then "Cannot be greater than 1300, please revise."

I tried adding another ElseIf, but its not working. Any help is greatly
appreciated.

Thanks in advance for your time.
 
K

KARL DEWEY

You need it as first test.
It never will work as last test due testing < Distance and > Distance
already as it will always be one of these.
 
M

Mike Painter

Trini said:
Hello,

I have the following code and it works fine:

Private Sub L_READING_AfterUpdate()

Dim DISTANCE As Integer
DISTANCE = 10

If L_DISTANCE = DISTANCE Then
READING = L_READING
ElseIf L_DISTANCE < DISTANCE Then
READING = ((L_DISTANCE / DISTANCE) * L_READING)
ElseIf L_DISTANCE > DISTANCE Then
READING = ((L_DISTANCE / DISTANCE) * L_READING)
Else
MsgBox "DISTANCE IS BLANK, PLEASE REVISE."
End If

End Sub

I want to tweek it a little to say:

If reading > 1300 then "Cannot be greater than 1300, please revise."

I tried adding another ElseIf, but its not working. Any help is
greatly appreciated.

Thanks in advance for your time.

A case statement usually makes things easier to read.

Select Case L_DISTANCE

Case DISTANCE ' = 10
READING = L_READING
Case "" 'it's blank
MsgBox "DISTANCE IS BLANK, PLEASE REVISE."
Case > 1300
MsgBox "Cannot be greater than 1300, please revise."
Case Else
READING = ((L_DISTANCE / DISTANCE) * L_READING)
End Select
 
T

Trini Gal

Karl and Mike,

Thank you so much for your quick responses, but after I posted I figured it
out. My code now looks like this:

Private Sub L_READING_AfterUpdate()

Dim DISTANCE As Integer
Dim CANCEL As Integer
DISTANCE = 10

If L_DISTANCE = DISTANCE Then
READING = L_READING
ElseIf L_DISTANCE < DISTANCE Then
READING = ((L_DISTANCE / DISTANCE) * L_READING)
ElseIf L_DISTANCE > DISTANCE Then
READING = ((L_DISTANCE / DISTANCE) * L_READING)
End If
If READING > 1300 Then
CANCEL = True
MsgBox "THE READING (uV/m)CANNOT BE GREATER THAN 1,300 PLEASE REVISE."
L_READING.SetFocus
End If

End Sub

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top