View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Gvaram Gvaram is offline
external usenet poster
 
Posts: 5
Default Evaluate text formula in VBA

Please someone help me with Evaluate function. I have some rules written in
Access table as string with predefined variables in it like:

and(" & CCA & "<30, " & CCB & "800)
and(""" & AAA & """=""" & AAB & """, """ & BBA & """<""" & BBB & """)
and(" & CCA & "<24, """ & BBA & """=""8o6t56565a"")

Then I'm trying to get these rules, supply with the same variables, Evaluate
them in excel and get True or False results. The code lookes like:

Sub HitRules()
Dim k As Integer
Dim DBS As New ADODB.Connection
Dim rstAl As New ADODB.Recordset
Dim AAA, AAB, BBA, BBB As String
Dim CCA, CCB As Long
Dim hhhh

With DBS
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0"
.Open "D:\TestDB.mdb"
End With
rstAl.Open "Select * from Rules", DBS, adOpenStatic, adLockReadOnly
AAA = "0503"
AAB = "0503"
BBA = "8o6t56-a"
BBB = "8o6t56-b"
CCA = 22
CCB = 1200
For k = 0 To rstAl.RecordCount - 1
hhhh = CStr(rstAl!Rule) 'formulas provided above are stored in
rstAl!Rule
Debug.Print k+1 & " - " & Evaluate(hhhh) 'Result should be True or
False
rstAl.MoveNext
Next k
rstAl.Close
DBS.Close
End Sub

But it does not work... It seems that Evaluate does not understands variable
names (AAA, AAB, etc.) and the result is always False(?). And on the 3'rd
rule, error msg appeares: runtime error 13 - type mismatch...
Please advice me something how to avoid this problem. Is there any other way
to read and execut the string formulas?