If / Then - Error Message

  • Thread starter Thread starter prosfora via AccessMonster.com
  • Start date Start date
P

prosfora via AccessMonster.com

I have the following code in the After Update event of my combo box and the
On Current event of my main form. If I try to open the main form, I get an
error message that reads: "Compile error, Block If without End If" (it also
highlights the End Sub in blue). If I take the code out of the main form, I
still get the same message after an update to my combo box. What have I done
wrong?

Private Sub cboresponsetype_AfterUpdate()
If Me.cboresponsetype = "A" Then
Me.sbfAAdd.Visible = True
Me.sbfBAdd.Visible = False
Me.sbfCAdd.Visible = False
Else
If Me.cboresponsetype = "B" Then
Me.sbfAAdd.Visible = False
Me.sbfBAdd.Visible = True
Me.sbfCAdd.Visible = False
Else
If Me.cboresponsetype = "C" Then
Me.sbfAAdd.Visible = False
Me.sbfBAdd.Visible = False
Me.sbfCAdd.Visible = True
End If
End Sub
 
If you use that construct you need ElseIf like this:

Private Sub cboresponsetype_AfterUpdate()
If Me.cboresponsetype = "A" Then
Me.sbfAAdd.Visible = True
Me.sbfBAdd.Visible = False
Me.sbfCAdd.Visible = False
ElseIf Me.cboresponsetype = "B" Then
Me.sbfAAdd.Visible = False
Me.sbfBAdd.Visible = True
Me.sbfCAdd.Visible = False
ElseIf Me.cboresponsetype = "C" Then
Me.sbfAAdd.Visible = False
Me.sbfBAdd.Visible = False
Me.sbfCAdd.Visible = True
End If
End Sub
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads
http://www.datastrat.com
http://www.mvps.org/access
 
prosfora via AccessMonster.com a écrit :
Private Sub cboresponsetype_AfterUpdate()
If Me.cboresponsetype = "A" Then
Me.sbfAAdd.Visible = True
Me.sbfBAdd.Visible = False
Me.sbfCAdd.Visible = False
Else
If Me.cboresponsetype = "B" Then
Me.sbfAAdd.Visible = False
Me.sbfBAdd.Visible = True
Me.sbfCAdd.Visible = False
Else
If Me.cboresponsetype = "C" Then
Me.sbfAAdd.Visible = False
Me.sbfBAdd.Visible = False
Me.sbfCAdd.Visible = True
End If
End Sub

For each "If" in your code, there should be an "End".
From VBA doc, the correct syntax is :
If condition Then
[statements]
[ElseIf condition-n Then
[elseifstatements] ...
[Else
[elsestatements]]
End If

In you situation, you should use "Select Case" though :

Select Case Me.cboresponsetype
Case "A"
Me.sbfAAdd.Visible = True
Me.sbfBAdd.Visible = False
Me.sbfCAdd.Visible = False
Case "B"
...
Case "C"
....
End Select
 
Thanks very much Arvin! Will be trying yours and Arnaud's suggestions
tonight!
If you use that construct you need ElseIf like this:

Private Sub cboresponsetype_AfterUpdate()
If Me.cboresponsetype = "A" Then
Me.sbfAAdd.Visible = True
Me.sbfBAdd.Visible = False
Me.sbfCAdd.Visible = False
ElseIf Me.cboresponsetype = "B" Then
Me.sbfAAdd.Visible = False
Me.sbfBAdd.Visible = True
Me.sbfCAdd.Visible = False
ElseIf Me.cboresponsetype = "C" Then
Me.sbfAAdd.Visible = False
Me.sbfBAdd.Visible = False
Me.sbfCAdd.Visible = True
End If
End Sub
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads
http://www.datastrat.com
http://www.mvps.org/access
I have the following code in the After Update event of my combo box and the
On Current event of my main form. If I try to open the main form, I get an
[quoted text clipped - 20 lines]
End If
End Sub
 
Thanks very much Arnaud! Will be trying yours and Arvin's suggestions
tonight!

Arnaud said:
prosfora via AccessMonster.com a écrit :
Private Sub cboresponsetype_AfterUpdate()
If Me.cboresponsetype = "A" Then
[quoted text clipped - 13 lines]
End If
End Sub

For each "If" in your code, there should be an "End".
From VBA doc, the correct syntax is :
If condition Then
[statements]
[ElseIf condition-n Then
[elseifstatements] ...
[Else
[elsestatements]]
End If

In you situation, you should use "Select Case" though :

Select Case Me.cboresponsetype
Case "A"
Me.sbfAAdd.Visible = True
Me.sbfBAdd.Visible = False
Me.sbfCAdd.Visible = False
Case "B"
...
Case "C"
....
End Select
 
Arvin and Arnaud,

Thanks again to both of you for providing the corrected code!! I have used
the Case Select for my final format, but do either of you know how I can make
all the forms hidden when the main form opens, or before any updates have
been made?



Thanks very much Arnaud! Will be trying yours and Arvin's suggestions
tonight!
prosfora via AccessMonster.com a écrit :
[quoted text clipped - 25 lines]
....
End Select
 
prosfora via AccessMonster.com a écrit :
Arvin and Arnaud,

Thanks again to both of you for providing the corrected code!! I have used
the Case Select for my final format, but do either of you know how I can make
all the forms hidden when the main form opens, or before any updates have
been made?

You're welcome, but this is a totally different question, and you
should therfore open a new thread for it.
 
Back
Top