NESTED IF'S STATEMENT (1 Viewer)

adewale4favour

Registered User.
Local time
Today, 14:48
Joined
Aug 9, 2019
Messages
55
Hi,
I have a project currently in my hand. Two variables are to be checked with If Statements. If the first set of conditions are true, then the code should stop executing, if the first condition is true, then it should proceed to check the second condition. If the second condition is verified, then another bunch of code can continue.

I keep getting a compiler error: "If without Else", and when I reverse the code, "I get Else without If error"

Below is the code, please help if you can

Private Sub Command49_Click()
On Error Resume Next

If Me.ROUTE.Value = "SILO" Then
If IsNull(DLookup("ATC_SILO", "ATC_SILOqry")) Then
MsgBox "The request is currently not available", "No ATC"
Cancel = True

Else

Me.ATC_NO = DLookup("ATC_SILO", "ATC_SILOqry")
DoCmd.Save
DoCmd.SetWarnings False
DoCmd.OpenQuery "qrydeleteupdatedSILO"
DoCmd.SetWarnings True
DoCmd.Close
DoCmd.OpenForm "ATC_UTILIZATION", acNormal

Many thanks in advance
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:48
Joined
Feb 28, 2001
Messages
27,001
Actually, what you DON'T have is an End If. So what is probably happening is that a later "IF" statement gets involved with your "dangling" IF and decides to complain at that time.
 

adewale4favour

Registered User.
Local time
Today, 14:48
Joined
Aug 9, 2019
Messages
55
Many thanks, Doc_Man. But, the End If Code Block should usually come after the bunch of code is completed. That is included, yet the error persists.
 

Grumm

Registered User.
Local time
Today, 22:48
Joined
Oct 9, 2015
Messages
395
No what Doc means is that for each If you start you need an End If.
like this :
Code:
if condition then
    if otherstuff=false then
        do stuff
    end if
else
    let's do something
end if

So right after your "Cancel = true" you need an End If
 

jdraw

Super Moderator
Staff member
Local time
Today, 17:48
Joined
Jan 23, 2006
Messages
15,364
You will also find that if you indent your code especially if-endif blocks, it may improve readability.
Also, you should use code tags when posting any vba as Grumm has shown.
 

Users who are viewing this thread

Top Bottom