Else without If error on compile

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pelicanstuff
    New Member
    • Nov 2007
    • 24

    Else without If error on compile

    Hi - Was wondering if anybody could tell me why this rather crappy code is giving me an 'Else without If' error on compile?
    All the Elses and Ifs look ok to me but there's a few.

    Code:
    Private Sub MySearchButtonClick()
    If gcfHandleErrors Then On Error GoTo PROC_ERR
      PushCallStack "MySearchButton"
    
    If Me.AllowFlexibility = True Then
            If Me.UseSpecialism = True And Me.UseCountryExperience = True Then
                If Me.Active = True Then
                    DoCmd.OpenQuery "Qry_amin5WithBothactive"
                Else
                    DoCmd.OpenQuery "Qry_amin5WithBoth"
                End If
            Else
            If Me.UseSpecialism = True And Me.UseCountryExperience = False Then
                If Me.Active = True Then
                    DoCmd.OpenQuery "Qry_amin5WithSpecialismsactive"
                Else
                    DoCmd.OpenQuery "Qry_amin5WithSpecialisms"
                End If
            Else
            If Me.UseSpecialism = False And Me.UseCountryExperience = True Then
                If Me.Active = True Then
                    DoCmd.OpenQuery "Qry_amin5WithCountryExpactive"
                Else
                    DoCmd.OpenQuery "Qry_amin5WithCountryExp"
                End If
            Else
            If Me.UseSpecialism = False And Me.UseCountryExperience = False Then
                If Me.Active = True Then
                    DoCmd.OpenQuery "Qry_amin5JoinEliminateSearchactive"
                Else
                    DoCmd.OpenQuery "Qry_amin5JoinEliminateSearch"
                End If
            
            End If
    
    Else
    
    If Me.UseSpecialism = True And Me.UseCountryExperience = True Then
                If Me.Active = True Then
                    DoCmd.OpenQuery "Qry_WithBothactive"
                Else
                    DoCmd.OpenQuery "Qry_WithBoth"
                End If
            Else
            If Me.UseSpecialism = True And Me.UseCountryExperience = False Then
                If Me.Active = True Then
                    DoCmd.OpenQuery "Qry_WithSpecialismsactive"
                Else
                    DoCmd.OpenQuery "Qry_WithSpecialisms"
                End If
            Else
            If Me.UseSpecialism = False And Me.UseCountryExperience = True Then
                If Me.Active = True Then
                    DoCmd.OpenQuery "Qry_WithCountryExpactive"
                Else
                    DoCmd.OpenQuery "Qry_WithCountryExp"
                End If
            Else
            If Me.UseSpecialism = False And Me.UseCountryExperience = False Then
                If Me.Active = True Then
                    DoCmd.OpenQuery "Qry_JoinEliminateSearchactive"
                Else
                    DoCmd.OpenQuery "Qry_JoinEliminateSearch"
                End If
            End If
            
            
    PROC_EXIT:
      PopCallStack
      Exit Sub
    
    PROC_ERR:
      GlobalErrHandler
      Resume PROC_EXIT
    End Sub
  • maxvernon
    New Member
    • Feb 2008
    • 12

    #2
    without knowing exactly what the intention of the code is the compiler is telling you to add 3 "End If' statements just above line 68. for example:

    67 End If
    68 End if
    69 End If
    70 Proc_Exit ' This line used to be line 68


    should do the trick

    Comment

    • maxvernon
      New Member
      • Feb 2008
      • 12

      #3
      make that four "End If" statements. Seems like you are assuming the VB "IF...ELSE...EN D IF" statement works like the Java "IF ELSE" statement which doesn't have an "END IF". In VB you must end each IF statement with a matching END IF

      Comment

      • pelicanstuff
        New Member
        • Nov 2007
        • 24

        #4
        Nope, putting those in doesn't help, it breaks at the 'else' on line 38. Hmmmmm.

        Comment

        • maxvernon
          New Member
          • Feb 2008
          • 12

          #5
          did you put in four "End If" statements?

          Comment

          • maxvernon
            New Member
            • Feb 2008
            • 12

            #6
            add 3 more "End If" statements at line 35...

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32656

              #7
              A good starting place would be to indent your code to match the If & looping structures. You have indenting of sorts, but as it doesn't match the code it's going to work against rather than for you.
              Lines 2 & 3 need to be rewritten. Your intentions are unclear here so I can't suggest replacement code.
              The rest will be obvious to you with correct indentation of the code. You're not simply interested in adding three "End If"s. You must add them into the logically correct positions. Only you can determine where that is for your logic.

              Comment

              • Stewart Ross
                Recognized Expert Moderator Specialist
                • Feb 2008
                • 2545

                #8
                NeoPa's right. With correct indentation and logic design things will fall into place.

                As NeoPa pointed out it is not just about adding Endifs. For interest I have removed all the inner if-then's which call one routine when 'active' is true and another when not. Straight away the unended if-then-elses become apparent, as do the flaws in the logic. There are unnecessary tests of True and False occurrences of the same variable in consecutive statements (lines 2 & 4 below, 6 & 8, etc).

                As an aside, I think you meant to use Elseif instead of the consecutive Else and If statements, but this would not help the flawed logic.

                Getting the logic right from the start would give you a better chance at a working solution - adding Endifs to keep the compiler happy is no solution to poor structure and logic.

                Code:
                 
                If Me.AllowFlexibility = True Then
                		If Me.UseSpecialism = True And Me.UseCountryExperience = True Then
                		Else
                		If Me.UseSpecialism = True And Me.UseCountryExperience = False Then
                		Else
                		If Me.UseSpecialism = False And Me.UseCountryExperience = True Then
                		Else
                		If Me.UseSpecialism = False And Me.UseCountryExperience = False Then
                		End If
                Else
                If Me.UseSpecialism = True And Me.UseCountryExperience = True Then
                		Else
                		If Me.UseSpecialism = True And Me.UseCountryExperience = False Then
                		Else
                		If Me.UseSpecialism = False And Me.UseCountryExperience = True Then
                		Else
                		If Me.UseSpecialism = False And Me.UseCountryExperience = False Then
                		End If
                -Stewart

                Comment

                • pelicanstuff
                  New Member
                  • Nov 2007
                  • 24

                  #9
                  Many thanks. I think I need to read some books quite urgently.

                  Don't worry about lines 2 and 3, they are for error handling. gcfHandleErrors is a global variable that determines whether debug mode is on or off.

                  It looks like I did mean to use Elseif - I've managed to muddle along so far (learning as I go along), but clearly I need to do some proper research.

                  Food for thought.

                  Comment

                  Working...