UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

2 Pages V   1 2 >  
Reply to this topicStart new topic
> ok basic question    
 
   
vincent1701
post Jun 15 2007, 05:17 PM
Post #1

UtterAccess Addict
Posts: 290
From: Riverhead, NY



OK, this is a really basic question but I never have gotten a handle on what this means...

When I see
If fOpenFile Then x=1

What does this mean? If fOpenFile what? is it asking if it contains a value? is it Null? What does it mean? Where's the argument?

I don't get it.
Vince
Go to the top of the page
 
+
GroverParkGeorge
post Jun 15 2007, 05:24 PM
Post #2

UA Admin
Posts: 19,373
From: Newcastle, WA



fOpenFile is a function. Although you didn't include that function here, one can assume that it returns a boolean value (true or false) and one can also assume--based on the name of the function--that it evaluates whether a particular file is open. Without seeing the function itself, one can only guess how that works.

So, the If statement basically says" If the file is open, set the value of x to 1"

Place the cursor on fOpenFile in your VBA module and press [Shift]F2 to go to the location of that function in your db. You can see what it does there.

George
Go to the top of the page
 
+
vincent1701
post Jun 15 2007, 08:01 PM
Post #3

UtterAccess Addict
Posts: 290
From: Riverhead, NY



Thanks. So basically when you don't put an argument after a function (or perhaps also a variable?) it is really like testing whether it has a value of True:

So
If fOpenFile then x=1
is the same as?
If fOpenFile = True then x=1
Go to the top of the page
 
+
ScottGem
post Jun 15 2007, 08:22 PM
Post #4

UtterAccess VIP / UA Clown
Posts: 25,200
From: LI, NY



Correct. When testing a boolean value the default is True so its unnecessary to use = True.
Go to the top of the page
 
+
niesz
post Jun 15 2007, 08:30 PM
Post #5

Utter A-fishin'-ado
Posts: 17,723
From: Cincinnati, Ohio, USA . . . ><((((°>



Actually, all comparisons boil down to a True or False.

Example:

x=2
y=1

If x>y Then ...
If 2>1 Then ...
If True Then ...

You just don't see this happening because it is occuring in memory.
Go to the top of the page
 
+
vincent1701
post Jun 15 2007, 08:54 PM
Post #6

UtterAccess Addict
Posts: 290
From: Riverhead, NY



thanks guys
Go to the top of the page
 
+
datAdrenaline
post Jun 16 2007, 03:04 AM
Post #7

UtterAccess Editor
Posts: 16,028
From: Northern Virginia, USA



Vincent Asks:
>> So basically when you don't put an argument after a function (or perhaps also a variable?) it is really like testing whether it has a value of True:

Scott Answer.
>> Correct. When testing a boolean value the default is True so its unnecessary to use = True.<<

Walter Answer.
>> Actually, all comparisons boil down to a True or False <<

<< Now ... I would like to peg the geek meter a bit ...>>

... actually .. every boolean test is a test against 0 ... so the default when testing a boolean condition is acutally a test for <> 0 ... which is different than a test for True.

By most coding standards, the value of 0 equals False, and True is not equal to 0. In MS Access the VBA constant of "True" = -1, the constant "False" = 0. With the actual boolean datatype (which is 2 byte integer at the machine level) a 0 is False, and anything between -32768 and 32787 (except 0) is coerced by VBA to a -1 before being stored in the boolean declared variable or field.

To demostrate:

Dim blMyBoolean As Boolean
blMyBoolean = 32000 'Seems like this should error out ... but it doesn't ... kind cool eh?
Debug.Print CInt(blMyBoolean)

Will yeild a -1 in the immediate window ..

So with Walters examples ...

If x>y Then ...
If 2>1 Then ...
If True Then ...

Is compiled down to ... (Note: the expession x>y will return a Boolean datatype, which is always coerced to 0 and -1 in VBA/MS Access)

If (x>y) <> 0 Then ...
If (2>1) <> 0 Then ...
If (True) <> 0 Then ...
If -1 <> 0 Then ...

And with Scotts assertation that ....
If fOpenFile Then ...

is equivalent to:
If fOpenFile = True Then ...

Falls apart. For example, lets make the assumption that fOpenFile returns a 1 if all is good, and 0 otherwise. So now lets assume the function will return a 1 because all is good ... the breakdown of the logic would be this:

If fOpenFile Then ... {In VBA like this}
If 1 Then ... {the function returns 1}
If 1 <> 0 Then ... {the returned value is compard to the default of <> 0}
{The True Part executes ... as expected}

Now lets apply the assertation that the default test is for = True ....

If fOpenFile = True Then ... {In VBA like this}
If 1 = -1 Then ... {the function returns 1, the constant True returns -1, (1 = -1) returns the boolean of False, which will force the If..Then to execute the "False" Part of the construct ... which is bad, because the function returns a 1, which was meant to mean all is good}

To demostrate in the immediate window ...

? 1 = True
False
? 1 <> False
True

So with all that ... you may see code that is intended to be ported look like this ...

If SomeExpression <> 0 Then ...

... I hope that explains a lot! ... I have a kid crying now so I have to go!!
Go to the top of the page
 
+
niesz
post Jun 16 2007, 07:38 AM
Post #8

Utter A-fishin'-ado
Posts: 17,723
From: Cincinnati, Ohio, USA . . . ><((((°>



Hey Brent. Ya gotta love your replies. (IMG:http://www.utteraccess.com/forum/style_emoticons/default/sad.gif) I think this one broke the meter! (IMG:http://www.utteraccess.com/forum/style_emoticons/default/laugh.gif) Maybe I can swing the needle from the technical side over to the psychological side for a moment. While I can't speak for Scott (I'll assume anyway), I do know what was going through my mind when I wrote what I did.

When I first seen Scott's response, I thought to myself that it wasn't the most intuitive answer. Let just say that I thought it could have been phrased differently to provide the maximum benefit for the poster. I'm pretty sure I know what he meant. He meant that:

If fOpenFile Then ...

..is the same as..

If fOpenFile = True Then ...

..confirming Vincent's assumption. Myself, I have never thought of an If..Then statement as having a default, so I guess if I was replying, I would never have used the word "default". But nonetheless the door was opened and the cows were out of the barn.

My reply, (in my own little concept of reality) was more to clarify Scott's statement, than anything. The point I was making was that ANY evaluation always comes down to a True or False test. And when I use those terms, I'm not speaking in the terms of the VBA environment True or False, ... I simply mean YES or NO. That's why I posted:

>>If True Then ...<<

Vincent (who's probably more confused than ever by now) seems to be the inquistive type, given the type of posts that he's recently started. And that's a good thing. But by the nature of his posts, he didn't come across to me as the technical type. (How's that for a 10-minute psycho-evaluation! (IMG:http://www.utteraccess.com/forum/style_emoticons/default/wink.gif) ) So when I crafted my response, I used terms that I thought he would be more familiar with. To be put more succinctly, it could have been written:

If [Found to be True] Then ...

Correspondingly, saying:

If [Boolean]=True Then ...
If True = True Then ...
If [True] Then ... <<< (Here True meaning (True = True) evaluated to being Non-VBA True.

But I thought that would be too confusing. (Which, in looking at it now, seems to confirm my initial thought. (IMG:http://www.utteraccess.com/forum/style_emoticons/default/wary.gif) )

You, on the other hand, when seeing my response (and knowing the way you process information with the geek side of your brain) translated it literally in within the framework on VBA. (you big geek! (IMG:http://www.utteraccess.com/forum/style_emoticons/default/grin.gif) )

While I don't think any of our three responses were wrong in any way (at least in our own microcosms of reality), I fear we may have confused poor Vincent more that ever, because at first glance, we seem to be contradicting ourselves. Given that, I would like to apologize for any misguidance I may be responsible for, and I fully take responsibility for any resulting confusion.

Looking at his thread in retrospect, I believe, just goes to show the great many varied points of view from our global gene pool here at UA, and I think it's awesome that many feel comfortable enough to render their opinions even though they may differ from others.

Long Live UA!
(IMG:http://www.utteraccess.com/forum/style_emoticons/default/uarulez2.gif)
Go to the top of the page
 
+
ace
post Jun 16 2007, 09:27 AM
Post #9

UtterAccess VIP
Posts: 5,283
From: Upstate NY, USA



QUOTE
So basically when you don't put an argument after a function (or perhaps also a variable?) it is really like testing whether it has a value of True:

Argument is not the right word. Comparison would be closer to
correct.

When you don't make a specific comparison of the value returned by a function
it defaults to whether it evaluates to true.

By a specific comparison I mean If fOpenfile = something.

You also need to be aware that if the function returns something that
that con not be converted to a boolean value it will raise an error.

CODE
Public Function bub() As String
  bub = "Zero"
End Function


If bub Then 'raises an error
If Len(bub) then 'evaluates to True
Go to the top of the page
 
+
GroverParkGeorge
post Jun 16 2007, 12:11 PM
Post #10

UA Admin
Posts: 19,373
From: Newcastle, WA



Thanks, Brent, Walter and Ace.


I created the following sub:

Public Sub test()
Dim blMyBoolean As Boolean
blMyBoolean = 32000
Debug.Print CInt(blMyBoolean)
Debug.Print blMyBoolean
End Sub

In the immediate window the following lines appeared:

-1
True

Why would that be?

George


Edited by: GroverParkGeorge on Sat Jun 16 13:23:38 EDT 2007.
Go to the top of the page
 
+
GroverParkGeorge
post Jun 16 2007, 12:13 PM
Post #11

UA Admin
Posts: 19,373
From: Newcastle, WA



Even more cool:

Public Sub test()

Dim blMyBoolean As Boolean
blMyBoolean = 32000
Debug.Print CInt(blMyBoolean)
Debug.Print blMyBoolean
Debug.Print CInt(blMyBoolean) = blMyBoolean
End Sub

Returns:

-1
True
True
Go to the top of the page
 
+
niesz
post Jun 16 2007, 01:11 PM
Post #12

Utter A-fishin'-ado
Posts: 17,723
From: Cincinnati, Ohio, USA . . . ><((((°>



A boolean value will either be True or False when queried.

blMyBoolean = 32000 <===This line sets blMyBoolean to True (simply because it is non-zero).
Debug.Print CInt(blMyBoolean) <===This coersion function forces the display of an Integer, thus the -1.
Debug.Print blMyBoolean <===When the boolean is queried , a True is returned (as stated above)
Debug.Print CInt(blMyBoolean) = blMyBoolean <===This line is returning the result of the comparison between the two values (-1 and True), which are equal in the Access world of things. And since the comparison equates, the result is True.
Go to the top of the page
 
+
datAdrenaline
post Jun 16 2007, 01:46 PM
Post #13

UtterAccess Editor
Posts: 16,028
From: Northern Virginia, USA



Ok ... I just re-read my post ... maybe a little heavy (sorry Vincent!)... see what happens when I can't sleep!!

However, the indication that:

If [expression] Then ...
is the same as:
If [expression] = True Then ...

Is in fact an incorrect statement (sorry Scott). It is correct, however, to say ...

If [expression] Then ....
is the same as:
If [expression] <> False Then ...
.. Or ..
If [expression] <> 0 Then ...

Also this statement by Ace ...
>>When you don't make a specific comparison of the value returned by a function
it defaults to whether it evaluates to true. <<

Is not accurate ... it would be correctly stated like this: (not trying to be difficult!!! ... (IMG:style_emoticons/default/smile.gif) )

>> When you don't make a specific comparison of the value returned by a function
it defaults to whether it evaluates to NOT FALSE (aka: Not Zero) << .... which, as stated is slightly different than equal to True. Remember True has the value of -1, so IF the implicit comparison is to True, then we are comparing to -1, which as shown, won't work.

Also ... if the [expression] in an If..Then construct results in a value of Null, the FALSE PART of the If..Then construct would be executed.... forgot to answer that for Vincent ...

In addition ... I wanted to point out that a Boolean datatype is simply a formated Integer (in a sense). Just like the Date datatype is a formated Double ..

(Note ...sorry if this reply is broken up ... I wrote it in 3 different moments of time! ... )

Hope all this makes sense, also, I mean no disrespect to anyone with this discussion.
Go to the top of the page
 
+
niesz
post Jun 16 2007, 02:07 PM
Post #14

Utter A-fishin'-ado
Posts: 17,723
From: Cincinnati, Ohio, USA . . . ><((((°>



I think this just reiterates my point that this whole subject was spawned because of semantics of expression.

I don't find either Scott's or Ace's responses incorrect when viewed in the light of the term "True" being used to express "found to be true", and not in the light of "evaluates to a True (-1) value".

>>I mean no disrespect to anyone with this discussion.<<
There was none taken by me, and I think we all feel the same way. (IMG:http://www.utteraccess.com/forum/style_emoticons/default/wink.gif)
Go to the top of the page
 
+
ace
post Jun 16 2007, 02:12 PM
Post #15

UtterAccess VIP
Posts: 5,283
From: Upstate NY, USA



It's all sort of interesting, but the bottom line is that effectively
True = Not False. Just because a value was picked to
assign to the VBA TRUE constant does not change that.

The VBA constants are a bit strange in that TRUE returns the String
TRUE when it is printed and FALSE Returns the string FALSE.

If x then
is effectively the same as
If x = TRUE then
and
If x <> FALSE then

I don't see where the rest of it is relevant except when the constants
TRUE and FALSE are used in some convoluted way.
Go to the top of the page
 
+
niesz
post Jun 16 2007, 02:28 PM
Post #16

Utter A-fishin'-ado
Posts: 17,723
From: Cincinnati, Ohio, USA . . . ><((((°>



I've been trying to think of a way to visually represent Brent's point of view. This is the best that I could achieve:

CODE
Public Function test()

    Dim x As Integer

    x = 30000  
    If x           Then Debug.Print "Evaluates to True" Else Debug.Print "Evaluates to False"
    If x = True    Then Debug.Print "Evaluates to True" Else Debug.Print "Evaluates to False"
    If x <> False  Then Debug.Print "Evaluates to True" Else Debug.Print "Evaluates to False"

End Function

Immediate Window Results:

Print test
Evaluates to True
Evaluates to False
Evaluates to True
Go to the top of the page
 
+
gemmathehusky
post Jun 16 2007, 07:07 PM
Post #17

UtterAccess VIP
Posts: 1,956
From: UK



thats what's been said before

blmyboolean is actually storing 32000 so the first debug.print prints 32000 because youve coerced it to an integer

the second debug.print reports the status of its "underlying" format namely a boolean which can only be true oe false. as 0 is false, and anything else is true, it reports true


this is why you can set booleans by

myboolean = vara>varb

which is

if vara>varb then
myboolean = true
else
myboolean = false
end if
Go to the top of the page
 
+
datAdrenaline
post Jun 16 2007, 09:46 PM
Post #18

UtterAccess Editor
Posts: 16,028
From: Northern Virginia, USA



Gemmathehusky,

>>blmyboolean is actually storing 32000 so the first debug.print prints 32000 because youve coerced it to an integer<<

Nope ... with all due respect, you are incorrect...did you actually execute the code? You are right in that I coerced blMyBoolean into an integer as you state. An integer datatype has a range of -32768 to 32767, so If blMyBoolean was actually storing 32000, then 32000 would be printed in the immediate window, but VBA does the coersion prior to the value being stored ... run this code ..

CODE
Public Sub test()
    
    Dim blMyBoolean As Boolean
    Dim intMyInteger As Integer
    
    blMyBoolean = 32000
    intMyInteger = blMyBoolean
    
    Debug.Print CInt(blMyBoolean)
    Debug.Print intMyInteger
    Debug.Print blMyBoolean
    
End Sub


After running it you will see this in the immediate window:

-1
-1
True

If 32000 was actually stored in the boolean variable then 32000 would be written to the integer typed variable, but in fact a -1 was written to the integer.
Go to the top of the page
 
+
datAdrenaline
post Jun 16 2007, 10:26 PM
Post #19

UtterAccess Editor
Posts: 16,028
From: Northern Virginia, USA



Ace ...

>> The VBA constants are a bit strange in that TRUE returns the String
TRUE when it is printed and FALSE Returns the string FALSE. <<

EVERYTHING is becomes a String when printed/concatenated together! ... And a boolean typed expression or variable will return a formated result of True or False ... just like a Date typed variable will return a format that looks like a date, but a date is strored in memory as Double, just as a Boolean is stored as an Integer, but a special one at that since it will only hold two values -1 (11111111 11111111 in binary) and 0 (00000000 00000000 in binary). To illustrate how everything becomes a string when concatenated together ....

Dim MyString As Variant
MyString = 1 & ":" & Now & ":" & True
Debug.Print MyString & " (" & VarType(MyString) & ")"

--> 1:06/16/2007 5:06:06 PM True (8)

VarType of 8 is vbString ...

VarType(1) => 2 => vbInteger
VarType(Now) => 7 => vbDate
VarType(True) => 11 => vbBoolean

Expanding on this ....

Function MyFunction() As Boolean
MyFunction = 1000 'Which is coerced to -1 BEFORE being written to memory
End Function

VarType(MyFunction) => 11 => vbBoolean

Plus ...
Function MyFunction2() As Integer
MyFunction2 = True '<= Boolean typed constant of True has a value of -1
End Function

VarType(MyFunction2) => 2 => vbInteger

Please read the response I gave here with respect to the assertation that a boolean datatyped variable/constant/function returns the string True. You participated in this discussion, so it may look familiar (IMG:style_emoticons/default/smile.gif) but my reply was to Chris, so you may not have gotten a notice.

Going back to True = Not False ... that is a true statement, and the VBA boolean typed constant named True does return a -1 and the VBA boolean typed constant False = 0, HOWEVER ... an If..Then statement ALWAYS decides an expressions "Truth" by comparing the result of the expression to 0 and anything that is <> 0 will direct the program flow to the "True Part" of an If..Then construct. Any expression result that is 0 or Null will direct the program path to the "False Part" or the If..Then construct.

So ... with all this and all the prior information .... Your statement:

>>
If x then
is effectively the same as
If x = TRUE then
<<

Is only valid if x is a boolean typed variable ... but if its any other numeric type, then your statement is incorrect ... let say x = 1000 (in the immediate window)

? 1000 = True
False
? IIf(1000, True, False)
True
? 1000 <> 0 = True
True

Which is effectively what Walter demonstrated with his brief, yet complete code.

Is any of this making sense?
Go to the top of the page
 
+
datAdrenaline
post Jun 16 2007, 10:39 PM
Post #20

UtterAccess Editor
Posts: 16,028
From: Northern Virginia, USA



Vincent ...

Sorry for robbing your post! ... I hope through all this verbage you were able to extract the information you needed!....
Go to the top of the page
 
+

2 Pages V   1 2 >
Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 19th June 2013 - 09:07 AM