' cet exemple nécessite 1 bouton et 4 zones de texte
Option Explicit
'
Const sSAMPLE1 As String = "(Text1.Text * Text2.Text)"
Const sSAMPLE2 As String = "(Text3.Text - Text4.Text) * 2)"
Const sSAMPLE3 As String = "(Text1.Text / 2) - 0.125)"
'
'
Private Sub Form_Load()
Text1.Text = "10"
Text2.Text = "10"
Text3.Text = "40"
Text4.Text = "27,4"
End Sub
'
'
Private Sub Command1_Click()
MsgBox sSAMPLE1 & vbCrLf & GetResultString(sSAMPLE1)
MsgBox sSAMPLE2 & vbCrLf & GetResultString(sSAMPLE2)
MsgBox sSAMPLE3 & vbCrLf & GetResultString(sSAMPLE3)
End Sub
'
'
Private Function GetResultString(ByVal sChaine As String) As String
sChaine = Replace(sChaine, "(Text", "( Text")
' on remplace les libellés "NOM.text" par leur contenu réel
Dim Ctrl As Object, i As Integer, iPos1 As Integer, iPos2 As Integer
Dim sObjName As String, sObjValue As String, bFound As Boolean
iPos1 = InStr(1, sChaine, " Text")
While iPos1 > 0
iPos1 = iPos1 + 1
iPos2 = InStr(iPos1, sChaine, ".Text")
sObjName = Mid$(sChaine, iPos1, iPos2 - iPos1)
bFound = False
For Each Ctrl In Me
If TypeOf Ctrl Is TextBox Then
If Ctrl.Name = sObjName Then
bFound = True
sObjValue = Ctrl.Text
Exit For
End If
End If
Next
If Not bFound Then 'au cas où
Err.Raise vbObject, , "La zone texte de la formule n'existe pas!!!!!!"
Else
sChaine = Replace(sChaine, sObjName & ".Text", sObjValue)
iPos1 = InStr(1, sChaine, " Text")
End If
Wend
sChaine = Replace(sChaine, " ", vbNullString)
' retour
GetResultString = Replace(sChaine, ",", ".")
End Function