Solved Can I go directly to the module (1 Viewer)

Superpat

Member
Local time
Today, 18:28
Joined
Aug 15, 2020
Messages
96
Hello,
I have this code who is good, but I don't know how to modify to go directy to my form (strNomModule) and modify this "For Each oComponent In Application.VBE.ActiveVBProject.VBComponents"


Code:
Function RechercheIntituleModForm(ByVal sSearchTerm As String, strNomModule As String)
        Dim oComponent As Object                                     'VBComponent
        For Each oComponent In Application.VBE.ActiveVBProject.VBComponents
                Debug.Print "Module IntituleModForm : " & oComponent.CodeModule
                If oComponent.CodeModule = strNomModule Then
                        If oComponent.CodeModule.Find(sSearchTerm, 1, 1, -1, -1, False, False, False) = True Then
                                Debug.Print "Module contient et vrai : " & oComponent.Name        'Name of the current module in which the term was found (at least once)
                                RechercheIntituleModForm = True
                        Else
                                Debug.Print "Module ne contient pas : " & oComponent.Name        'Name of the current module in which the term was not found (at least once)
                                RechercheIntituleModForm = False
                        End If
                        Exit For                                     'We know it exist so let leave, no point continuing
                End If
         Next oComponent
End Function
Thank to help
 

moke123

AWF VIP
Local time
Today, 12:28
Joined
Jan 11, 2013
Messages
3,920
just a guess as I cant test, can you use the index?

Code:
For Each oComponent In Application.VBE.ActiveVBProject.VBComponents(strNomModule)
 

Superpat

Member
Local time
Today, 18:28
Joined
Aug 15, 2020
Messages
96
just a guess as I cant test, can you use the index?

Code:
For Each oComponent In Application.VBE.ActiveVBProject.VBComponents(strNomModule)
Thanks, but it do not work. Error 438
2023-02-08_154028.png
 

isladogs

MVP / VIP
Local time
Today, 17:28
Joined
Jan 14, 2017
Messages
18,227
Do you have the Visual Basic Extensibility reference assigned in your project?
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:28
Joined
Sep 21, 2011
Messages
14,308
Can you have an index by the name of the form? :unsure:
 

isladogs

MVP / VIP
Local time
Today, 17:28
Joined
Jan 14, 2017
Messages
18,227
It works fine for me.
I made a couple of cosmetic changes for testing purposes:

Code:
Function RechercheIntituleModForm(ByVal sSearchTerm As String, strNomModule As String)
        Dim oComponent As Object                                     'VBComponent
        For Each oComponent In Application.VBE.ActiveVBProject.VBComponents
              '  Debug.Print "Module IntituleModForm : " & oComponent.CodeModule
                If oComponent.CodeModule = strNomModule Then
                        Debug.Print "Module IntituleModForm : " & oComponent.CodeModule
                        If oComponent.CodeModule.Find(sSearchTerm, 1, 1, -1, -1, False, False, False) = True Then
                                Debug.Print "Module contient et vrai : " & oComponent.Name & " : " & sSearchTerm         'Name of the current module in which the term was found (at least once)
                                RechercheIntituleModForm = True
                        Else
                                Debug.Print "Module ne contient pas : " & oComponent.Name & " : " & sSearchTerm        'Name of the current module in which the term was not found (at least once)
                                RechercheIntituleModForm = False
                        End If
                        Debug.Print ""
                        Exit For                                     'We know it exist so let leave, no point continuing
                End If
         Next oComponent
End Function

Then tested it using

Code:
Sub TestSearch()
    RechercheIntituleModForm "ViewCode", "Form_frmMain"
    RechercheIntituleModForm "XKPWQ", "Form_frmMain"
End Sub

NOTE; you must use the name of the form code module "Form_YourFormName"

Results:
Code:
Module IntituleModForm : Form_frmMain
Module contient et vrai : Form_frmMain : ViewCode

Module IntituleModForm : Form_frmMain
Module ne contient pas : Form_frmMain : XKPWQ
 

Superpat

Member
Local time
Today, 18:28
Joined
Aug 15, 2020
Messages
96
Can you have an index by the name of the form? :unsure:
I do not know how to do, I have just the name of the form strNomModule. However, the loop works well, but reviews all the forms.
 

Josef P.

Well-known member
Local time
Today, 18:28
Joined
Feb 2, 2023
Messages
826
Hi,
Are you looking for this:
Code:
set oComponent = Application.VBE.ActiveVBProject.VBComponents(strNomModule)
 

isladogs

MVP / VIP
Local time
Today, 17:28
Joined
Jan 14, 2017
Messages
18,227
Ah I missed that point...
That simplifies the code significantly...

Code:
Function RechercheIntituleModForm(ByVal sSearchTerm As String, strNomModule As String)
        Dim oComponent As Object                                     'VBComponent
        
        Set oComponent = Application.VBE.ActiveVBProject.VBComponents(strNomModule)
    
        If oComponent.CodeModule.Find(sSearchTerm, 1, 1, -1, -1, False, False, False) = True Then
                Debug.Print "Module contient et vrai : " & oComponent.Name & " : " & sSearchTerm         'Name of the current module in which the term was found (at least once)
        Else
                Debug.Print "Module ne contient pas : " & oComponent.Name & " : " & sSearchTerm        'Name of the current module in which the term was not found (at least once)
        End If
End Function
 

Superpat

Member
Local time
Today, 18:28
Joined
Aug 15, 2020
Messages
96
Sorry,I have a little problem more. I have a form with no module.
In errSub, I put err=9, but it is not very elegant, can we treat this case otherwise or precisely treating nothing at all
Code:
Function RechercheIntituleModForm(ByVal sSearchTerm As String, strNomModule As String)
        Dim oComponent As Object                                     'VBComponent
        On Error GoTo errSub
        Set oComponent = Application.VBE.ActiveVBProject.VBComponents(strNomModule)
        Debug.Print "Module IntituleModForm : " & oComponent.CodeModule
        If oComponent.CodeModule = strNomModule Then
                If oComponent.CodeModule.Find(sSearchTerm, 1, 1, -1, -1, False, False, False) = True Then
                        Debug.Print "Module contient et vrai : " & oComponent.Name
                        RechercheIntituleModForm = True
                Else
                        Debug.Print "Module ne contient pas : " & oComponent.Name
                        RechercheIntituleModForm = False
                End If
        End If
Exitsub:
        On Error GoTo 0
        Exit Function
errSub:
        If err = 9 Then
                RechercheIntituleModForm = True
                On Error GoTo 0
                Exit Function
        End If
End Function
 

Josef P.

Well-known member
Local time
Today, 18:28
Joined
Feb 2, 2023
Messages
826
Do you want to find out whether the appropriate code module is available in ActiveVBProject.VBComponents? => Loop over VBComponents (as it can already be seen above).
Or do you want to check whether the form has a code module before checking the text in the code module? => FormReference.HasModule
From somewhere RechercheIntituleModForm must get the module name. In this procedure you could check in advance if the form has a code module.
 

Superpat

Member
Local time
Today, 18:28
Joined
Aug 15, 2020
Messages
96
Before, in this module :
Code:
Function fnDmwListAllFormulairesDeroulantes(lComptageFormulaires As String) As String
        ' Liste les formulaires dans le fichier "t_ListeDeroulanteFormulaires
        On Error GoTo ErrHandler
        Dim bolExiste As Boolean
        Dim Table_Name As String
        Dim oRst    As DAO.Recordset
        Dim oDb     As DAO.Database
        Dim frm     As Object
        Set oDb = CurrentDb
        Table_Name = "t_ListeDeroulanteFormulaires"
        Set oRst = oDb.OpenRecordset(Table_Name, dbOpenDynaset)
        For Each frm In CurrentProject.AllForms
                Debug.Print "CurrentProject.AllForms : " & frm.Name
'                If frm.Name = "FormulaireVide" Then
'                        Stop
'                End If
                With oRst
                        bolExiste = RechercheIntituleModForm(strSearch, "Form_" & frm.Name)
                        If bolExiste = True Then
                                Debug.Print "Existe form : " & frm.Name
                        Else
                                Debug.Print "Existe pas form : " & frm.Name
                                .AddNew
                                ![Liste_Formulaires] = frm.Name
                                .Update
                        End If
                End With
        Next frm
        oRst.Close
        oDb.Close
        Set oRst = Nothing
        Set oDb = Nothing
        Dim strSQLComptageOperation As Long
        lComptageFormulaires = DCount("Liste_Formulaires", Table_Name)
        fnDmwListAllFormulairesDeroulantes = lComptageFormulaires
procDone:
        Exit Function
ErrHandler:
        MsgBox err.Number & " " & err.Description
        Resume procDone
End Function
 

Josef P.

Well-known member
Local time
Today, 18:28
Joined
Feb 2, 2023
Messages
826
If a form does not contain a code module, should it be stored in the table?
 

Josef P.

Well-known member
Local time
Today, 18:28
Joined
Feb 2, 2023
Messages
826
Ok, then just an idea:
You only want to check the code modules of forms.

Code:
Function fnDmwListAllFormulairesDeroulantes(lComptageFormulaires As String) As String

    ...
    
    Set oDb = CurrentDb
    Table_Name = "t_ListeDeroulanteFormulaires"
    Set oRst = oDb.OpenRecordset(Table_Name, dbOpenDynaset)
    
    Dim vbc As VBComponent
    For Each vbc In GetAllFormVBComponents
        'bolExiste = RechercheIntituleModForm(strSearch, vbc) ' Interface changed form string to VBComponent (Object)
        'or
        bolExiste = RechercheIntituleModForm(strSearch, vbc.Name)
        If bolExiste = True Then
            Debug.Print "Existe form : " & frm.Name
        Else
            Debug.Print "Existe pas form : " & frm.Name
            .AddNew
            ![Liste_Formulaires] = frm.Name
            .Update
        End If
    Next
    
    ...
  
End Function

Private Function GetAllFormVBComponents() As Collection

    Dim col As Collection
    Dim vbc As VBComponent
    
    Set col = New Collection
    
    For Each vbc In Application.VBE.ActiveVBProject.VBComponents
        If Left(vbc.Name, 5) = "Form_" Then
            col.Add vbc, vbc.Name
        End If
    Next
    
    Set GetAllFormVBComponents = col

End Function
 

isladogs

MVP / VIP
Local time
Today, 17:28
Joined
Jan 14, 2017
Messages
18,227
Another possible approach would be to use the Boolean HasModule property
You will need to open the form(s) in design view in order to check that
Something like

Code:
Docmd.OpenForm "FormName",acDesign,,,,acHidden

If Forms!FormName.HasModule = False Then Exit Function
 

Users who are viewing this thread

Top Bottom