Results 1 to 10 of 10
  1. #1
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544

    For without next, if without end if warnings driving me crazy!

    Hello everyone!



    I don't know if I'm brain damaged or what, I'm working on a very simple routine to deactivate the Main Menu buttons if it is past a certain date (for demo purposes). My main menu (mainmenuskeleton) has a current date field (todaysdate) and a "license" field (bound to the underlying table, CompanyInformation) and several command buttons to open various forms. My code:
    Code:
    Private Sub Form_Load()
    
        Dim ctrl As Control
    
        For Each ctrl In Me.Controls
            If Forms![mainmenuskeleton]!todaysdate > #4/1/2017# And TypeOf ctrl Is CommandButton Then
                Debug.Print todaysdate
    
                ctrl.Enabled = True
                Me.license.Visible = True
    
            End If
        Next
        
        Me.license.Visible = True
            If Me.license = "p060117" Then
            For Each ctrl In Me.Controls
                ctrl.Enabled = True
    
            End If
        Next
    
    
    End Sub
    I keep getting the error "end if without block if" with the last "end if" highlighted. And if it's not the "IFS" giving me trouble, I will get errors on "For without next". Grrrrrrr. TIA!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    Code:
        Me.license.Visible = True
            If Me.license = "p060117" Then
            For Each ctrl In Me.Controls
                ctrl.Enabled = True
            next
            End If
        Next

  3. #3
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Thank you ranman!

    So end if doesn't have to come before next? I read a thread that it did! Working now. Thanks again!

  4. #4
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544

    Spoke too soon (for, next, if, end if continued)

    I did speak too soon. I am now getting an error "object doesn't support this property or method" highlights "ctrl.enabled=true". And here's the code again:
    Code:
    Dim ctrl As Control
    
        For Each ctrl In Me.Controls
            If Forms![mainmenuskeleton]!todaysdate > #4/1/2017# And TypeOf ctrl Is commandbutton Then
                Debug.Print todaysdate
    
                ctrl.Enabled = False
                Me.license.Visible = True
    
            End If
        Next
        
           Me.license.Visible = True
            If Me.license = "p060117" Then
            For Each ctrl In Me.Controls
                ctrl.Enabled = True
    
            Next
            End If
    Thoughts?

  5. #5
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,369
    You indent your code, which is very good practice. That will help you spot such errors - it helps you see incomplete "blocks". Which comes before which really depends on the order that the pairs are nested which is done according to how you want to execute the logic, so assuming my If is to be processed independently from the With
    Code:
    For
     If
      With
    must be paired as
    Code:
    For
      If
    
      End If
      With
      
      End With
    
    Next
    If my With is to be dependent on my If then it would be
    Code:
    For
      If
        With
      
        End With
    
      End If
    
    Next
    and I would alter my indentation to reflect that. It's like brackets; they are grouped as pairs.
    EDIT:
    TypeOf ctrl Is commandbutton Then
    I've never seen that syntax or type declaration. Try And ctl.Type = acCommandButton
    I think your code could use a bit of tweaking if you want me to make suggestions.
    Last edited by Micron; 04-13-2017 at 11:18 AM. Reason: added info
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Quote Originally Posted by Micron View Post
    You indent your code, which is very good practice. That will help you spot such errors - it helps you see incomplete "blocks". Which comes before which really depends on the order that the pairs are nested which is done according to how you want to execute the logic, so assuming my If is to be processed independently from the With
    Code:
    For
     If
      With
    must be paired as
    Code:
    For
      If
    
      End If
      With
      
      End With
    
    Next
    If my With is to be dependent on my If then it would be
    Code:
    For
      If
        With
      
        End With
    
      End If
    
    Next
    and I would alter my indentation to reflect that. It's like brackets; they are grouped as pairs.
    EDIT:

    I've never seen that syntax or type declaration. Try And ctl.Type = acCommandButton
    I think your code could use a bit of tweaking if you want me to make suggestions.
    Of course, I am always open to suggestions Micron! Thank you for taking the time!

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    Quote Originally Posted by Gina Maylone View Post
    I am now getting an error "object doesn't support this property or method" highlights "ctrl.enabled=true".
    You're hitting a control that doesn't have the enabled property, like a label. Either test for the types of controls you want to affect, or use the Tag property to identify the controls you want to affect.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    PBaldy - I am testing for commandbuttons (ctl.Type = acCommandButton) per micron. You guys, this is so weird. I used ranman's suggestion and it worked fine, disabled all the command buttons as I wanted. Now when I go into the menu, I get "object doesn't support this property or method". Why would Access decide mid-stream it's not going to support it?! I am able to disable the buttons individually, which is a drag. Still open for suggestions. Thanks everyone for the time!

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    You've got two loops. The first checks the control type; the second does not.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,369
    I might not be understanding the logical tests (i.e. what if the date passes but the license does not, or vice versa?) but I'm thinking they can be combined. Plus, it seems license is visible regardless, so it may as well be set once. The first avoids having to evaluate the tests for every command button.
    Code:
    Dim ctrl As Control
    
    Me.license.Visible = True 'seems to be the case regardless, so only written once
    If Forms![mainmenuskeleton]!todaysdate > #4/1/2017# Or Me.license <> "p060117" Then
      For Each ctrl In Me.Controls
        If ctrl.Type = acCommandButton Then ctrl.Enabled = False
      Next
    Else
      For Each ctrl In Me.Controls
        If ctrl.Type = acCommandButton Then ctrl.Enabled = True
      Next
    End If
    Another way is less code, but requires the evaluation to take place for each command button. Either way is a bit shorter than what you had; just thought I'd show some alternatives. I have to presume all is nested correctly as I can't test the above. Sure would be embarrassing if it wasn't, given that's the initial subject here.
    Code:
    Dim ctrl As Control
    
    Me.license.Visible = True
    For Each ctrl In Me.Controls
      If Forms![mainmenuskeleton]!todaysdate > #4/1/2017# Or Me.license <> "p060117" Then
        If ctrl.Type = acCommandButton Then ctrl.Enabled = False
      Else
        If ctrl.Type = acCommandButton Then ctrl.Enabled = True
      End If
    Next
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. #Type! error driving me crazy!
    By N1755L in forum Forms
    Replies: 22
    Last Post: 05-06-2014, 03:22 PM
  2. Dlookup driving me crazy
    By NJMike64 in forum Modules
    Replies: 3
    Last Post: 04-19-2014, 01:58 PM
  3. Too few arguments is driving me crazy....
    By Spidee in forum Access
    Replies: 3
    Last Post: 07-10-2013, 07:41 AM
  4. I know it's easy but it's driving me crazy!!!
    By pensived in forum Queries
    Replies: 1
    Last Post: 02-22-2012, 02:55 AM
  5. ShipToCode is driving me crazy
    By Accessgrasshopper in forum Access
    Replies: 7
    Last Post: 02-26-2011, 04:55 PM

Posting Permissions

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