Solve a string with True or False

saracen666

Registered User.
Local time
Today, 22:45
Joined
May 12, 2017
Messages
11
Hi All,
Hope you are well.

I am looking to solve a string containing a textual Boolean expression.

e.g. I want to solve

Boolean result = "(True Or False) | False | (True And False)"

How can I do this?
 
in what? a query?
IIF([field]="true",true,false)
 
A VBA text string so:

Dim string1 As String
Dim result As Boolean

string1 = "(True Or False) | False | (True And False)"
result = string1

So in this case result should be Boolean True
 
Please walk me through how that string resolves to true. Also, what does the pipe character (|) represent?
 
Pipe is OR. True or false or false = true

Use the Eval() function

Code:
Private Sub Command2_Click()
    MsgBox Eval(Replace(Text0, "|", " or "))
End Sub

Private Sub Form_Load()
    Text0 = "(True Or False) | False | (True And False)"
End Sub
 
Hi,
Getting failures when the string length is very long. What is the maximum string length eval() can handle?

For example runtime error '10025' is flagged when the rule is.....
(False Or True) And True And (False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or True Or False Or False Or False Or False Or False Or False)
 
Ugh.

This suggests to me that you need a totally different approach
If you can understand that string, well done you...

Not sure why static suggested using eval but in answer to your question
"No max length limit except for that placed on string length...."

FYI error 10025 is:

"This operation will test the table's record and field validation rules, as well as the Required and AllowZeroLength properties, for all data in the table.@This process may take a long time. Do you want to continue anyway?@@19@@@2"
 
Continue button is disabled.

The field is long text so cannot set maximum.

Should I be setting record and field validation rules for this?

Are there alternative functions to eval()
 
static suggested eval because it answered the question.

The question appears to have turned stupid though.
 
static suggested eval because it answered the question.
:)

OK I accept it gives the answer -1 for the example in post 3
I suppose I'm not clear what Saracen is actually doing when the output is as shown in post 7
 
Saracen,

This is beginning to look like either an intellectual exercise (not against that, by the way) or some sort of proof-of-concept for something that might eventually become a programmable logic controller or logic expression analyzer.

I think we need an explanation of WHY you are doing this because the answers could differ very much based on your response.

Are there alternative functions to eval()

Yes - building your own type of expression analyzer.

Should I be setting record and field validation rules for this?

Not unless there is another reason to do so.

As to the warning implicit in error 10025, here's the catch. As long as the field in question has no explicit string limit, you are probably OK up to the first limit I would expect for text - 65K bytes for a very long string. Your string as posted is nowhere near that length. I don't know why your Continue button is disabled, but then again I've never actually run into that message since I don't use EVAL that often.

The limit of 65K bytes comes from this article:

https://support.office.com/en-us/ar...ications-1E521481-7F9A-46F7-8ED9-EA9DFF1FA854

The specifications for Access don't change that much from 2003 forward, so that is as good as any other even if you aren't using Ac2010. At least half a dozen elements show the 65K limit, which means that the most commonly used string-length pointers are 16-bit unsigned integers.

Note, however, that an occasional limit pops up for shorter strings. Often, that limit is related to some other method of string input. How are YOU getting these strings into the database for evaluation?
 
There is no need to store such a long string

Let's assume 1 is true and 0 is false.
Lets turn OR's and AND's into pluses

(0+1)+1+(0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+1+0+0+0+0+0+0)

We see there are 3 parts to this rule. Each part needs to contain a 1 so 0's are worthless.

(1)+1+(1) = 3
 
Hi the returned result should only be boolean. For example:

bool = (False Or True) And True And (False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or False Or True Or False Or False Or False Or False Or False Or False)

in this case true
 
Just remember that unless you declared variable Bool to actually be datatype Boolean, your answer will be 0 or -1. You can guarantee the output format by printing or displaying CBool(Bool).

But we still need to know what you are really trying to do to see if there is a shortcut or alternate approach. Don't tell us the formula. Tell us the procedure and goal in English. For which, by the way, the correct answer is NOT "return true or false from evaluating this string."
 
Hi,
My company receives order strings. Order codes that are comma separated. I also have a set of validation rules to validate the order string.

So i do the following:

Extract validation rule and split codes into an array.

Lookup the validation codes to see if they are in the order string and replace with true or false. Essentially building what you have seen. The validation rules can be any length upto capacity of long text.

Evaluate the built true or false statement to single true or false result. Indicating whether the order has passed the validation rule. Eval worked well up until the long strings.

I deleted 2 falses from what I posted and the runtime error was no more. But this doesn't reflect the original rule.
 
Code:
Function CheckRule(strRule As String) As Boolean
    Debug.Print
    Dim s As String
    s = Replace(strRule, "true", "1")
    s = Replace(s, "false", "0")
    s = Replace(s, "or", "|")
    s = Replace(s, "and", "+")
    s = Replace(s, " ", "")
    s = Replace(s, "0|", "")
    s = Replace(s, "|0", "")
    
    s = Replace(s, "+", " and ")
    s = Replace(s, "|", " or ")
    Debug.Print s
    CheckRule = CBool(Eval(s))
End Function

example

Code:
Private Sub Command0_Click()
    Dim strRule As String
    
    strRule = "(False Or True) And True And (False Or False Or " & _
        "False Or False Or False Or False Or False Or False Or " & _
        "False Or False Or False Or False Or False Or False Or " & _
        "False Or False Or False Or False Or False Or False Or " & _
        "False Or False Or False Or False Or False Or False Or " & _
        "False Or False Or False Or False Or False Or False Or " & _
        "False Or False Or False Or False Or False Or False Or " & _
        "False Or False Or False Or False Or False Or False Or " & _
        "False Or False Or False Or False Or False Or False Or " & _
        "False Or False Or False Or False Or False Or False Or " & _
        "False Or False Or False Or False Or False Or False Or " & _
        "False Or False Or False Or False Or False Or False Or " & _
        "False Or False Or False Or False Or False Or False Or " & _
        "False Or False Or False Or False Or False Or False Or " & _
        "False Or False Or False Or False Or False Or False Or " & _
        "False Or False Or False Or False Or False Or False Or " & _
        "False Or False Or True Or False Or False Or False Or " & _
        "False Or False Or False)"
        
    Debug.Print CheckRule(strRule)
    
    Debug.Print CheckRule("((true Or True) or true or false) and true and (true or (false or true))")
    
    Debug.Print CheckRule("(true Or True) or true or false")

    Debug.Print CheckRule("(false Or false) or true or false")

    Debug.Print CheckRule("false and (false Or false)")

    Debug.Print CheckRule("true and (false Or false)")
End Sub

results


Code:
(1) and 1 and (1)
True

((1 or 1) or 1) and 1 and (1 or (1))
True

(1 or 1) or 1
True

(0) or 1
True

0 and (0)
False

1 and (0)
False
 
I deleted 2 falses from what I posted and the runtime error was no more. But this doesn't reflect the original rule.

This very strongly suggests that your problem is string length.

I'm going to go out on a limb here. You problem is these arbitrary rules. Not saying "random" but rather "unpredictable." I.e. the customer who places these orders could order pretty much anything and you have no way of knowing until this "order string" arrives and you get to see it.

My thought is that you are aggregating this in the wrong way, horizontally instead of vertically. What happens if you find an order code that isn't right? Do you reject the entire order? In other words, is there really an aggregate Boolean sum here that affects the entire order, invalidating it perhaps? Or does it work the other way, stating that if at least one order code was correct, you might be able to ship SOMETHING even if not the whole order?

In any case, I would perhaps split this up into two tables, parent/child style. The order overall record is the information about the order source and the child records would be the individual pieces-parts. The order record would have some PK, doesn't matter what, could even be autonumber. The order code records would have the "rules" regarding the individual codes and would be marked with a foreign key (FK) of the parent order. By splitting each order code test into a simpler subset of that awkward input string, you get to "divide and conquer" - and old Julius Caesar was pretty smart about that tactic.

Then you would have a summary Boolean code in the parent record (or more than one depending on what you want). Or a set of counters, perhaps. You might then run a couple of queries on the child records based on a JOIN of the order record PK to the order code record FK.

Code:
UPDATE Orders INNER JOIN OrderCodes ON Orders.OrderPK = OrderCodes.OrderFK
SET Orders.GoodOrder = Orders.GoodOrder + 1
WHERE OrderCodes.GoodFlag = TRUE;

This would give you the ability to "normalize" the incoming data. With a couple of SQL queries similar to the one I showed you could count good orders, bad orders, total orders, etc. AND could build a report with a header for an order and a detail section that listed each code separately, flagged with good or bad markers.

Therefore, your original problem could be attributed to simply having unnormalized data that was too intractable to manipulate wholesale.
 

Users who are viewing this thread

Back
Top Bottom