Bonjour à tous,

J'ai 2 TextBoxs, la première qui reçoit une chaine de caractère brute, et la deuxième qui affiche cette même chaîne de caractère mais beaucoup plus agréable et lisible, après un clic sur un bouton.
Cette chaine de caractère est en fait un code SQL qui provient de MS-ACCESS.

Je cherche à créer une boucle en VBA qui va lire par exemple tout ce qui se situe entre SELECT et FROM dans la TextBox1 et qui va me l'afficher avec des retours chariots après chaque élément dans la TextBox 2.
La boucle doit détecter le mot-clé SELECT et s'effectuer tant que le mot-clé "FROM" n'a pas été rencontré. Dès lors que le mot est rencontré, la boucle s'arrête et passe à ce qui se situe entre FROM et WHERE, c'est-à-dire "bouclé" jusqu'à la rencontre du mot clé "WHERE" et ainsi de suite, de manière à dérouler la requête et permettre d'améliorer sa lisibilité.

Pour l'instant, j'ai réalisé ceci, mais ce n'est pas une solution optimale car je risque de ne pas traiter le cas de certaines requêtes SQL complexes à déchiffrer.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
Sub Test()
 
' déclaration des variables
Dim aFormerSQLString() As String     ' Récupère la chaine SQL non lisible
Dim astrMotCles(27) As String          ' Variable tableau qui contient les éléments (mots clés, attributs, etc.) de la syntaxe SQL
Dim intCptSQLString As Integer         ' Entier compteur
Dim strNewSQLString As String         ' Affiche la chaine SQL lisible
 
 ' Mot Clés de la syntaxe SQL
 astrMotCles(1) = "SELECT"
 astrMotCles(2) = "INTO"
 astrMotCles(3) = "FROM"
 astrMotCles(4) = "WHERE"
 astrMotCles(5) = "GROUP"
 astrMotCles(6) = "HAVING"
 astrMotCles(7) = "ORDER"
 astrMotCles(27) = "INSERT"
 
 ' Attributs placés après l'instruction SELECT
 astrMotCles(8) = "ALL"
 astrMotCles(9) = "DISTINCT"
 astrMotCles(10) = "DISTINCTROW"
 astrMotCles(11) = "TOP"
 astrMotCles(12) = "PERCENT"
 
 ' Attributs de la clause FROM
 astrMotCles(13) = "INNER"
 astrMotCles(14) = "LEFT"
 astrMotCles(15) = "RIGHT"
 
 ' Paramètres de la clause WHERE
 astrMotCles(16) = "AND"
 astrMotCles(17) = "OR"
 astrMotCles(18) = "XOR"
 astrMotCles(19) = "BETWEEN"
 astrMotCles(20) = "IN"
 
 ' Autres mots
 astrMotCles(21) = "AS"
 astrMotCles(22) = "As"
 astrMotCles(23) = "In"
 astrMotCles(24) = "ON"
 astrMotCles(25) = "On"
 astrMotCles(26) = "And"
 
 
    ' Suppression des retours chariots contenus dans la chaine SQL d'origine
    UserForm1.TextBox1.Value = Replace(UserForm1.TextBox1.Value, vbCrLf, " ")
 
    ' Découpe la chaine en fonction des espaces " "
    ' Le résultat de la fonction Split est stocké dans un Tableau appelé aFormerSQLString
    aFormerSQLString = Split(UserForm1.TextBox1.Value, " ")
 
 
' Boucle sur le Tbl pour visualiser le résultat
For intCptSQLString = LBound(aFormerSQLString) To UBound(aFormerSQLString)
 
    ' Conditions sur les éléments de syntaxe du langage SQL
    Select Case aFormerSQLString(intCptSQLString)
 
        Case Is = astrMotCles(1), astrMotCles(2)
            strNewSQLString = strNewSQLString & aFormerSQLString(intCptSQLString) & Chr(13)
 
        Case Is = astrMotCles(8), astrMotCles(9), astrMotCles(10), astrMotCles(11), astrMotCles(12)
            strNewSQLString = strNewSQLString & aFormerSQLString(intCptSQLString) & Chr(13)
 
        Case Is = astrMotCles(27)
            strNewSQLString = strNewSQLString & aFormerSQLString(intCptSQLString) & " " & aFormerSQLString(intCptSQLString + 1) & Chr(13)
            intCptSQLString = intCptSQLString + 1
 
        Case Is = astrMotCles(3), astrMotCles(4), astrMotCles(6)
            strNewSQLString = strNewSQLString & aFormerSQLString(intCptSQLString) & Chr(13)
 
        Case Is = astrMotCles(5), astrMotCles(7), astrMotCles(13), astrMotCles(14), astrMotCles(15)
            strNewSQLString = strNewSQLString & aFormerSQLString(intCptSQLString) & " " & aFormerSQLString(intCptSQLString + 1) & Chr(13)
            intCptSQLString = intCptSQLString + 1
 
        Case Is = astrMotCles(16), astrMotCles(17), astrMotCles(18), astrMotCles(26)
           strNewSQLString = strNewSQLString & aFormerSQLString(intCptSQLString) & " "
 
        Case Is = astrMotCles(19), astrMotCles(20)
           strNewSQLString = strNewSQLString & aFormerSQLString(intCptSQLString) & " "
 
        Case Is = astrMotCles(21), astrMotCles(22), astrMotCles(23), astrMotCles(24), astrMotCles(25)
            strNewSQLString = strNewSQLString & aFormerSQLString(intCptSQLString) & " "
 
        Case Is = "="
           strNewSQLString = strNewSQLString & aFormerSQLString(intCptSQLString) & " "
 
        Case Else
            strNewSQLString = strNewSQLString & StrConv(aFormerSQLString(intCptSQLString), vbLowerCase) & Chr(13)
 
    End Select
 
Next intCptSQLString
 
' Espace après chaque parenthèse pour améliorer la lisibilité de la chaine SQL
strNewSQLString = Replace(strNewSQLString, "(", " " + "(" + " ")
strNewSQLString = Replace(strNewSQLString, ")", " " + ")")
 
 
UserForm1.TextBox2.Value = strNewSQLString
 
End Sub

J'ai tenté avec des Boucles While, mais le résultat n'était pas très concluant...

Si vous pouviez m'aider, ça serait super !!
Je vous remercie d'avance