Results 1 to 15 of 15

If's with Elseif's

  1. #1
    yeah is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    Nov 2010
    Posts
    61

    If's with Elseif's

    Am stumbling on syntax...


    If rs.AbsolutePosition > 0 Then
    If rs!LastReadingID > 0 And rs!LastDoseID > 0 Then rs!Trend = GetGlucoseStatement(rs!SugarID, rs!LastReadingID, rs!LastDoseID, "Both")
    ElseIf rs!LastReadingID > 0 Then rs!Trend = GetGlucoseStatement(rs!SugarID, rs!LastReadingID, rs!LastDoseID, "Reading")
    ElseIf rs!LastDoseID > 0 Then rs!Trend = GetGlucoseStatement(rs!SugarID, rs!LastReadingID, rs!LastDoseID, "Dose")
    End If
    End If




    Why does the compiler puke on the last End If, telling me, "End if without block if" ? Aren't both Endif's needed?

  2. #2
    Minty is offline Hacker
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    804
    I think your logic flow is flawed - Indent the code and it becomes more obvious;
    Code:
        If rs.AbsolutePosition > 0 Then
            If rs!LastReadingID > 0 And rs!LastDoseID > 0 Then rs!Trend = GetGlucoseStatement(rs!SugarID, rs!LastReadingID, rs!LastDoseID, "Both")
        ElseIf rs!LastReadingID > 0 Then rs!Trend = GetGlucoseStatement(rs!SugarID, rs!LastReadingID, rs!LastDoseID, "Reading")
        ElseIf rs!LastDoseID > 0 Then rs!Trend = GetGlucoseStatement(rs!SugarID, rs!LastReadingID, rs!LastDoseID, "Dose")
        End If
    You may find this easier with a Select Case statement - they are certainly a lot easier to read.
    Please use the star below the post to say thanks if we have helped !

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    12,960
    As Minty said, indenting your code makes it more readable.
    Here is your code after being processed with SmartIndenter(a free utility) and placed within Code tags.
    Code:
    If rs.AbsolutePosition > 0 Then
            If rs!LastReadingID > 0 And rs!LastDoseID > 0 Then
                rs!Trend = GetGlucoseStatement(rs!SugarID, rs!LastReadingID, rs!LastDoseID, "Both")
            ElseIf rs!LastReadingID > 0 Then
                rs!Trend = GetGlucoseStatement(rs!SugarID, rs!LastReadingID, rs!LastDoseID, "Reading")
            ElseIf rs!LastDoseID > 0 Then
                rs!Trend = GetGlucoseStatement(rs!SugarID, rs!LastReadingID, rs!LastDoseID, "Dose")
            End If
    End If

  4. #4
    yeah is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    Nov 2010
    Posts
    61
    Ahaaaa. I get it. The 2 ElseIf's belong to the first If - not the second.
    I had no way of knowing that. Couldn't find a decent example.
    You are correct in your surmisal. The logic was flawed because my intent was that the 2 else-if's belong to the 2nd If.
    Is it impossible to have an elseif that belongs to the 2nd If (the nested If) ? Just askin'.
    Thx MUCHO for the heads up.

  5. #5
    yeah is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    Nov 2010
    Posts
    61
    I appreciate the tip. But I've been writing and indenting code since 1975.
    I have NO EARTHLY idea why it all left justifies when I paste it here.
    I tried stuffing space at the beginning of the lines. I tried TAB key - but that just tabb'd me to the next field on the form, a you would expect.
    so I gave up on it 'n posted. I figured if I couldn't cut 'n paste it directly from a procedure in a class module, it wasn't meant to be.

  6. #6
    yeah is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    Nov 2010
    Posts
    61
    Here's what I went with...

    Never mind... pasting code here is all left-justified. So I'm not gonna bother.
    Basically, I rid the code of the else-if's 'n did it with successive If statements.

    Thanks!

  7. #7
    Minty is offline Hacker
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    804
    If you enclose the code in code tags - press the # on the editor it's about . . . . . . ^ There on my display
    You'll find it doesn't remove the spare white space and maintains the indentation.

    As to the logic... Does this achieve the same in possibly a less confusing way (not much less in fairness)
    Code:
    If rs.AbsolutePosition > 0 Then        
            Select Case rs!LastReadingID
                Case 0
                    rs!Trend = GetGlucoseStatement(rs!SugarID, rs!LastReadingID, rs!LastDoseID, "Dose")
                Case Else
                    If rs!LastDoseID > 0 Then
                        rs!Trend = GetGlucoseStatement(rs!SugarID, rs!LastReadingID, rs!LastDoseID, "Both")
                    Else
                        rs!Trend = GetGlucoseStatement(rs!SugarID, rs!LastReadingID, rs!LastDoseID, "Reading")
                    End If
            End Select
    End If
    Please use the star below the post to say thanks if we have helped !

  8. #8
    yeah is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    Nov 2010
    Posts
    61
    Thanks for the tip on use of CODE TAGS. Seems to do the trick.
    What I concocted, you see below. Will have a stab at your select-case approach, test it 'n get back with you on it. 'preciate the suggestion.

    Code:
    Point1:
            If rs.AbsolutePosition > 0 Then
                If rs!LastReadingID > 0 And rs!LastDoseID > 0 Then rs!Trend = GetGlucoseStatement(rs!SugarID, rs!LastReadingID, rs!LastDoseID, "Both"): GoTo Point2
                If rs!LastReadingID > 0 Then rs!Trend = GetGlucoseStatement(rs!SugarID, rs!LastReadingID, rs!LastDoseID, "Reading"): GoTo Point2
                rs!Trend = GetGlucoseStatement(rs!SugarID, rs!LastReadingID, rs!LastDoseID, "Dose")
            End If
    Point2:
            rs.Update

  9. #9
    ridders52's Avatar
    ridders52 is offline Voodoo Is Practised
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    2,269
    Quote Originally Posted by yeah View Post
    Ahaaaa. I get it. The 2 ElseIf's belong to the first If - not the second.
    I had no way of knowing that. Couldn't find a decent example.
    You are correct in your surmisal. The logic was flawed because my intent was that the 2 else-if's belong to the 2nd If.
    Is it impossible to have an elseif that belongs to the 2nd If (the nested If) ? Just askin'.
    Thx MUCHO for the heads up.
    No. The two ElseIfs do belong to the second If
    However your code was all on one line which confused Access

    If you have code like this you don't need an End if line
    Code:
    If A=B Then C
    Whereas this does
    Code:
    If A =B Then
    C
    End if
    What you can't do is mix up the two methods in the same section of code
    Colin (Mendip Data Systems), Website, email
    If this has helped, please click the star button and leave a comment
    nil illegitimi carborundum est

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    12,960
    This is the link to SmartIndenter. I've used it with AC2003, 2010 an 2016.

    MzTools for VBA also has a code indenter, along with many other useful features.
    It comes with a 30 day free trial.

  11. #11
    Minty is offline Hacker
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    804
    @Orange - as i have found to my displeasure the smart indenter doesn't work on 64-bit Access

    I've reverted to using the one in MZ-Tools but it's not very customisable by comparison.
    I tried the one in Rubberduck but found it too unstable unfortunately.
    Please use the star below the post to say thanks if we have helped !

  12. #12
    ridders52's Avatar
    ridders52 is offline Voodoo Is Practised
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    2,269
    Quote Originally Posted by Minty View Post
    @Orange - as i have found to my displeasure the smart indenter doesn't work on 64-bit Access

    I've reverted to using the one in MZ-Tools but it's not very customisable by comparison.
    I tried the one in Rubberduck but found it too unstable unfortunately.
    I had also tried Rubberduck as it sounded excellent. Unfortunately it was so unstable it caused me problems, so I removed it
    Colin (Mendip Data Systems), Website, email
    If this has helped, please click the star button and leave a comment
    nil illegitimi carborundum est

  13. #13
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    12,960
    @Minty,

    Yes SmartIndenter is limited to 32 bit, but it's 15+ years old.
    Just curious, why did you select 64 bit Access?

  14. #14
    Minty is offline Hacker
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    804
    Quote Originally Posted by orange View Post
    @Minty,

    Yes SmartIndenter is limited to 32 bit, but it's 15+ years old.
    Just curious, why did you select 64 bit Access?
    I didn't - New corporate policy installs 64-bit office 365 on all users machine, including mine, and getting a different version would be really difficult/awkward/politically challenging.
    As it happens I have also discovered that you need to compile in the same bit level as the users machine, which I didn't know.

    So legacy equipment (running 32 bit access runtime) will run the accdb 64 bit version, but not the compiled version of the same database.
    So i'm making two copies of everything to be distributed based on the current environment, great fun.
    Learn something everyday!
    Please use the star below the post to say thanks if we have helped !

  15. #15
    ridders52's Avatar
    ridders52 is offline Voodoo Is Practised
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    2,269
    Quote Originally Posted by Minty View Post
    I didn't - New corporate policy installs 64-bit office 365 on all users machine, including mine, and getting a different version would be really difficult/awkward/politically challenging.
    As it happens I have also discovered that you need to compile in the same bit level as the users machine, which I didn't know.

    So legacy equipment (running 32 bit access runtime) will run the accdb 64 bit version, but not the compiled version of the same database.
    So i'm making two copies of everything to be distributed based on the current environment, great fun.
    Learn something everyday!
    I've been creating 32-bit & 64-bit accde files for a few years due to some clients running 64-bit.
    I had to install it myself just for that reason

    Whilst there's no way of avoiding creating 2 ACCDE files, I use an installer (SamLogic Visual Installer) which allows me to bundle both versions together.
    It then installs the correct version for the bitness of the target PC

    If you are interested, there's an example of this here: http://www.mendipdatasystems.co.uk/free-apps/4592091376 (Currency Exchange Rate Tracker)
    Colin (Mendip Data Systems), Website, email
    If this has helped, please click the star button and leave a comment
    nil illegitimi carborundum est

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Help with If...Then...ElseIf
    By willmafingerdo in forum Programming
    Replies: 2
    Last Post: 09-23-2016, 07:15 AM
  2. If then Elseif then....
    By Thompyt in forum Programming
    Replies: 3
    Last Post: 10-27-2014, 12:31 PM
  3. If ElseIf Not Working
    By theperson in forum Programming
    Replies: 5
    Last Post: 02-27-2013, 06:40 AM
  4. Help with ElseIf Statements
    By dshillington in forum Programming
    Replies: 6
    Last Post: 12-16-2011, 02:32 PM
  5. Easy one, If then ElseIf
    By Bruce in forum Forms
    Replies: 4
    Last Post: 12-01-2011, 11:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Tech Forums: Microsoft Office Forums