LEN function (2 Viewers)

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 05:56
Joined
Jan 20, 2009
Messages
12,854
It always pays to understand the rules of the language you are speaking.

The meaning of the expression in VBA is unambiguous.

BTW It is actually an abbreviation of:
Let a = b = c

The Let has been dropped as understood but it is still supported (at least to A2007 which was the lat time I looked)

When VB.NET turned all variables into objects they dropped the Set keyword too.
 

smig

Registered User.
Local time
Today, 22:56
Joined
Nov 25, 2009
Messages
2,209
It always pays to understand the rules of the language you are speaking.
Good point :)

The meaning of the expression in VBA is unambiguous.

BTW It is actually an abbreviation of:
Let a = b = c
I already could understand this once Markk used it, and realy liked this short writing :)
I only pointed out why it can be ambiguous for some users. I for example learned everything I know in Access, VBA and programming by myself and it was ambiguous at first look.
Clearly you can't even see why I might see it as ambiguous.

Though I'm celebrating my 50th birthday soon, I'm still learning new things every day thanks to users like you :)
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:56
Joined
Sep 12, 2006
Messages
15,677
out of interest, why not do this with a query?
 

radek225

Registered User.
Local time
Today, 12:56
Joined
Apr 4, 2013
Messages
307
Because I don't know, how to do this with a query:p
 
Last edited by a moderator:

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:56
Joined
Sep 12, 2006
Messages
15,677
In math and for most people writing:
A = B = 3
Is the same as:
A = B and B = 3, result A and B = 3

for you it's only make sense it's like saying:
If B = 3 then A is True Else A is False



So maybe I don't understand how to read logical expressions, but I'm happy to be on the normal side of the globe :D

smig. the bit in bold is exactly what it is


although there is an implied "let", I prefer to think of the significant part of this as the expression having notional brackets

hence

A = B = 3 is actually

A = (B = 3)

so the compiler first establishes a Boolean result as to whether B and 3 are equal, and assigns the result to A.

----
it's the way the compiler resolves the expression in machine code.

so it gets the expression A =

it's sets up a register to store the value for A, then looks at what A is equal to. Which is (B = 3). And it knows from this that B = 3 is a logical comparison, because we are now on the RHS of the equation/expression.

Something like that. Maybe have a look at RPN (reverse polish notation). I think that's where expression parsing comes from.

----

the Boolean assignment is how vba does it. other languages may work differently.

it's similar in concept to

dim a,b as string

some languages would dim a and b as strings

as we know vba dims b as a string, but dims a as a variant.

just the way the compiler works.
 
Last edited:

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:56
Joined
Sep 12, 2006
Messages
15,677
Because I don't know, how to do this with a query:p

design a query in the access design pane. put in the field you want to check.

in the criteria put null, or maybe null or ""
(or anything else, depending on what you want)

include some other columns so you can identify the rows.
run the query.



(sorry, I mistakenly edited your post initially instead of quoting it)
 

smig

Registered User.
Local time
Today, 22:56
Joined
Nov 25, 2009
Messages
2,209
smig. the bit in bold is exactly what it is


although there is an implied "let", I prefer to think of the significant part of this as the expression having notional brackets

hence

A = B = 3 is actually

A = (B = 3)

so the compiler first establishes a Boolean result as to whether B and 3 are equal, and assigns the result to A.

----
it's the way the compiler resolves the expression in machine code.

so it gets the expression A =

it's sets up a register to store the value for A, then looks at what A is equal to. Which is (B = 3). And it knows from this that B = 3 is a logical comparison, because we are now on the RHS of the equation/expression.

Something like that. Maybe have a look at RPN (reverse polish notation). I think that's where expression parsing comes from.

----

the Boolean assignment is how vba does it. other languages may work differently.

it's similar in concept to

dim a,b as string

some languages would dim a and b as strings

as we know vba dims b as a string, but dims a as a variant.

just the way the compiler works.

I understood how it works in VBA hours ago :) No need to convince me it's how VBA does it.
I only said it's not trivial and can be ambiguous to some ppl.

I really appreciate you, Galaxiom, MarkK and many other for teaching me new things for many years I'm using this forum.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:56
Joined
Sep 12, 2006
Messages
15,677
I understood how it works in VBA hours ago :) No need to convince me it's how VBA does it.
I only said it's not trivial and can be ambiguous to some ppl.

I really appreciate you, Galaxiom, MarkK and many other for teaching me new things for many years I'm using this forum.

yes, it can definitely be confusing


replacing

if statement=true
result = true
else
result = false
end if


with constructs like

result = statement
result = statement = true
result = somevar = someothervar

is definitely not an obvious step to take.
 

Brianwarnock

Retired
Local time
Today, 20:56
Joined
Jun 2, 2003
Messages
12,701
I like concise neat code too, but at 2am when debugging somebody else's undocumented code clarity beats concise everytime.

Brian
 

MarkK

bit cruncher
Local time
Today, 12:56
Joined
Mar 17, 2004
Messages
8,186
I never even do
Code:
If someBoolean = True Then
Comparing a boolean to True has no effect. The boolean already has only two states, True or False. Compare True to True, you get True; compare False to True, you get False, so "= True" does nothing. "= True" is the identity function in boolean math, the same as "* 1" in decimal math.
My college prof wanted to see . . .
Code:
If someBoolean Then
But I think boolean math looks very distinct in code. It's not hard to distinguish from normal math.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 05:56
Joined
Jan 20, 2009
Messages
12,854
Confusing is only a matter of lack of familiarity.

Many developers would not be familiar with these perfectly legitimate VBA expressions.
(The variables are integers.)

a = b And c

a = b Or c
 

smig

Registered User.
Local time
Today, 22:56
Joined
Nov 25, 2009
Messages
2,209
Confusing is only a matter of lack of familiarity.

Many developers would not be familiar with these perfectly legitimate VBA expressions.
(The variables are integers.)

a = b And c

a = b Or c
Honestly I have no idea what this will give me :D

I guess you should remember that not all developers are at the same level as you. Clearly I'm not and I'm happy to have you and many other to help me when I need :)
When you put concise neat code you will probably be asked for some explanations.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 05:56
Joined
Jan 20, 2009
Messages
12,854
When you put concise neat code you will probably be asked for some explanations.

When I use those ones I do include an explanation. They are a type of operator that many developers never encounter and probably rarely need in their own code. However Access would use them internally whenever you see summable option constants in a 1,2,4,8,16 ... etc pattern.

They are bitwise operators. They allow a single operation to simultaneously process what would otherwise potentially require a plethora of boolean variables and many lines of code.

A good example of using them, with a brief explanation included in the code at post 6 in this thread.
 

smig

Registered User.
Local time
Today, 22:56
Joined
Nov 25, 2009
Messages
2,209
When I use those ones I do include an explanation. They are a type of operator that many developers never encounter and probably rarely need in their own code. However Access would use them internally whenever you see summable option constants in a 1,2,4,8,16 ... etc pattern.

They are bitwise operators. They allow a single operation to simultaneously process what would otherwise potentially require a plethora of boolean variables and many lines of code.

A good example of using them, with a brief explanation included in the code at post 6 in this thread.
I totally lost you here :eek:
 

MarkK

bit cruncher
Local time
Today, 12:56
Joined
Mar 17, 2004
Messages
8,186
There are 10 types of people in the world, those who understand binary math, and those who don't.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:56
Joined
Sep 12, 2006
Messages
15,677
bitwise operations.

an 8 bit byte can go from

00000000 to 11111111 - ie 256 different values

so 00100001 corresponds to number 65, (or is also interpreted as capital A)

now, if the individual bits are considered independently then the single byte consists of 8 separate bits (or flags). Effectively you get 8 Booleans in a single byte. Useful and efficient for many applications.

now you can compare two such patterns using logical operators, AND, OR, and others. A very important one is XOR

AND returns a 1 only where the two bits are both 1. this is used to test whether a flag is set or not.
00100001 AND
00100000
00100000 - result

if value AND 64 = 64 then
- tests whether bit 3 is set or not



OR returns a 1 where either bit is one 1. This can therefore be used to set a flag.
00100001 OR
00100010
00100011 - result

value = value or 64
- sets bit 3.


XOR returns a 1 where only one of the bits (but not both) is a 1
00100001 XOR
01101010
01001011 - result

the useful thing about XOR is that repeating the operation returns the original value

01001011 XOR
01101010
00100001 - result

because of this, XOR is very useful as a simple encryption tool. XOR a byte (char) with another byte, and you get an encrypted result. XOR it again, and you get the original byte (Char) back again.

encrypt = original XOR mask
original = encrypt XOR mask
 
Last edited:

smig

Registered User.
Local time
Today, 22:56
Joined
Nov 25, 2009
Messages
2,209
Code:
(The variables are integers.)
a = b And c
a = b Or c
Are you saying that b and c are integers converted into their binary numbers and a is the result of the manipulation on the single bits of them :eek:

so:
A = 8 OR 3 will result as A = 11
A = 8 AND 3 will result as A = 0
A = 6 OR 3 will result as A = 7


Or do I make myself stupid ? :D
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:56
Joined
Sep 12, 2006
Messages
15,677
Code:
(The variables are integers.)
a = b And c
a = b Or c
Are you saying that b and c are integers converted into their binary numbers and a is the result of the manipulation on the single bits of them :eek:

so:
A = 8 OR 3 will result as A = 11
A = 8 AND 3 will result as A = 0
A = 6 OR 3 will result as A = 7


Or do I make myself stupid ? :D

those 3 examples are correct, I think.
easily tested, too!
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:56
Joined
Sep 12, 2006
Messages
15,677
Code:
(The variables are integers.)
a = b And c
a = b Or c
Are you saying that b and c are integers converted into their binary numbers and a is the result of the manipulation on the single bits of them :eek:

so:
A = 8 OR 3 will result as A = 11
A = 8 AND 3 will result as A = 0
A = 6 OR 3 will result as A = 7


Or do I make myself stupid ? :D

yes, that's right. those 3 examples are correct, I think.
easily tested, too!

?6 or 3 in the immediate window gives 7
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 05:56
Joined
Jan 20, 2009
Messages
12,854
This code scrap would cause some head scratching to a beginner:

Code:
a = 3
If a = 6 Or 3 Then

;)
 

Users who are viewing this thread

Top Bottom