Option Explicit Private mesTxtB(1 To 7) As cTextBox_Date Private Sub UserForm_Initialize() Dim i As Integer Dim List_TextBoxDate List_TextBoxDate = Array("TextBox2", "TextBox5", "TextBox7", "TextBox9", "TextBox11") For i = 1 To 5 Set mesTxtB(i) = New cTextBox_Date mesTxtB(i).Item = Me.Controls(List_TextBoxDate(i - 1)) mesTxtB(i).Index = i Next i Set mesTxtB(6) = New cTextBox_Date mesTxtB(6).Item = Me.Controls("MultiPage1") mesTxtB(6).Index = 6 Set mesTxtB(7) = New cTextBox_Date mesTxtB(7).Item = Me.Controls("Frame1") mesTxtB(7).Index = 7 TextBox1.SetFocus End Sub Private Sub UserForm_Terminate() Dim i As Integer For i = 1 To 7 mesTxtB(i).Clear Next i Erase mesTxtB End Sub
Option Explicit 'Source : http://www.h3.dion.ne.jp/~sakatsu/Breakthrough_P-Ctrl_Arrays_Eng_ref.htm#C2CP Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(0 To 7) As Byte End Type #If VBA7 And Win64 Then Private Declare PtrSafe Function ConnectToConnectionPoint Lib "shlwapi" Alias "#168" _ (ByVal punk As stdole.IUnknown, _ ByRef riidEvent As GUID, _ ByVal fConnect As Long, _ ByVal punkTarget As stdole.IUnknown, _ ByRef pdwCookie As Long, _ Optional ByVal ppcpOut As LongPtr) As Long #Else Private Declare Function ConnectToConnectionPoint Lib "shlwapi" Alias "#168" _ (ByVal punk As stdole.IUnknown, _ ByRef riidEvent As GUID, _ ByVal fConnect As Long, _ ByVal punkTarget As stdole.IUnknown, _ ByRef pdwCookie As Long, _ Optional ByVal ppcpOut As Long) As Long #End If Private Cookie As Long Private MyCtrl As Object Private MyIndex As Integer Private Sub ConnectEvent(ByVal Connect As Boolean) Dim IID_IDispatch As GUID With IID_IDispatch .Data1 = &H20400 .Data4(0) = &HC0 .Data4(7) = &H46 End With ConnectToConnectionPoint Me, IID_IDispatch, Connect, MyCtrl, Cookie, 0& End Sub Public Property Let Index(NewIndex As Integer) MyIndex = NewIndex End Property Public Property Let Item(NewCtrl As Object) Set MyCtrl = NewCtrl Call ConnectEvent(True) End Property Public Sub Clear() If Cookie <> 0 Then Call ConnectEvent(False) Set MyCtrl = Nothing End Sub Public Sub Entrer() If TypeOf MyCtrl Is MsForms.Frame Or TypeOf MyCtrl Is MsForms.MultiPage Then UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [Enter]": Exit Sub UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [Enter] Value=" & MyCtrl.Value End Sub Public Sub Sortie(ByVal Cancel As MsForms.ReturnBoolean) If TypeOf MyCtrl Is MsForms.Frame Or TypeOf MyCtrl Is MsForms.MultiPage Then UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [Exit]": Exit Sub UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [Exit] Value=" & MyCtrl.Value End Sub Public Sub AvantUpdate(ByVal Cancel As MsForms.ReturnBoolean) If TypeOf MyCtrl Is MsForms.Frame Or TypeOf MyCtrl Is MsForms.MultiPage Then UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [BeforeUpdate]": Exit Sub UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [BeforeUpdate] Value=" & MyCtrl.Value End Sub Public Sub ApresUpdate() If TypeOf MyCtrl Is MsForms.Frame Or TypeOf MyCtrl Is MsForms.MultiPage Then UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [AfterUpdate]": Exit Sub UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [AfterUpdate] Value=" & MyCtrl.Value End Sub
Public Sub Entrer() Attribute Entrer.VB_UserMemId = -2147384830 If TypeOf MyCtrl Is MsForms.Frame Or TypeOf MyCtrl Is MsForms.MultiPage Then UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [Enter]": Exit Sub UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [Enter] Value=" & MyCtrl.Value End Sub Public Sub Sortie(ByVal Cancel As MsForms.ReturnBoolean) Attribute Sortie.VB_UserMemId = -2147384829 If TypeOf MyCtrl Is MsForms.Frame Or TypeOf MyCtrl Is MsForms.MultiPage Then UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [Exit]": Exit Sub UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [Exit] Value=" & MyCtrl.Value End Sub Public Sub AvantUpdate(ByVal Cancel As MsForms.ReturnBoolean) Attribute AvantUpdate.VB_UserMemId = -2147384831 If TypeOf MyCtrl Is MsForms.Frame Or TypeOf MyCtrl Is MsForms.MultiPage Then UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [BeforeUpdate]": Exit Sub UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [BeforeUpdate] Value=" & MyCtrl.Value End Sub Public Sub ApresUpdate() Attribute ApresUpdate.VB_UserMemId = -2147384832 If TypeOf MyCtrl Is MsForms.Frame Or TypeOf MyCtrl Is MsForms.MultiPage Then UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [AfterUpdate]": Exit Sub UserForm1.ListBox1.AddItem MyCtrl.Name & "(" & MyIndex & ") [AfterUpdate] Value=" & MyCtrl.Value End Sub
EDIT : je vais dans la semaine en cours tenter de mettre en oeuvre un autre mécanisme beaucoup plus simple et que j' "entrevois" ainsi :
- une textbox "spéciale" unique, utilisée comme tremplin de contrôle de saisie (on pourra y ajouter des contrôles divers (dates, nombres, etc ...)
- superposition systématique de toute textbox présente (en y entrant) par ce tremplin
- utilisation de la propriété tag des textboxes à contrôler pour déterminer le type de contrôle à faire (ou à ne pas faire ...)
Je vais à ce propos "voir" s'il est plus "adroit" d'utiliser cette propriété tag ou de "jouer" avec les noms des textboxes concernées par un contrôle de saisie ...
- saisie "réelle" sur la textbox "contrôlée", synchronisée sur la saisie dans le "tremplin"
- utilisation (en sortie) du seul évènement exit de la textbox tremplin
etc ...
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPrivate deja As Boolean
Private col As New Collection
Private Sub Teste_Change()
If ActiveControl.Text <> "" And Len(ActiveControl.Text) < 10 Then
If Not deja Then inhibe ActiveControl: deja = True
Else
activer ActiveControl
End If
End Sub
Private Sub inhibe(ct As Object)
For i = 1 To col.Count
col.Remove (i)
Next
Dim c As Object
For Each c In Me.Controls
If Not c Is ct And c.Enabled Then
c.Enabled = False
col.Add c.Name, c.Name
End If
Next
End Sub
Private Sub activer(ct As Object)
Dim c As Object
For i = col.Count To 1 Step -1
Me.Controls(col.Item(i)).Enabled = True
col.Remove (i)
Next
deja = False
End Sub
Or si un tel contrôle demeure obligé, pourquoi vouloir le faire en cours de saisie?