Solved how to control enable and disable a sub form from another sub form (1 Viewer)

mloucel

Member
Joined
Aug 5, 2020
Messages
214
Hello Gurus:
I have the following form, that contains 2 sub forms [CONTINUOUS FORMS BOTH],
When MainForm is opened
SF1 is opened with AllowAdditions = False
and SF2 with AllowAdditions = True


when the user clicks the ADD button
I perform the following code:

Code:
    Me.AllowAdditions = True
    DoCmd.GoToRecord , "", acNewRec
    Forms("MainForm").SF2.enable = False

stopping the user from entering any info on SF2 until a new record is created and saved.
So far so good and it works as it should.

MY PROBLEM
when the user clicks the SAVE button, I perform the opposite of the above code, because I need to re-enable SF2 so that the User can enter data.

Code:
    DoCmd.RunCommand acCmdSaveRecord
    Me.AllowAdditions = False
    Forms("MainForm").SF2.enable  = True
    SaveButton.Enabled = False

I have an error routine that gives me the following error:

"Object doesn't support this property or method"


I have taken out the LINE:
acCmdSaveRecord
since the error started there.
then gave me the error again with the line:
"Forms("MainForm").SF2.enable = True"

and I am out of Ideas and I have NO IDEA how to solve it.
The COMPLETE code for BOTH buttons is at the end of the picture.

PLEASE kindly remember that I am asking for help because I am a beginner, I am trying my best not to ask many questions but from time to time I need help to get out of my own mess, do not feel offended by my stupid questions, I rather be a fool once and ask the question than forever and ignore my problem.

Thanks.
Maurice.


EXAMPLE FORM.png



This is the ADD code:
Code:
Private Sub AddButton_Click()
On Error GoTo AddButton_Click_Err

    On Error Resume Next
    Me.AllowAdditions = True
    
    ' This is allowed in a continuous form
    
    DoCmd.GoToRecord , "", acNewRec
    Forms("MainForm").SF2.enable = False
    SaveButton.Enabled = True
    
    If (MacroError <> 0) Then
        Beep
        MsgBox MacroError.Description, vbOKOnly, ""
    End If


AddButton_Click_Exit:
 
    Exit Sub

AddButton_Click_Err:
    MsgBox Error$
    Resume AddButton_Click_Exit

End Sub

=================================================================
This is the SAVE code:
Code:
Private Sub SaveButton_Click()
On Error GoTo SaveButton_Click_Err
    
    DoCmd.RunCommand acCmdSaveRecord
    Me.AllowAdditions = False
    Forms("MainForm").SF2.enable  = True
    SaveButton.Enabled = False
    
SaveButton_Click_Exit:
    Exit Sub

SaveButton_Click_Err:
    MsgBox Error$
    Resume SaveButton_Click_Exit

End Sub
 
You could refactor that code slightly
Code:
    If Me.Dirty then Me.Dirty = False
    Me.AllowAdditions = False
    Me.Parent.SF2.Enabled  = True
    SaveButton.Enabled = False

See if that works or you.

Make sure you have Option Explicit at the top of all you code modules.
 
You could refactor that code slightly
Code:
    If Me.Dirty then Me.Dirty = False
    Me.AllowAdditions = False
    Me.Parent.SF2.Enabled  = True
    SaveButton.Enabled = False

See if that works or you.

Make sure you have Option Explicit at the top of all you code modules.
IT WORKED!!!
and I found my error as well thanks to you and @pbaldy
is ENABLED not enable :):):) thanks for your kind help.
I was also wondering about the me.dirty code just was not sure, now I am.

Thanks a million.
 
It's Enabled, not enable.
YES my friend, you are 100% absolutely right, weird part is that in the first part of the code
Enable=false
it worked, so a mystery there, once I changed it to Enabled ... poof ... worked like a charm, no problems whatsoever.

I also learned a better way to code by using
" Me.Parent" instead of the whole " Forms("MainForm")" which is easier.
as well as using the if me.dirty then me.dirty=false
thanks to @Minty .
This little things do improve myself by learning from the great ones.

Thank you so much to both.
 
YES my friend, you are 100% absolutely right, weird part is that in the first part of the code
Enable=false
it worked, so a mystery there, once I changed it to Enabled ... poof ... worked like a charm, no problems whatsoever.

I also learned a better way to code by using
" Me.Parent" instead of the whole " Forms("MainForm")" which is easier.
as well as using the if me.dirty then me.dirty=false
thanks to @Minty .
This little things do improve myself by learning from the great ones.

Thank you so much to both.

Happy to help!
 
I think MacroError is for macros, not VBA ?
You are right !!

I converted the button from Macro to VBA, then added the extra code I needed for my buttons to work the way I wanted them to work, I did not see that those likes are not even going to be executed since I am no longer working with a macro.
I already got rid of them.
Thanks for your kind help, I hope some day I will be able to help others as well as you guys help me.

Sincerely;

Maurice.
 
Thanks! I hope it helps, let me know if you need help.
If you don't mind a last question...

I would like to do the following:
After the user clicks on the save button, the last line to move the focus to a control on SF2, I have been experimenting with the following code:

Code:
Me.Parent.SF2.Form.FieldName1.SetFocus

but for some reason, I have followed the code [F8] and it does execute, exits the sub, but the pointer is never placed on FieldName1 is actually who knows where.
I have to Click on either SF1 or SF2, if I click after SAVE on SF2/FieldName1 it does work there is no problem, no errors.
Except that the SetFocus instruction is nowhere, it does execute as I said.
Maybe I am guessing I am missing something, I have never worked with sub forms that are Continuous forms in this way, so this is new experience, and I am learning a lot.
This is not a DO or DIE situation, if it works great, if it doesn't, oh well, no one will ever miss it, as a learning tool it will be great to know the why.

Thanks
 

Users who are viewing this thread

Top Bottom