Learn to create function

  • Thread starter Crystal via AccessMonster.com
  • Start date
C

Crystal via AccessMonster.com

I am new to vba and I am learning to create functions. I copied this function
from MSDN. Would someone be kind enough to explain to me:

Why variable Globalflag is needed to be declared in this function?
What does “GlobalFlag = x” do at the end?
Why does this function works fine the first time it is used but afterwards
the very first record would be marked incorrectly? (1*, 2,3,5*. Should be 1,2,
3,5*)

Eager to hear from any of you experts. Appreciate it. See function code below:



Option Compare Database

Dim GlobalFlag As Variant

Public Function SetFlag(x As Variant)
If GlobalFlag <> x - 1 Then
SetFlag = "*"
Else
SetFlag = " "
End If

GlobalFlag = x


End Function
 
M

Marshall Barton

Crystal said:
I am new to vba and I am learning to create functions. I copied this function
from MSDN. Would someone be kind enough to explain to me:

Why variable Globalflag is needed to be declared in this function?
What does “GlobalFlag = x” do at the end?
Why does this function works fine the first time it is used but afterwards
the very first record would be marked incorrectly? (1*, 2,3,5*. Should be 1,2,
3,5*)

Eager to hear from any of you experts. Appreciate it. See function code below:

Option Compare Database

Dim GlobalFlag As Variant

Public Function SetFlag(x As Variant)
If GlobalFlag <> x - 1 Then
SetFlag = "*"
Else
SetFlag = " "
End If

GlobalFlag = x
End Function


It probably work the way you want the very first time you
used the function. If you called it with one sequence,
then, sometime later, called it with a different sequence,
it would mark the first number in the second sequence
because it doesn't have any way of knowing that you wanted
to start over. The reason it should work once is that the
module level variable is automatically initialized to Null
when you start Access (or do something to reset your
project).

The last line, GlobalFlag = x, is saving the current value
of x for the next time it is called.
 
C

Crystal via AccessMonster.com

Thank you so much for helping me with the question that probably is too
simple. Would you be kind enough to evaluate the following:

Am I correct in interpreting the variable globalflag as the following when
the function evaluates the number sequence of 1,2,5,7

Globalflag=Null, therefore equivalent to 1-1, so no star
Globalflag=1, therefore equivalent to 2-1, so no star
Globalflag=5, therefore equivalent to 5-1, therefore 5 should have *
Globalflag=7, therefore equivalent to 7-1, therefore 7 should have

Please help. Thanks!


Marshall said:
I am new to vba and I am learning to create functions. I copied this function
from MSDN. Would someone be kind enough to explain to me:
[quoted text clipped - 20 lines]
GlobalFlag = x
End Function

It probably work the way you want the very first time you
used the function. If you called it with one sequence,
then, sometime later, called it with a different sequence,
it would mark the first number in the second sequence
because it doesn't have any way of knowing that you wanted
to start over. The reason it should work once is that the
module level variable is automatically initialized to Null
when you start Access (or do something to reset your
project).

The last line, GlobalFlag = x, is saving the current value
of x for the next time it is called.
 
M

Marshall Barton

That is correct, except for the part about where GlobalFlag
is Null. It is not equivalent to 1-1. The Null situation
works because
If GlobalFlag <> x - 1 Then
will always fail regadless of the value of x. So the Else
condition is used. This is a little subtle and not a
particularly good practice.

The reason it will always fail is that Null can never be
compared anything, not even to itself. That statement makes
some sense if you think of Null as meaning 'unknown', then
ask yourself these questions: does an unknown value equal
somevalue; and is one unknown value equal to another unknown
value. The answer to both questions is I don't know or
'unknown'.
--
Marsh
MVP [MS Access]

Thank you so much for helping me with the question that probably is too
simple. Would you be kind enough to evaluate the following:

Am I correct in interpreting the variable globalflag as the following when
the function evaluates the number sequence of 1,2,5,7

Globalflag=Null, therefore equivalent to 1-1, so no star
Globalflag=1, therefore equivalent to 2-1, so no star
Globalflag=5, therefore equivalent to 5-1, therefore 5 should have *
Globalflag=7, therefore equivalent to 7-1, therefore 7 should have


Marshall said:
I am new to vba and I am learning to create functions. I copied this function
from MSDN. Would someone be kind enough to explain to me:
[quoted text clipped - 20 lines]
GlobalFlag = x
End Function

It probably work the way you want the very first time you
used the function. If you called it with one sequence,
then, sometime later, called it with a different sequence,
it would mark the first number in the second sequence
because it doesn't have any way of knowing that you wanted
to start over. The reason it should work once is that the
module level variable is automatically initialized to Null
when you start Access (or do something to reset your
project).

The last line, GlobalFlag = x, is saving the current value
of x for the next time it is called.
 
C

Crystal via AccessMonster.com

Thank you so so much. I really appreciate your patient explanation!!
Marshall said:
That is correct, except for the part about where GlobalFlag
is Null. It is not equivalent to 1-1. The Null situation
works because
If GlobalFlag <> x - 1 Then
will always fail regadless of the value of x. So the Else
condition is used. This is a little subtle and not a
particularly good practice.

The reason it will always fail is that Null can never be
compared anything, not even to itself. That statement makes
some sense if you think of Null as meaning 'unknown', then
ask yourself these questions: does an unknown value equal
somevalue; and is one unknown value equal to another unknown
value. The answer to both questions is I don't know or
'unknown'.
Thank you so much for helping me with the question that probably is too
simple. Would you be kind enough to evaluate the following:
[quoted text clipped - 25 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top