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.
 
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
 
Joined
Mar 18, 2009
Messages
3
Reaction score
0
Individual Instances of Status Indicators to change background color

Jim Burke in Novi said:
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.

"Dan H." wrote:

> 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
Jim,
I see what you did for Dan and this is very similar to what I am trying to do.... The problem I am having is that this Option group with 4 ToggleButtons is being changed for not any particular record, but for all records as if it is not attached to the record but the form itself. How can I make it so that when I click one of these toggle buttons that it only changes that record. The value itself is saved in the record already.

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.

"Dan H." wrote:

> 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.
>
 

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