1

I am using the following code as event handler for the button cmd_Edit on my main form:

Private Sub cmd_Edit_Click()

    If intCanEdit = False Then

        If MsgBox("Sollen vorhandene Prozeduren verändert werden ?", vbYesNo, "Frage") = vbNo Then Exit Sub
        Me.AllowEdits = True
        Me.AllowAdditions = True

        Dim sbfrm As Control
        For Each sbfrm In Me.Controls
            With sbfrm
                Select Case .ControlType
                    Case acSubform
                          .Form.AllowEdits = True
                          .Form.AllowAdditions = True
                End Select
            End With
        Next sbfrm

        intCanEdit = True
    Else

        Me.AllowEdits = False
        Me.AllowAdditions = False

        For Each sbfrm In Me.Controls
            With sbfrm
                Select Case .ControlType
                    Case acSubform
                          **.Form.AllowEdits = False**
                          .Form.AllowAdditions = False
                End Select
            End With
        Next sbfrm

        intCanEdit = False
    End If

    cmd_Edit.Caption = IIf(intCanEdit, "Click to Save", "Click to Edit")
    cmd_Edit.BackColor = IIf(intCanEdit, vbRed, vbGreen)

End Sub

The form loads with intCanEdit set to False. When i click the button once (setting it to true) everything works as expected, when i click it again (setting it to false again) i get an error (Runtime error 2455) with the Debugger sending me to the line i marked with asterisks in the above code.

Does anybody have an idea why i can set the property to True with my code, but get an error when i try to set the same property back to False? :(

June7
  • 19,874
  • 8
  • 24
  • 34
user71302
  • 11
  • 2
  • 1
    Have you read these two questions? I'm not familiar with Access-Vba, but they could help you find the source of the problem: [Set Form Properties on SubSubform from the Main Form with VBA](https://stackoverflow.com/questions/12417866/set-form-properties-on-subsubform-from-the-main-form-with-vba) , [Subform reference throws Error 2455](https://stackoverflow.com/questions/29830292/subform-reference-throws-error-2455-you-entered-an-expression-that-has-an-inval) . – dadler Jan 18 '18 at 11:02
  • Ran your code and don't get the error. Where do you declare the intCanEdit variable? Edit your question with additional info. I declared in the module header. – June7 Jan 19 '18 at 09:57
  • Why even have a procedure where user has to choose to make form editable? – June7 Jan 19 '18 at 10:17
  • Dear dadler, thank you very much for your comment. I have read those threads in the meantime, and i have used someone elses code to lock the controls instead of setting the allowedits property. Along this way i got the exact same problem again, i.e., unlocking works, locking again throws runtime 2455. And i noticed that the error only pops up when working on a record which has a specific sub-subform empty. So it seems the code isnt the real problem, but the sub-subform. – user71302 Jan 20 '18 at 04:56
  • Dear June7, thanks for running my code snippet. I declare it as Dim As Integer in the head of the forms module and i set it to False On_Load. By now, i think the problem sits in one of the forms subsubforms. Somehow, when its empty i get the error, when it has data, i dont get it. Still dont know how to fix that, though. Any ideas ? And as for the "why", im looking for the simplest possible way to avoid inadvertent changes to a record, and "my" users asked me for pretty much exactly this function. How would you solve that ? – user71302 Jan 20 '18 at 05:00
  • I have projects where I do exactly this but it's only parent and child - not grandchild sub-form. And I have no problems when the sub-form has no data. What is interesting in your case is that the positive case works but not the negative case. You mention "subsubform". Do you mean that? Is there a sub-form in the sub-form? (I haven't tried this in that case because I don't have a ready to go example.) What sort of sub-form is it - datasheet, form, continuous form? – andrew Jan 21 '18 at 03:08

0 Answers0