If Then Else - End If

T

TeeSee

Private Sub SiScode_AfterUpdate()
If Me.Indicator.Value <> 0 Then _
GoTo LeaveSub Else _
MsgBox "You MUST select whether you are adding to VMI or billing
CD before proceeding", , "PAY ATTENTION!"
End If ' This is "END IF #1"

If (Me.Dirty = True) Then
' Clear it
Me.Undo
End If
Me.SiScode.SetFocus
LeaveSub:
End If ' This is "END IF #2"
End Sub

I originally made the mistake of placing the END IF #2 and got a
compile error referencing block if issue. Searched the group and felt
that I found the answer from Marsh saying that each if/end is
indepenent of each other. I then moved the end if to position END IF
#1 and still get the same compile error. The strange thing to me is
that without either of those two end ifs the code compiles and runs as
expected.
Do you actually require an end if in this set of circumstances??
 
R

ruralguy via AccessMonster.com

Remove all of the line continuation characters "_" and see how the compiler
takes it.
 
T

TeeSee

Remove all of the line continuation characters "_" and see how the compiler
takes it.








--
RuralGuy (RG for short) aka Allan Bunch MS Access MVP - acXP WinXP Pro
Please post back to this forum so all may benefit.

Message posted viahttp://www.accessmonster.com- Hide quoted text -

- Show quoted text -

The compiler like this ...
Private Sub SiScode_AfterUpdate()
If Me.Indicator.Value <> 0 Then GoTo LeaveSub Else MsgBox "You
MUST select whether you are adding to VMI or billing CD before
proceeding", , "PAY ATTENTION!"

If (Me.Dirty = True) Then
' Clear it
Me.Undo
End If
Me.SiScode.SetFocus
LeaveSub:
End Sub
***********************************But does NOT like
this*************************
Private Sub SiScode_AfterUpdate()
If Me.Indicator.Value <> 0 Then GoTo LeaveSub Else MsgBox "You
MUST select whether you are adding to VMI or billing CD before
proceeding", , "PAY ATTENTION!"
End If ' *************HERE it is Here ************
If (Me.Dirty = True) Then
' Clear it
Me.Undo
End If
Me.SiScode.SetFocus
LeaveSub:
End Sub
 
R

ruralguy via AccessMonster.com

The compiler prefers that:
If ... Then
Else
End If
...be on different lines in order to properly interpret the syntax.
It is also *much* easier to read. FWIW, you can code your procedure so no
GoTo is used! You can also *not* set the focus to the current control from
within the current control. You are already there!
Remove all of the line continuation characters "_" and see how the compiler
takes it.
[quoted text clipped - 31 lines]
- Show quoted text -

The compiler like this ...
Private Sub SiScode_AfterUpdate()
If Me.Indicator.Value <> 0 Then GoTo LeaveSub Else MsgBox "You
MUST select whether you are adding to VMI or billing CD before
proceeding", , "PAY ATTENTION!"

If (Me.Dirty = True) Then
' Clear it
Me.Undo
End If
Me.SiScode.SetFocus
LeaveSub:
End Sub
***********************************But does NOT like
this*************************
Private Sub SiScode_AfterUpdate()
If Me.Indicator.Value <> 0 Then GoTo LeaveSub Else MsgBox "You
MUST select whether you are adding to VMI or billing CD before
proceeding", , "PAY ATTENTION!"
End If ' *************HERE it is Here ************
If (Me.Dirty = True) Then
' Clear it
Me.Undo
End If
Me.SiScode.SetFocus
LeaveSub:
End Sub
 
J

JohnB

I see that ruralguy has helped you out (and I'm not capable of helping) but
I'm curious - did you leave an 'End If' out of the code where you said:

The compiler like this ...
Private Sub SiScode_AfterUpdate()
If Me.Indicator.Value <> 0 Then GoTo LeaveSub Else MsgBox "You
MUST select whether you are adding to VMI or billing CD before
proceeding", , "PAY ATTENTION!"

If (Me.Dirty = True) Then
' Clear it
Me.Undo
End If
Me.SiScode.SetFocus
LeaveSub:
End Sub

Shouldn't there be an 'End If' on a new line after the PAY ATTENTION line?
Otherwise your code has two Ifs and only one End If. Just trying to
understand this stuff.
 
T

TeeSee

I see that ruralguy has helped you out (and I'm not capable of helping) but
I'm curious - did you leave an 'End If' out of the code where you said:

The compiler like this ...
Private Sub SiScode_AfterUpdate()
    If Me.Indicator.Value <> 0 Then GoTo LeaveSub Else MsgBox "You
MUST select whether you are adding to VMI or billing CD before
proceeding", , "PAY ATTENTION!"

    If (Me.Dirty = True) Then
        ' Clear it
        Me.Undo
    End If
    Me.SiScode.SetFocus
LeaveSub:
End Sub

Shouldn't there be an 'End If' on a new line after the PAY ATTENTION line?
Otherwise your code has two Ifs and only one End If. Just trying to
understand this stuff.







- Show quoted text -

I am trying to understand it as well. The answer is yes ... I did
leave out the "end if" and the compiler compiled it which is the part
I don't yet get. I changed it as per what I understood from the last
post and here it is.
Private Sub SiScode_AfterUpdate()
If Me.Indicator.Value <> 0 Then
GoTo LeaveSub
Else: MsgBox "You MUST select whether you are adding to VMI or
billing CD before proceeding", , "PAY ATTENTION!"
End If

If (Me.Dirty = True) Then
' Clear it
Me.Undo
End If
Requery
'Me.SiScode.SetFocus
LeaveSub:
End Sub

The other part I don't get is the setfocus part .... By the time i get
this message box displayed and I click the YES button the focus has
gone to the second control on the form. As you can see from the above
the only way I could come up with to get focus back to the top control
on the form was REQUERY. Are there any repercussions I shd expect from
that?
How else would you code this so as not to use a GOTO??

Thanks for the comments.
 
T

Tom Lake

TeeSee said:
I am trying to understand it as well. The answer is yes ... I did
leave out the "end if" and the compiler compiled it which is the part
I don't yet get. I changed it as per what I understood from the last
post and here it is.
Private Sub SiScode_AfterUpdate()
If Me.Indicator.Value <> 0 Then
GoTo LeaveSub
Else: MsgBox "You MUST select whether you are adding to VMI or
billing CD before proceeding", , "PAY ATTENTION!"
End If

If (Me.Dirty = True) Then
' Clear it
Me.Undo
End If
Requery
'Me.SiScode.SetFocus
LeaveSub:
End Sub

The other part I don't get is the setfocus part .... By the time i get
this message box displayed and I click the YES button the focus has
gone to the second control on the form. As you can see from the above
the only way I could come up with to get focus back to the top control
on the form was REQUERY. Are there any repercussions I shd expect from
that?
How else would you code this so as not to use a GOTO??

Use Exit Sub rather than GoTo LeaveSub

Tom Lake
 
R

ruralguy via AccessMonster.com

Try this code in the *BeforeUpdate* event of the control.

Private Sub SiScode_BeforeUpdate(Cancel As Integer)

If Me.Indicator = 0 Then
MsgBox "You MUST select whether you are adding to VMI " & _
"or billing CD before proceeding", , "PAY ATTENTION!"
Cancel = True
End If

End Sub

Why are you checking the Indicator control in this control?
I see that ruralguy has helped you out (and I'm not capable of helping) but
I'm curious - did you leave an 'End If' out of the code where you said:
[quoted text clipped - 84 lines]
- Show quoted text -

I am trying to understand it as well. The answer is yes ... I did
leave out the "end if" and the compiler compiled it which is the part
I don't yet get. I changed it as per what I understood from the last
post and here it is.
Private Sub SiScode_AfterUpdate()
If Me.Indicator.Value <> 0 Then
GoTo LeaveSub
Else: MsgBox "You MUST select whether you are adding to VMI or
billing CD before proceeding", , "PAY ATTENTION!"
End If

If (Me.Dirty = True) Then
' Clear it
Me.Undo
End If
Requery
'Me.SiScode.SetFocus
LeaveSub:
End Sub

The other part I don't get is the setfocus part .... By the time i get
this message box displayed and I click the YES button the focus has
gone to the second control on the form. As you can see from the above
the only way I could come up with to get focus back to the top control
on the form was REQUERY. Are there any repercussions I shd expect from
that?
How else would you code this so as not to use a GOTO??

Thanks for the comments.
 

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