Solved Function that deactivates my toolbar (1 Viewer)

Superpat

Member
Local time
Today, 18:03
Joined
Aug 15, 2020
Messages
113
Hello,
I created a toolbar in my VBA editor, it has 3 buttons, including one to send an MSGBOX box (Coucou) which works from any module.
Another button to insert a module header when I click on this button, the function runs, does its job, but after the buttons are disabled!
I suspect the "inserenttetetetitremodule" of short-circuited my toolbar, could you help me ?
To start, the function : BrandNewBarAndButton
 

Attachments

I've just done a quick test but without studying your code.
All three buttons create a message box and the header comment section is created with the middle button whose FaceID I modified

1737065457329.png


The toolbar buttons don't get disabled in my tests
 
No errors here either. However, the buttons are disabled when you click the middle button, Ajouter en tête de module.

Rerunning the code that instantiates it originally reenables it, though. Focus on the code in Sub InsererEnTeteTitreModule() for trouble-shooting
 
Strange how the buttons aren't disabled for me after clicking the middle button
I'm running A365 version 2502 build 18502.20000 64-bit (Beta Channel) in Windows 10 22H2
 
Strange how the buttons aren't disabled for me after clicking the middle button
I'm running A365 version 2502 build 18502.20000 64-bit (Beta Channel) in Windows 10 22H2
I'm on 32 bit, but that shouldn't matter.
 
I changed this section of the initialization code. The command bar is no longer disabled.

Code:
 ...
        Set myControl = myBar.Controls.Add(msoControlButton, , , 2)
        With myControl
                .caption = "Ajouter entête module"
                .DescriptionText = "Ajouter en tête de module"
                .Style = msoButtonIconAndCaption
                .FaceId = 558
'                .OnAction = "InsererTeteTitreModule"
                .OnAction = "InsererEnTeteTitreModule" ' Direct call to the sub
        End With
   ...
 
I changed this section of the initialization code. The command bar is no longer disabled.

Code:
 ...
        Set myControl = myBar.Controls.Add(msoControlButton, , , 2)
        With myControl
                .caption = "Ajouter entête module"
                .DescriptionText = "Ajouter en tête de module"
                .Style = msoButtonIconAndCaption
                .FaceId = 558
'                .OnAction = "InsererTeteTitreModule"
                .OnAction = "InsererEnTeteTitreModule" ' Direct call to the sub
        End With
   ...
Correction, it is not always disabled. There are times when it is disabled.
 
Thanks for your reply :)
For me, it deactivates every time, even with the modification. Curious !
 
well, maybe you can call BrandNewBarAndButton to rebuild your button after each call to your OnAction sub, eg:

Code:
Sub InsererTeteTitreModule()
        Call InsererEnTeteTitreModule
        BrandNewBarAndButton
        'Debug.Print "InsererEnTeteTitreModule appelé"
End Sub
 
No, It does'not !
Curious, curious...
I try also that :

Code:
        ' Insérer l'en-tête au début du module
        CodeMod.InsertLines 1, strHeader

        MsgBox "En-tête inséré avec succès dans le module '" & vbComp.Name & "'.", vbInformation, "Succès"
'Add
BrandNewBarAndButton
End Sub
But nothing, I try also to put a new Public Sub, but it's the same problem, I do the first time and not the second.
I've found that if, in the function "InsertionModificationModule", I disable the line:

Code:
 ' Insert header at the beginning of the module
        'CodeMod.InsertLines 1, strHeader

Nothing happens, but the menus continue to work
 
Last edited:
No, It does'not !
Curious, curious...
I try also that :

Code:
        ' Insérer l'en-tête au début du module
        CodeMod.InsertLines 1, strHeader

        MsgBox "En-tête inséré avec succès dans le module '" & vbComp.Name & "'.", vbInformation, "Succès"
'Add
BrandNewBarAndButton
End Sub
But nothing, I try also to put a new Public Sub, but it's the same problem, I do the first time and not the second.
I've found that if, in the function "InsertionModificationModule", I disable the line:

Code:
 ' Insert header at the beginning of the module
        'CodeMod.InsertLines 1, strHeader

Nothing happens, but the menus continue to work
That's what I observed as well.

That indicates the problem is caused by modifying the code module. That seems to leave the commandbar disabled. It does not impact other command bars, though.
 
This feels kludgy, but try it. The code goes to a "cleanup" step that regenerates the command bar, regardless of whether the header is actually inserted.

I can't say why the command bar and the buttons on it are disabled because of the insertion of the header into a module, but regenerating a new command bar bypasses that. I'd rather find a cleaner method, but what works, works.


Code:
Public Sub InsererEnTeteTitreModule()
Dim vbComp As VBComponent
Dim CodeMod As CodeModule
Dim strHeader As String
Dim versionParDefaut As String
Dim i As Long
Dim lineContent As String
Dim headerExists As Boolean

    ' Définir la version par défaut
    versionParDefaut = "1.0"

    ' Sélectionner le module courant
    Set vbComp = Application.VBE.SelectedVBComponent
    If vbComp Is Nothing Then
        MsgBox "Aucun module sélectionné.", vbExclamation
        GoTo Cleanup
        '        Exit Sub
    End If
    Set CodeMod = vbComp.CodeModule

    ' Vérifier si l'en-tête existe déjà
    headerExists = False
    For i = 1 To IIf(CodeMod.CountOfLines > 10, 10, CodeMod.CountOfLines)
        lineContent = CodeMod.Lines(i, 1)
        If InStr(1, lineContent, "' Créé par       : SuperPat", vbTextCompare) > 0 Then
            headerExists = True
            Exit For
        End If
    Next i

    If headerExists Then
        MsgBox "Un en-tête existe déjà dans ce module.", vbInformation, "En-tête déjà présent"
        GoTo Cleanup
        '        Exit Sub
    End If

    ' Créer l'en-tête
    strHeader = "'---------------------------------------------------------------------------------------" & vbNewLine & _
        "' Module         : " & vbComp.Name & vbNewLine & _
        "' But            :" & vbNewLine & _
        "' Créé par       : SuperPat" & vbNewLine & _
        "' Le             : " & Format(Date, "dd/mm/yyyy") & vbNewLine & _
        "' Version        : " & versionParDefaut & vbNewLine & _
        "' But            :" & vbNewLine & _
        "'---------------------------------------------------------------------------------------" & vbNewLine

    '     Insérer l 'en-tête au début du module
    CodeMod.InsertLines 1, strHeader
 
    MsgBox "En-tête inséré avec succès dans le module '" & vbComp.Name & "'.", vbInformation, "Succès"
Cleanup:
    BrandNewBarAndButton
    Exit Sub
End Sub
 
Thanks @GPGeorge ,
I try it before and with your "Cleanup", but that don't works. And I saw my message !
Code:
Cleanup:
        BrandNewBarAndButton
        Debug.Print "passé sur Cleanup InsererEnTeteTitreModule"
        Exit Sub
End Sub
I have windows 11 Access version 16-18324.
 
As soon as the mcolBarEvents collection disappears from the memory, the system no longer listens to the button click.

Module: InsertionBarreOutils
Code:
...
' Dans un module standard (par exemple, Module1)
Public mcolBarEvents As Collection                                                                 'collection to store menu item click event handlers

' add and run for test:
Public Sub DisableEvents()
    Set mcolBarEvents = Nothing
End Sub

Public Sub DisableEventsV2()
    End
End Sub
...
 
Last edited:
Its still works perfectly for me so cannot make any sensible suggestions on why you & George get different results
Good luck solving it. . .
 
As soon as the mcolBarEvents collection disappears from the memory, the system no longer listens to the button click.

Module: InsertionBarreOutils
Code:
...
' Dans un module standard (par exemple, Module1)
Public mcolBarEvents As Collection                                                                 'collection to store menu item click event handlers

' add and run for test:
Public Sub DisableEvents()
    Set mcolBarEvents = Nothing
End Sub

Public Sub DisableEventsV2()
    End
End Sub
...
The goal is to NOT remove the collection from memory after inserting a header into a module.

Is there a way to prevent removing the collection ?
 
Thanks @GPGeorge ,
I try it before and with your "Cleanup", but that don't works. And I saw my message !
Code:
Cleanup:
        BrandNewBarAndButton
        Debug.Print "passé sur Cleanup InsererEnTeteTitreModule"
        Exit Sub
End Sub
I have windows 11 Access version 16-18324.
Now we have even more varied results. Colin doesn't have the problem at all; it goes away for me with this kludge; nothing works for you.
 
You could call the code for adding the modules from an add-in. Then there are 2 different VBA projects.
Now we have even more varied results. Colin doesn't have the problem at all; it goes away for me with this kludge; nothing works for you.
Correction. The problem goes away sometimes, with this kludge. If I put a breakpoint somewhere and step through the code, the commandbar event collection is not removed and it continues to work.
 
You could call the code for adding the modules from an add-in. Then there are 2 different VBA projects.
It work perfectly for me, thanks you very much.
What is the point of making an add-in (I have never made one) ?
Could we do everything in the same project
 

Users who are viewing this thread

Back
Top Bottom