Compile Error: Else without IF

D

Dan H.

I'm trying to change the backcolor of my control by keying in on select words
such as "Vacant", "Military", "Contractor", and "KTR". When one of these
words are type into the control the back color will change. Anything else
the field stays in its default color. I know that conditional formating is
available but I have more than the 3 allowed so this is the work around.

Private Sub DispatchName_AfterUpdate()
If [DispatchName] = "Vacant" Then [DispatchName].BackColor = vbGreen
ElseIf [DispatchName] = "KTR" Then [DispatchName].BackColor = vbYellow
ElseIf [DispatchName] = "Military" Then [DispatchName].BackColor = vbBlue
Else: [DispatchName].BackColor = vbWhite
End If
End Sub

Please help.

If there is an easier way other than condidtional format plese let me know.
I'm not a VB program by any means.

Thanks
 
J

Jim Burke in Novi

It would be easiest for you, in my opinion, to use a SELECT statement, though
if you have the three colors plus white you can use conditional formatting.
Just make White the color you specify in the control properties, then use
cond formatting for the other three. If you have more, not sure what the
options are besides using this SELECT statement.

SELECT CASE DispatchName
Case "Vacant"
[DispatchName].BackColor = vbGreen
Case "KTR"
[DispatchName].BackColor = vbYellow
Case "Military"
[DispatchName].BackColor = vbBlue
Case Else
[DispatchName].BackColor = vbWhite
End SELECT

If White is the color specified in the control porperties then the Else
isn't even needed.
 
C

Chris O'C via AccessMonster.com

You don't want to use if-elseif-else to build your decision block. Use
select case, like this:

Select Case DispatchName
Case "Vacant"
DispatchName.BackColor = vbGreen
Case "KTR"
DispatchName.BackColor = vbYellow
Case "Military"
DispatchName.BackColor = vbBlue
Case Else
DispatchName.BackColor = vbWhite
End Select

You really can use 4 colors for conditional formatting. White would be the
default color. But if you need more than 4 colors, you need the select case
block.

Chris
Microsoft MVP

I'm trying to change the backcolor of my control by keying in on select words
such as "Vacant", "Military", "Contractor", and "KTR". When one of these
words are type into the control the back color will change. Anything else
the field stays in its default color. I know that conditional formating is
available but I have more than the 3 allowed so this is the work around.

Private Sub DispatchName_AfterUpdate()
If [DispatchName] = "Vacant" Then [DispatchName].BackColor = vbGreen
ElseIf [DispatchName] = "KTR" Then [DispatchName].BackColor = vbYellow
ElseIf [DispatchName] = "Military" Then [DispatchName].BackColor = vbBlue
Else: [DispatchName].BackColor = vbWhite
End If
End Sub

Please help.

If there is an easier way other than condidtional format plese let me know.
I'm not a VB program by any means.
 
D

Douglas J. Steele

Jim's right that a Select statement would be better. I just thought I'd
explain why what you have is raising an error.

If you're trying to use ElseIf, you cannot put your conditions on the same
line as the keywords. You must use the syntax

If condition Then
statements
ElseIf condition-n Then
elseifstatements
Else
elsestatements
End If
 
D

Dan H.

Thank you everyone that worked great. I have one other question if I may...
I have a boarder and I like to have the color change to Red and the thickness
change to 2pt by clicking a button then if clicked again have it switch back
to the previouse setting.

Thanks again.

Douglas J. Steele said:
Jim's right that a Select statement would be better. I just thought I'd
explain why what you have is raising an error.

If you're trying to use ElseIf, you cannot put your conditions on the same
line as the keywords. You must use the syntax

If condition Then
statements
ElseIf condition-n Then
elseifstatements
Else
elsestatements
End If

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Dan H. said:
I'm trying to change the backcolor of my control by keying in on select
words
such as "Vacant", "Military", "Contractor", and "KTR". When one of these
words are type into the control the back color will change. Anything else
the field stays in its default color. I know that conditional formating
is
available but I have more than the 3 allowed so this is the work around.

Private Sub DispatchName_AfterUpdate()
If [DispatchName] = "Vacant" Then [DispatchName].BackColor = vbGreen
ElseIf [DispatchName] = "KTR" Then [DispatchName].BackColor = vbYellow
ElseIf [DispatchName] = "Military" Then [DispatchName].BackColor =
vbBlue
Else: [DispatchName].BackColor = vbWhite
End If
End Sub

Please help.

If there is an easier way other than condidtional format plese let me
know.
I'm not a VB program by any means.

Thanks
 
D

Douglas J. Steele

You can work with the BorderColor and BorderWidth properties:

With Me.NameOfTextbox
If .BorderWidth = 2 Then
.BorderWidth = 0
.BorderColor = 0
Else
.BorderWIdth = 2
.BorderColor = vbRed
End If
End With

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Dan H. said:
Thank you everyone that worked great. I have one other question if I
may...
I have a boarder and I like to have the color change to Red and the
thickness
change to 2pt by clicking a button then if clicked again have it switch
back
to the previouse setting.

Thanks again.

Douglas J. Steele said:
Jim's right that a Select statement would be better. I just thought I'd
explain why what you have is raising an error.

If you're trying to use ElseIf, you cannot put your conditions on the
same
line as the keywords. You must use the syntax

If condition Then
statements
ElseIf condition-n Then
elseifstatements
Else
elsestatements
End If

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Dan H. said:
I'm trying to change the backcolor of my control by keying in on select
words
such as "Vacant", "Military", "Contractor", and "KTR". When one of
these
words are type into the control the back color will change. Anything
else
the field stays in its default color. I know that conditional
formating
is
available but I have more than the 3 allowed so this is the work
around.

Private Sub DispatchName_AfterUpdate()
If [DispatchName] = "Vacant" Then [DispatchName].BackColor = vbGreen
ElseIf [DispatchName] = "KTR" Then [DispatchName].BackColor =
vbYellow
ElseIf [DispatchName] = "Military" Then [DispatchName].BackColor =
vbBlue
Else: [DispatchName].BackColor = vbWhite
End If
End Sub

Please help.

If there is an easier way other than condidtional format plese let me
know.
I'm not a VB program by any means.

Thanks
 

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