If ..Else.. End If "Compile Error: Else without If" what have i missed?

wiklendt

i recommend chocolate
Local time
Tomorrow, 07:38
Joined
Mar 10, 2008
Messages
1,746
i am developing this code, and getting the error
compile error: else without if
highlighting the "Else" where i have it in red in the code below. i have counted all my if's, then's, else's and end if's... i can't see the problem? can anyone else? (oh, and if anyone can see that this code is doomed to fail in other ways, let me know!)

Code:
[COLOR=Blue]Private Sub[/COLOR] Detail_Format(Cancel [COLOR=Blue]As Integer[/COLOR], FormatCount [COLOR=Blue]As Integer[/COLOR])

[COLOR=Blue]Dim [/COLOR]sct [COLOR=Blue]As [/COLOR]Section

[COLOR=SeaGreen]' first check whether the form with the checkbox is open[/COLOR]

[COLOR=Blue]If [/COLOR]Forms!frmLISTS.IsLoaded = True [COLOR=Blue]Then[/COLOR]
[COLOR=SeaGreen]
    ' check whether checkbox on frmLISTS is true or false[/COLOR]
    [COLOR=Blue]If [/COLOR]Forms!frmLISTS.chkReportDetails = [COLOR=Blue]False Then[/COLOR] [COLOR=SeaGreen]' details not checked[/COLOR]
    
[COLOR=SeaGreen]        ' user wants no details
        ' select all sections with tag "UserDetails" and make invisible[/COLOR]
        [COLOR=Blue]For Each [/COLOR]sct [COLOR=Blue]In [/COLOR]Me
        [COLOR=Blue]If [/COLOR]sct.Tag = "UserDetails" [COLOR=Blue]Then[/COLOR]
            sct.Visible = [COLOR=Blue]False[/COLOR]
     [COLOR=Blue]   End If[/COLOR]
        
  [COLOR=Red]  Else[COLOR=Black] [COLOR=SeaGreen]' details checked, show all sections[/COLOR][/COLOR][/COLOR]
        
       [COLOR=Blue] For Each[/COLOR] sct [COLOR=Blue]In [/COLOR]Me
        [COLOR=Blue]If [/COLOR]sct.Tag = "UserDetails" [COLOR=Blue]Then[/COLOR]
            sct.Visible = [COLOR=Blue]True[/COLOR]
   [COLOR=Blue]     End If[/COLOR]
    
[COLOR=Blue]    End If[/COLOR]

[COLOR=Blue]Else[/COLOR][COLOR=SeaGreen]' checkbox unavailable, show all sections as default[/COLOR]
    
        [COLOR=Blue]For Each[/COLOR] sct [COLOR=Blue]In [/COLOR]Me
        [COLOR=Blue]If [/COLOR]sct.Tag = "UserDetails" [COLOR=Blue]Then[/COLOR]
            sct.Visible = [COLOR=Blue]True[/COLOR]
       [COLOR=Blue] End If

End If[/COLOR] 

[COLOR=Blue]End Sub[/COLOR]
 
ok, got rid of that error by putting in "next" at the end of my "for each":
Code:
        ' user wants no details
        ' select all sections with tag "UserDetails" and make invisible
        For Each sct In Me
        If sct.Tag = "UserDetails" Then
            sct.Visible = False
        End If
[COLOR=Red]        Next[/COLOR]
but now i'm getting error on
Code:
Forms!frmLISTS.IsLoaded = True
run-time error 2465
application-defined or object-defined error
i also tried
Code:
If IsLoaded(Forms!frmLISTS) Then
but it simply closes the report when i try to open it (without error popups)

EDIT:

and i know it's the IsLoaded causing the close b/c i've also tried this with the same symptom of report closing when trying to open without error messages:

Code:
If IsLoaded(Forms!frmLISTS) Then
[INDENT] MsgBox "Form Loaded"
[/INDENT]Else ' checkbox unavailable, show all sections as default
[INDENT] MsgBox "Form Not Loaded"
[/INDENT]End If
 
Last edited:
You've already noticed how any error such as "If without End If" or "Do without Next" are rarely accurate, especially if it's nested in other control statement. I have to make a mental note that it's not necessary just the "Else" that's missing but something else as well.

As for error, try this:

Code:
?AccessError(2465)

in the immediate windows to get you the full error message.
 
ha! i always wanted to know what that "immediate" window is for! ;)

?AccessError(2465)
Microsoft Office Access can't find the field '|' referred to in your expression.@You may have misspelled the field name, or the field may have been renamed or deleted.@@1@1@11730@1

hm. no, the frmLISTS is called frmLISTS. i triple checked that just now.

i think i need to focus my search on the IsLoaded thing - i seem to remember some time ago there's funny things going on there in general with access.
 
ah... ok, here we go - turns out cut'n'paste ain't always the most simple way to do things... forgot to paste in the IsLoaded module from my other project... ;)

Code:
Function IsLoaded(ByVal strFormName As String) As Boolean

  'Returns True if the specified form is open in Form view or Datasheet view.

    Const conObjStateClosed = 0
    Const conDesignView = 0

    If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
        If Forms(strFormName).CurrentView <> conDesignView Then
            IsLoaded = True
        End If
    End If

End Function
but now i get another error (there's no end to it!! :eek:)

Compile error: type mismatch
on the same code
Code:
If IsLoaded(Forms!frmLISTS) Then

waaaaaaa!!! :(
 
Well, I'd verify that IsLoaded is actually available in your Access's version. It was kinda recent, IIRC.

Oh! Oh! I remember now!

You can't use Forms collections to check if a form is IsLoaded. Forms collections contain only the forms that's already opened. Therefore you have to reference AllForms collection to test if a form within the AllForms collection is IsLoaded. That's why you got the error- IsLoaded is a property of AccessObject, not a Form or Report, IIRC.

Try and use AllForms collections and see what happens.
 
for interests' sake, i get the object required error also on:

If AllForms!frmLISTS.IsLoaded Then

(i am using Access 2007)
 
I dont have Access in front of me right now, but verify if you need to reference Allforms somehow... I'm wondering if it has to be something like CurrentProject.AllForms("blah").isloaded?

Check the helpfiles on AllForm collections and see what syntax it suggests...
 
and one more thing- I forgot to said earlier-

If you are using 2007, then you should definitely have IsLoaded property builtin and do not need that IsLoaded function- it's good only for older version. Comment it out so Access doesn't get confused which IsLoaded you want, the builtin property or the function.
 
I dont have Access in front of me right now, but verify if you need to reference Allforms somehow... I'm wondering if it has to be something like CurrentProject.AllForms("blah").isloaded?

Check the helpfiles on AllForm collections and see what syntax it suggests...
will do. have to work on something else right now, but i'll chew on it. thanks banana :)
 
and one more thing- I forgot to said earlier-

If you are using 2007, then you should definitely have IsLoaded property builtin and do not need that IsLoaded function- it's good only for older version. Comment it out so Access doesn't get confused which IsLoaded you want, the builtin property or the function.

damn, i completely forget everytime that although i'm developing in access 2007, i'm using originally 2000 mdb files which will mostly be used on computers with access 2003... and i think the added module is important for those - if memory serves... ?

but anyway, yes, i'll do more searching in Allforms / CurrentProject... etc. :)
 
Ah, yes, it could be a problem. I am afraid I can't remember exactly when Isloaded was introduced, so we'll ahve to make sure about that, and if it's not going to be available, we can just forget about using AllForms anyway...

Thanks for the kind thoughts- I'm sure there's lot other of great folks here who could stand to get one or two green jellybeans. I think I'd like some red, though... you know, the cinnamon ones? :D
 
i've seen bob's last jelly is a light green - so there is scope for a more colourful approach! :)
 
is this sorted now

i think its the same point rich just made

you are passing to isloaded the form itself, but the function you have wants a string


------------
what are you trying to do generally - can you explain?
 
Since Access 2000 (SP1 at least) this has been available:

CurrentProject.AllForms("YourFormNameHereInQuotes").IsLoaded
 

Users who are viewing this thread

Back
Top Bottom