Appeler une methode d'une autre autre userform

Fermé
Marvin.R Messages postés 15 Date d'inscription lundi 29 octobre 2012 Statut Membre Dernière intervention 8 novembre 2012 - 7 nov. 2012 à 18:31
michel_m Messages postés 16603 Date d'inscription lundi 12 septembre 2005 Statut Contributeur Dernière intervention 16 décembre 2023 - 9 nov. 2012 à 08:31
Bonjour,

Est-il possible d'appeler une méthode (public sub...) qui se situe dans userform1, depuis userform2 ?

3 réponses

Heliotte Messages postés 1491 Date d'inscription vendredi 26 octobre 2012 Statut Membre Dernière intervention 28 janvier 2013 92
Modifié par Heliotte le 7/11/2012 à 18:44
Bonsoir Marvin.R,

Logiquement, c'est possible:
Call userform2.MethodeDeUserform1(Paramètre1,Paramètre2,Paramètren)
Si c'est une fonction:
Retour = userform2.MethodeDeUserform1(Paramètre1,Paramètre2,Paramètren)
0
Marvin.R Messages postés 15 Date d'inscription lundi 29 octobre 2012 Statut Membre Dernière intervention 8 novembre 2012
7 nov. 2012 à 19:49
Bonsoir Heliotte,

voici la déclaration de ma méthode :

Public Sub decisionTutoriel(choix As Boolean)

Maintenant, l'instruction sur lequel le compilateur plante :

Call UserForm1.decisionTutoriel(True)
0
Heliotte Messages postés 1491 Date d'inscription vendredi 26 octobre 2012 Statut Membre Dernière intervention 28 janvier 2013 92
Modifié par Heliotte le 7/11/2012 à 20:26
Peut-être une erreur de syntaxe : "userform1" et "UserForm1" ? mais j'en doute .. à voir !
Tu travaille dans quel version de Visual Basic ?
0
Marvin.R Messages postés 15 Date d'inscription lundi 29 octobre 2012 Statut Membre Dernière intervention 8 novembre 2012
7 nov. 2012 à 20:27
Comment le savoir ?
0
Heliotte Messages postés 1491 Date d'inscription vendredi 26 octobre 2012 Statut Membre Dernière intervention 28 janvier 2013 92
7 nov. 2012 à 20:54
Dans ta question originale, tu mets "userform1" et "userform2" en minuscule, et dans ton pos d'après les initiales sont en majuscule.
Pour en avoir le coeur net, au début de ta feuille de code, il faut mettre "Option Explicit". Ces mots signifie que si tu utilise une variable non déclarée, le programme te signalera une erreur. Essaie et tiens-moi au courant.
Si cela ne marche toujours pas, pense à mettre ton code ici : "www.CiJoint.com" en prenant la précaution de supprimer ou de remplacer toutes données sensibles bien sûr ! Tu mets l'adresse (le lien) dans ton prochain msg.
0
Marvin.R Messages postés 15 Date d'inscription lundi 29 octobre 2012 Statut Membre Dernière intervention 8 novembre 2012
8 nov. 2012 à 06:36
Bonjours,

Le problème n'est toujours pas résolu. Je ne sais pas comment utiliser le site indiqué.

Voici les codes sources :

UserForm1

Option Explicit

Dim choixCharge As Boolean, choixMateriaux As Boolean
Dim tableauTbSection(9) As String
Dim charge As Integer, section As Integer, i As Integer, j As Integer, k As Integer

Public Sub decisionTutoriel(choix As Boolean)
    For i = 1 To 3
        Me("charge" & i).Enabled = choix
        Next i
    For j = 1 To 5
        Me("charge" & j).Enabled = choix
        Next j
    For k = 1 To 8
        Me("tbsection" & k).Enabled = choix
        Next k
    cbSection.Enabled = choix
    
    If choix = False Then
        relireTutoriel.Visible = True
    Else
        relireTutoriel.Visible = False
    End If
End Sub

Private Sub UserForm_Initialize()
    UserForm2.Show
End Sub

' Type de charge

Private Sub charge1_Click()
    Call selectionnerCharge(1)
    If section <> 0 Then
        Call appelerCalculerTensionInertie
    End If
End Sub
Private Sub charge2_Click()
    Call selectionnerCharge(2)
    If section <> 0 Then
        Call appelerCalculerTensionInertie
    End If
End Sub
Private Sub charge3_Click()
    Call selectionnerCharge(3)
    If section <> 0 Then
        Call appelerCalculerTensionInertie
    End If
End Sub

' Relire le tutoriel

Private Sub relireTutoriel_Click()
    UserForm2.Show
End Sub

' Type de Section

Private Sub section1_Click()
    Call afficherTbSection(1, 1, 2)
    section = 1
End Sub
Private Sub section2_Click()
    Call afficherTbSection(2, 3, 0)
    section = 2
End Sub
Private Sub section3_Click()
    Call afficherTbSection(3, 4, 5)
    section = 3
End Sub
Private Sub section4_Click()
    Call afficherTbSection(4, 6, 0)
    section = 4
End Sub
Private Sub section5_Click()
    Call afficherTbSection(5, 7, 8)
    section = 5
End Sub

' tbSections

Private Sub tbSection1_Enter()
    Call modifierTbSection(1)
End Sub
Private Sub tbSection2_Enter()
    Call modifierTbSection(2)
End Sub
Private Sub tbSection3_Enter()
    Call modifierTbSection(3)
End Sub
Private Sub tbSection4_Enter()
    Call modifierTbSection(4)
End Sub
Private Sub tbSection5_Enter()
    Call modifierTbSection(5)
End Sub
Private Sub tbSection6_Enter()
    Call modifierTbSection(6)
End Sub
Private Sub tbSection7_Enter()
    Call modifierTbSection(7)
End Sub
Private Sub tbSection8_Enter()
    Call modifierTbSection(8)
End Sub

' Type de matériaux

Private Sub cbSection_Change()
    If choixCharge <> False Then
        Call appelerCalculerTensionInertie
    End If
End Sub
Private Sub cbSection_Click()
    Call modifierTbSection(100)
    If choixCharge = False Then
        cbSection.Value = "Quel matériaux..."
        MsgBox "Tu dois avant, choisir le type de charge !"
    Else
        choixMateriaux = True
        cbSection.BackColor = RGB(43, 43, 255)
    End If
End Sub

' tbSection_KeyUp
Private Sub tbSection1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call calculerTensionInertie(1, 1, 2)
End Sub
Private Sub tbSection2_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call calculerTensionInertie(1, 1, 2)
End Sub
Private Sub tbSection3_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call calculerTensionInertie(2, 3, 0)
End Sub
Private Sub tbSection4_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call calculerTensionInertie(3, 4, 5)
End Sub
Private Sub tbSection5_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call calculerTensionInertie(3, 4, 5)
End Sub
Private Sub tbSection6_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call calculerTensionInertie(4, 6, 0)
End Sub
Private Sub tbSection7_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call calculerTensionInertie(5, 7, 8)
End Sub
Private Sub tbSection8_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Call calculerTensionInertie(5, 7, 8)
End Sub

' Fonctions appelées

Private Sub réinitialiserBoutonCharge()
    For i = 1 To 3
        Me("charge" & i).BackColor = RGB(255, 255, 255)
        Next i
    choixCharge = True
End Sub
Private Sub selectionnerCharge(choix As Integer)
    cbSection.BackColor = RGB(255, 255, 255)
    Call modifierTbSection(0)
    Call réinitialiserBoutonCharge
    Me("charge" & choix).BackColor = RGB(128, 0, 255)
    charge = choix + 1
End Sub
Private Sub réinitialiserBoutonSection()
    For i = 1 To 5
        Me("section" & i).BackColor = RGB(255, 255, 255)
        Next i
End Sub
Private Sub réinitialiserTbSection()
    tableauTbSection(1) = "b"
    tableauTbSection(2) = "h"
    tableauTbSection(3) = "d"
    tableauTbSection(4) = "d"
    tableauTbSection(5) = "D"
    tableauTbSection(6) = "s"
    tableauTbSection(7) = "a"
    tableauTbSection(8) = "b"
    
    For i = 1 To 8
        Me("tbSection" & i).Visible = False
        Me("tbsection" & i).Value = tableauTbSection(i)
        Me("tbsection" & i).BackColor = RGB(255, 255, 255)
        Next i
End Sub
Private Sub afficherTbSection(choix As Integer, parametre1 As Integer, parametre2 As Integer)
    If choixCharge = True And choixMateriaux = True Then
        cbSection.BackColor = RGB(255, 255, 255)
        Call cacherMomentFlexion
        Call réinitialiserBoutonSection
        Call réinitialiserTbSection
        Me("section" & choix).BackColor = RGB(128, 0, 255)
        If choix Mod 2 <> 0 Then
            Me("tbSection" & parametre1).Visible = True
            Me("tbSection" & parametre2).Visible = True
        Else
            Me("tbSection" & parametre1).Visible = True
        End If
    ElseIf (choixCharge = False) Then
        MsgBox "Tu dois avant, choisir le type de charge !"
    Else
        MsgBox "Tu dois choisir le matériaux !"
    End If
End Sub
Private Sub modifierTbSection(choix As Integer)
    For i = 1 To 8
        Me("tbSection" & i).BackColor = RGB(255, 255, 255)
        If IsNumeric(Me("tbsection" & i)) = False Then
            Me("tbsection" & i).Value = tableauTbSection(i)
        End If
        Next i
    If choix <> 0 And choix <> 100 Then
        Me("tbsection" & choix).BackColor = RGB(43, 43, 255)
        Me("tbsection" & choix).Value = ""
    End If
    If choix <> 100 Then
        cbSection.BackColor = RGB(255, 255, 255)
    End If
End Sub
Private Sub calculerTensionInertie(choix As Integer, parametre1 As Integer, parametre2 As Integer)
    Dim tableau(3) As Integer, j As Integer
    
    If choix Mod 2 <> 0 Then
        For i = 1 To 2
            If i = 1 Then
                j = parametre1
            Else
                j = parametre2
            End If
            If IsNumeric(Me("tbSection" & j)) = True And Me("tbSection" & j) > 0 Then
                tableau(i) = 1
            Else
                tableau(i) = 0
            End If
            Next i
    Else
        If IsNumeric(Me("tbSection" & parametre1)) = True And Me("tbSection" & parametre1) > 0 Then
            tableau(1) = 1
        Else
            tableau(1) = 0
        End If
        tableau(2) = 1
    End If
    If cbSection.Value <> "Quel matériaux..." Then
        Dim moment As Integer
        moment = cbSection.Column(charge)
        tableau(0) = 1
    Else
        tableau(0) = 0
    End If
    
    If tableau(0) = 1 And tableau(1) = 1 And tableau(2) = 1 Then
        Call afficherMomentFlexion
        momentInertie.BackColor = RGB(255, 128, 64)
        tensionFlexion.BackColor = RGB(255, 128, 64)
        Select Case choix
            Case Is = 1
                momentInertie.Caption = "Moment d'inertie : " & (tbSection1 * tbSection2 ^ 3) / 12
                tensionFlexion.Caption = "Tension de flexion : " & (6 * moment) / (tbSection1 * tbSection2 ^ 2)
            Case Is = 2
                momentInertie.Caption = "Moment d'inertie : " & (3.14 * tbSection3 ^ 4) / 64
                tensionFlexion.Caption = "Tension de flexion : " & (10 * moment) / tbSection3 ^ 3
            Case Is = 3
                momentInertie.Caption = "Moment d'inertie : " & (3.14 / 64) * (tbSection5 ^ 4 - tbSection4 ^ 4)
                tensionFlexion.Caption = "Tension de flexion : " & (10 * moment * tbSection5) / (tbSection5 ^ 4 - tbSection4 ^ 4)
            Case Is = 4
                momentInertie.Caption = "Moment d'inertie : " & (5 * 1.73 * tbSection6 ^ 2) / 144
                tensionFlexion.Caption = "Tension de flexion : " & (24 * 1.73 * moment) / (5 * tbSection6 ^ 3)
            Case Is = 5
                momentInertie.Caption = "Moment d'inertie : " & (3.14 * tbSection7 ^ 3 * tbSection8) / 4
                tensionFlexion.Caption = "Tension de flexion : " & (4 * moment) / (3.14 * tbSection7 ^ 2 * tbSection8)
        End Select
    End If
End Sub
Private Sub appelerCalculerTensionInertie()
    Select Case section
        Case Is = 1
            Call calculerTensionInertie(1, 1, 2)
        Case Is = 2
            Call calculerTensionInertie(2, 3, 0)
        Case Is = 3
            Call calculerTensionInertie(3, 4, 5)
        Case Is = 4
            Call calculerTensionInertie(4, 6, 0)
        Case Is = 5
            Call calculerTensionInertie(5, 7, 8)
    End Select
End Sub
Private Sub cacherMomentFlexion()
    momentInertie.Visible = False
    tensionFlexion.Visible = False
End Sub
Private Sub afficherMomentFlexion()
    momentInertie.Visible = True
    tensionFlexion.Visible = True
End Sub


UserForm2

Option Explicit

Private Sub non_Click()
    UserForm2.Hide
    Call UserForm1.decisionTutoriel(False)
End Sub

Private Sub oui_Click()
    UserForm2.Hide
    Call UserForm1.decisionTutoriel(True)
End Sub

Private Sub UserForm_Initialize()
    Label1.Caption = "Tutoriel :"
    Label2.Caption = "1°) Choisir le type de charge"
    Label3.Caption = "2°) Choisir le matériaux"
    Label4.Caption = "3°) Selectionner une section"
    Label5.Caption = "4°) Remplir les parametres"
End Sub


Dans UserForm2, lorsque j'appuie sur les boutons "oui" ou "non", un message du compilateur m'indique "Objet spécifié introuvable" en pointant sur l'une des 2 instructions qui appellent la fonction "decisionTutoriel" (dans UserForm1).
0
Heliotte Messages postés 1491 Date d'inscription vendredi 26 octobre 2012 Statut Membre Dernière intervention 28 janvier 2013 92
8 nov. 2012 à 09:03
La ou les fonction(s) appelés à partir de plusieurs endroits différents (ton cas) est à placer dans un module.
Donc, tu insère un module, tu le nomme et tu y met la ou les fonction(s).
Évidemment, il faut supprimer les autres fonctions de même nom (ou au moins les renommer).
Essaie cela et tiens moi au courant !
0
michel_m Messages postés 16603 Date d'inscription lundi 12 septembre 2005 Statut Contributeur Dernière intervention 16 décembre 2023 3 303
8 nov. 2012 à 09:57
Bonjour

tu appelles des sub et non des fonctions
donc

call lasub parametre1,parametre2

lafunction(parametre1,parametre2)

==> pas de parenthèses si tu appelles une sub
et comme déjà indiqué, déclaration des "public" dans un module général

0
Heliotte Messages postés 1491 Date d'inscription vendredi 26 octobre 2012 Statut Membre Dernière intervention 28 janvier 2013 92
8 nov. 2012 à 10:01
Merci michel_m, j'avais pas pensé au
call
et compère.
0
michel_m Messages postés 16603 Date d'inscription lundi 12 septembre 2005 Statut Contributeur Dernière intervention 16 décembre 2023 3 303
8 nov. 2012 à 10:33
Je m'adressais à Marvin :-(

Merci michel_m, j'avais pas pensé au
????
tu ignorais surtout les parenthèses... le Call n'est pas obligatoire dans VBA excel, donc tes pensées...
0
Heliotte Messages postés 1491 Date d'inscription vendredi 26 octobre 2012 Statut Membre Dernière intervention 28 janvier 2013 92
8 nov. 2012 à 10:45
Vous le prenez bien de haut monsieur
Sachez que je connais les parenthèses, mais que je n'y avait pas pensé, c'est tout.
Tant mieux pour vous si vous pensez à tout !
0
Marvin.R Messages postés 15 Date d'inscription lundi 29 octobre 2012 Statut Membre Dernière intervention 8 novembre 2012
8 nov. 2012 à 19:55
Bonsoir,

Lorsque j'enlève les parenthèses, j'ai une erreur dés que je déplace mon curseur :
"Attendu: fin d'instruction"
Call UserForm1.decisionTutoriel False


Comment insère t-on un module ? Un module, ce n'est pas une userform ?
0
michel_m Messages postés 16603 Date d'inscription lundi 12 septembre 2005 Statut Contributeur Dernière intervention 16 décembre 2023 3 303
Modifié par michel_m le 9/11/2012 à 08:44
Bonjour,

Dans l'éditeur VBA (Alt+F11), lorsque tu cliques sur "insertion", apparait un menu avec : userform-module-module de classe

Dans l'option "userform" tu installes les codes des contrôles que tu as installé (textbox, commandbutton....) et les macros propres à cet userform uniquement
tu les appelles par Call (non obligatoire mais permet d'indiquer à la maintenance qu'il s'agit d'une macro et non d'une variable)

Tes macros servent à un autre userform
Ces macros sont alors publiques et doivent impérativement être situées dans un module et déclarées ainsi
Public sub decisionTutoriel()

la syntaxe de l'appel de cette macro dans l'userform2 sera
Call decisionTutoriel False

attention ne confond pas Function et sub(quand tu écris fonctions appellées, il faudrait écrire procédures (ou macros) appellées mais c'est véniel

Function renvoie seulement la valeur d'une variable au nom de la fonction
par ex:
Function coucou() as string
coucou="bonjour"
End funtion

mais ta démarche de constituer des "bibliothèques" de macros ou fonctions réutilisables est la bonne -ne pas réinventer la roue- mais il n'est pas forcément nécessaire d'en coller partout

par exemple lorsque je vois
Private Sub tbSection8_Enter()  
    Call modifierTbSection(8)  
End Sub

je me demande si ca vaut la peine de créer une procédure juste pour une ligne
call modifierTbsection 8
semblerait être suffisant

un tableau récapitulatif sur la portée des variables (source: dvp)
https://www.cjoint.com/?3KjiQMbJL6t



si tu veux joindre ton fichier (mais je n'aurai guère de temps)
mettre le classeur sans données confidentielles en pièce jointe sur
https://www.cjoint.com/
puis copier l'adresse du lien et la coller dans le message de réponse
0