Solved Code in subform which does not behave as in a form (1 Viewer)

Superpat

Member
Local time
Today, 19:26
Joined
Aug 15, 2020
Messages
96
Hello and Happy Holidays.
I have an error 2516 telling me : Access cannot find the module "Form_formulaire2".
The code should not be correct, but I don't know how to correct it.
You have to click on Error in Form 2, then Déboguer. The error is on line 55.
Precision, the subform is not a subform, but a form.
Code:
Function RechercherProcedure(sNomProcedure As String, sModuleEnCours As String, sErl As Long, Optional sFormulaire_en_cours As String)
            'Error in next line
55          DoCmd.OpenModule sModuleEnCours, sNomProcedure
End Function
Thanks for help
 

Attachments

  • essai_01.accdb
    640 KB · Views: 48

Ranman256

Well-known member
Local time
Today, 13:26
Joined
Apr 9, 2015
Messages
4,337
if the sub is in the form, and you call it from the form, you can just use: formulaire2
RechercherProcedure () not needed

if you are calling formulaire2 from outside the the form, then make the procedure public:
PUBLIC sub formulaire2()
end sub

then you can call it from anywhere using the full path:
forms!fMyForm.formulaire2
 

mike60smart

Registered User.
Local time
Today, 18:26
Joined
Aug 6, 2017
Messages
1,905
What exactly should happen when you click on Formulaire2 ??
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:26
Joined
Feb 28, 2001
Messages
27,188
Ranman256's proposed solution is likely to be correct. Perhaps I should explain why.

When you have a form/sub-form situation, you have TWO FORMS. The sub-form is just a another form having its own context and its own form-class module. It is a complete form within itself (even though it might not function correctly when opened independently). The parent form has a sub-form control that acts as the go-between for the two forms to synchronize them (where appropriate). But if you are running code in the parent form, things in the sub-form are in another form. The default for elements / components of forms is that they are considered private to the outside world... unless you wish to share them with that world outside of their own private little class module. Which you do using the Public declaration.

However, there are other viewpoints on this sharing, such as moving things like code routines to general modules that can be shared by all callers. It isn't quite so simple as that, but in overview that is one approach used to make code from one form available to other forms.
 

Superpat

Member
Local time
Today, 19:26
Joined
Aug 15, 2020
Messages
96
Thanks for yours answers.
What exactly should happen when you click on Formulaire2 ??
I want to open the form (Form_Formulaire2) or the module (mdlGestionErreurs) where I made the error .
2023-12-28_163600.jpg

if the sub is in the form, and you call it from the form, you can just use: formulaire2
RechercherProcedure () not needed

if you are calling formulaire2 from outside the the form, then make the procedure public:
PUBLIC sub formulaire2()
end sub

then you can call it from anywhere using the full path:
forms!fMyForm.formulaire2
I try it in the window execute a lot of code
Code:
DoCmd.OpenModule "form_formulaire1","Commande0_Click"
it does
Code:
DoCmd.OpenModule Forms![formulaire1]![formulaire2]
DoCmd.OpenModule Forms![Formulaire1].Form.[Formulaire2]
it does not

Sorry, I can not does it.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:26
Joined
Feb 28, 2001
Messages
27,188
The documentation for DoCmd.OpenModule tells you that you must provide strings because OpenModule works from NAMES.


Code:
DoCmd.OpenModule "form_formulaire1","Commande0_Click"

(which you say works) provides names.

Code:
DoCmd.OpenModule Forms![formulaire1]![formulaire2]
DoCmd.OpenModule Forms![Formulaire1].Form.[Formulaire2]

(which you say doesn't work) doesn't provide names. Those two failing references are OBJECT references, not names.
 

Superpat

Member
Local time
Today, 19:26
Joined
Aug 15, 2020
Messages
96
The documentation for DoCmd.OpenModule tells you that you must provide strings because OpenModule works from NAMES.
However,
Code:
DoCmd.OpenModule "form_formulaire2","Commande2_Click"
doesn't work.
I cannot open a code in a subform with DoCmd.OpenModule ?
 

GPGeorge

Grover Park George
Local time
Today, 10:26
Joined
Nov 25, 2004
Messages
1,873
"doesn't work."?

What DOES happen instead?
 

Superpat

Member
Local time
Today, 19:26
Joined
Aug 15, 2020
Messages
96
"doesn't work."?

What DOES happen instead?
I have Error 2516. (see #5)
Code:
DoCmd.OpenModule "","sousForm1"
DoCmd.OpenModule "form_formulaire1","Commande0_Click"
DoCmd.OpenModule "form_formulaire1","essai32"
DoCmd.OpenModule "","essai33"
DoCmd.OpenModule "Pat_Mod_MsgBox2","MsgBoxTitleBtnProc"
DoCmd.OpenModule "","essai32"
DoCmd.OpenModule "form_formulaire1","Commande8_Click"
All that code is correct with modules or Formulaire1
Code:
DoCmd.OpenModule "form_formulaire2","Commande2_Click"
DoCmd.OpenModule "","essai31"
This two codes are not correct, they are in Formulaire2
Now I try to find another solution for Subform Formulaire2
 

Attachments

  • essai_02.accdb
    768 KB · Views: 42

Superpat

Member
Local time
Today, 19:26
Joined
Aug 15, 2020
Messages
96
I found :
Code:
30        sFrmName = Screen.ActiveControl.Parent.Name
40        Debug.Print sFrmName
50        If Nz(sFrmName, "") = "" Then Exit Function

          Dim sSsForm As String
60        Call sousForm2(sSsForm)
70        Debug.Print sSsForm
80        If Not IsNull(sSsForm) And Left(sModuleEnCours, 5) = "Form_" Then
90            Application.VBE.ActiveVBProject.VBComponents("Form_" & sFrmName).CodeModule.CodePane.Show
Code:
Function sousForm2(Optional sSsForm As String) As String
      'Récupère les noms des formulaires, sous-formulaires, encadrement
      Dim strFormName As String
      Dim frmCurrentForm As Form
      Dim C As Control
      Set frmCurrentForm = Screen.ActiveForm
      strFormName = frmCurrentForm.Name
      For Each C In frmCurrentForm.Controls
            If C.ControlType = acSubform Then
'                  Debug.Print "form actif : " & strFormName
'                  Debug.Print "Nom de l'encadrement du sous formulaire : " & C.Name
'                  Debug.Print "Nom sous formulaire : " & C.SourceObject
                  sSsForm = C.SourceObject
            End If
      Next C
      sousForm2 = sSsForm
End Function
Now, it's all right for me :)
 

Josef P.

Well-known member
Local time
Today, 19:26
Joined
Feb 2, 2023
Messages
826
I was about to suggest the way via VBE.ActiveVBProject.VBComponents, but you were quicker ;)
Note: but be careful with ActiveVBProject if you use Access (accdb, accde) references or add-ins.

Another idea to get to the error location:
Code:
Select Case fGestError2(strNomProcedure, sModuleEnCours, Formulaire_en_cours, Err, Erl, Err.Description)
            Case vbAbort
                On Error GoTo 0
                Resume
 

Superpat

Member
Local time
Today, 19:26
Joined
Aug 15, 2020
Messages
96
I send my last form, it's all right for me.
Thanks for yours answers... and Happy new year :)
 

Attachments

  • essai_05.accdb
    488 KB · Views: 43

Users who are viewing this thread

Top Bottom