Error trying to AllowAdditions in a subform with no records

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frieda

    #1

    Error trying to AllowAdditions in a subform with no records

    I needed to stop users from accidentally editing fields in several forms and subfoms. I'm not much of a programmer, so using code I found in various forums, I was able to turn off AllowEdits from On Current, then turn it back on when a button is clicked. Subforms are set to AllowEdits and AllowAdditions only when the parent form allows editing using On Enter.

    However, if a subform doesn't have any records, I get Error 2474, "The expression you entered requires the control to be in the active window." The debugger stops on the following line: Screen.ActiveCo ntrol.Parent.Al lowAdditions = True

    Here's the code I'm using:
    Code:
    'Called from clicking a button on the parent form
    Public Function Edit()
    Screen.ActiveForm.AllowEdits = Not Screen.ActiveForm.AllowEdits
    End Function
    
    'Called from On Current in the parent form
    Public Function Read()
    Screen.ActiveForm.AllowEdits = False
    End Function
    
    'Called from On Enter of the subform control
    Public Function AddEditSub()
    If Screen.ActiveForm.AllowEdits = True Then
        Screen.ActiveControl.Parent.AllowAdditions = True
        Screen.ActiveControl.Parent.AllowEdits = True
        Exit Function
            End If
    End Function
    
    'Called from On Enter of the subform control like previous when additions aren't needed
    Public Function EditSub()
    If Screen.ActiveForm.AllowEdits = True Then
        Screen.ActiveControl.Parent.AllowEdits = True
        Exit Function
            End If
    End Function
    
    'Called from On Exit of the subform control
    Public Function ReadSub()
    Screen.ActiveControl.Parent.AllowAdditions = False
    Screen.ActiveControl.Parent.AllowEdits = False
    End Function
    What am I doing wrong and how can I work around it?

    Thanks,
    Frieda
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    And you Only get this error when the subform has no records?
    Can you see the subform?
    Ive had issues before where I was using allowadditions= false and allowedits=fals e, and when the subform had no records, the form would not display. And with the form not being displayed, neither were the Edit and New buttons on my subform.

    Other then that, you could try to add a line saying:
    On Error Resume Next
    simply bypassing the error. You would have to test to see how that functions against your requirements.

    Comment

    Working...