Else Without If error

bconner

Registered User.
Local time
Today, 16:57
Joined
Dec 22, 2008
Messages
183
I am trying to run the code below in the On Timer Even but I keep getting the Else Without If Error Message. I have 2 If statments and 2 End If statement, I can't figure out what I am doing wrong.. Any help is greatly appreciated.....



Code:
Private Sub Form_Timer()
 
[FrmDashBoard].Requery
[FrmDashBoard2].Requery
 
If Time() > #11:30:00 PM# And Time() < #11:36:00 PM# Then DoCmd.Quit
 
Else
 
If (Time() > #10:27:00 AM# And Time() < #10:45:00 PM# And Weekday(Date) = 3) Then
DoCmd.RunMacro "DB Backups"
 
 
End If
End If
End Sub
 
A quick use of SMART INDENTER (free by the way) yields this:
Code:
Private Sub Form_Timer()
    [FrmDashBoard].Requery
    [FrmDashBoard2].Requery
    If Time() > #11:30:00 PM# And Time() < #11:36:00 PM# Then DoCmd.Quit
Else
    If (Time() > #10:27:00 AM# And Time() < #10:45:00 PM# And Weekday(Date) = 3) Then
        DoCmd.RunMacro "DB Backups"
 
    End If
End If
End Sub

Which shows you need to use more than one line for the one IF statement.

Code:
Private Sub Form_Timer()
    [FrmDashBoard].Requery
    [FrmDashBoard2].Requery
[B][COLOR=red]    If Time() > #11:30:00 PM# And Time() < #11:36:00 PM# Then[/COLOR][/B]
[B][COLOR=red]         DoCmd.Quit[/COLOR][/B]
[B][COLOR=red]    End If[/COLOR][/B]
Else
    If (Time() > #10:27:00 AM# And Time() < #10:45:00 PM# And Weekday(Date) = 3) Then
        DoCmd.RunMacro "DB Backups"
 
    End If
End If
End Sub
 
Thanks Bob, that worked!
 
That seems an odd construct, wouldn't it be more natural to use an ElseIf

Code:
Private Sub Form_Timer()
    [FrmDashBoard].Requery
    [FrmDashBoard2].Requery
    If Time() > #11:30:00 PM# And Time() < #11:36:00 PM# Then
         DoCmd.Quit
    ElseIf (Time() > #10:27:00 AM# And Time() < #10:45:00 PM# And Weekday(Date) = 3) Then
        DoCmd.RunMacro "DB Backups"
 
    End If

End Sub

Brian
 
Code:
Private Sub Form_Timer()
    [FrmDashBoard].Requery
    [FrmDashBoard2].Requery
[B][COLOR=red]  If Time() > #11:30:00 PM# And Time() < #11:36:00 PM# Then[/COLOR][/B]
[B][COLOR=red]       DoCmd.Quit[/COLOR][/B]
[B][COLOR=red]  End If[/COLOR][/B]
Else
    If (Time() > #10:27:00 AM# And Time() < #10:45:00 PM# And Weekday(Date) = 3) Then
        DoCmd.RunMacro "DB Backups"
 
    End If
End If
End Sub
This code still gives the "Else Without If error". The Else statement is clearly outside the IF statement

Brian's code would be a sensible way to go
 
If the program quits if the first rule is found to be true then you don't really need the else statement, just test for the second rule afterwards if the program has not quit.

Code:
Private Sub Form_Timer()
    [FrmDashBoard].Requery
    [FrmDashBoard2].Requery
 
    If Time() > #11:30:00 PM# And Time() < #11:36:00 PM# Then DoCmd.Quit
 
    If (Time() > #10:27:00 AM# And Time() < #10:45:00 PM# And Weekday(Date) = 3) Then DoCmd.RunMacro "DB Backups"
 
End Sub
 
I think the point for the OP is the syntax was wrong

The usage/syntax changes depending on how many statements you need to execute.
So:

Code:
if condition then action


if condition then
  action 1
  action 2
end if


if condition then
  action
else
  different action
end if
 

Users who are viewing this thread

Back
Top Bottom