Else without If

G

Guest

Hi this is the second time Ive tried to use an if and else statement and I
get the "Else without if" error, in this instance I want text814 box to be
visible if the two conditions are true, otherwise (else) for the text box to
be invisible. Is it a syntax error or have I got the logic incorrect?
thanks Timboo.

Private Sub Text814_AfterUpdate()
If [Check819] =True And [Check41] = True Then Me!Text814 = Visible
Else: Me!Text814 = inVisible
End If
 
B

Brendan Reynolds

In VBA, you can use a single-line form of the If ... Then statement, with no
End If, like so ...

If condition Then result

Or you can use a multi-line version, with an End If like so ...

If condition Then
result
End If

Your code is trying to mix the two ...

'Single line If ... Then

If True = True And True = True Then MsgBox "One"

'Else without If - the If above was part of the single-line
'If ... Then statement, it is finished with now, and is not
'related in any way, as far as VBA is concerned, with
'this new instance of the 'Else' keyword.

Else: MsgBox "Two"
End If

BTW: To make a text box invisible, you set the Visible property of the text
box to False, e.g. "Me!ControlName.Visible = False".

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
B

Brendan Reynolds

"Brendan Reynolds" <brenreyn at indigo dot ie> wrote in message
If True = True And True = True Then MsgBox "One"

That might look a bit confusing. What I did was just to replace the
references to textboxes in your code with test values so that I could test
the code without having to reproduce your form and its controls.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
G

Guest

Brendan, many thanks for your reply, its so nice when ideas get translated
into something real, the frustration of knowing what you want to do but not
how to do it is deminished by people like you who take the time to "give",
thank you. Tim
 
F

fredg

Hi this is the second time Ive tried to use an if and else statement and I
get the "Else without if" error, in this instance I want text814 box to be
visible if the two conditions are true, otherwise (else) for the text box to
be invisible. Is it a syntax error or have I got the logic incorrect?
thanks Timboo.

Private Sub Text814_AfterUpdate()
If [Check819] =True And [Check41] = True Then Me!Text814 = Visible
Else: Me!Text814 = inVisible
End If

You would have been OK (well, almost OK) had you broken the first line
after the word 'Then'.
Also, = Visible or = InVisible are not access expressions.
You set the control's Visible property to True or False.
Also, the colon after Else is not needed.

Private Sub Text814_AfterUpdate()
If [Check819] =True And [Check41] = True Then
Me!Text814.Visible = True
Else
Me!Text814 .Visible = false
End If
End Sub
====
An easier method is simply:

Private Sub Text814_AfterUpdate()
Me!Text814.Visible = ([Check819] =True And [Check41] = True)
End Sub
 

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