If- question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello.

I need some help with my If-statement.

This is a part of it.

If Fieldname1 = "***0" Then
Fieldname2 = True

What I need help with, is what I should write instead of "***0"
I want it to be true when Fieldname1 has a number that ends with a zero.
(ex. 1410, 1420, 1430 and so on)
 
try

If Right(Fieldname1, 1) = 0 Then

btw, if your *complete* If statement merely sets the value of Fieldname2 to
True/False, based on the whether or not the last digit of Fieldname1 = 0,
then you can shorten the expression to

Fieldname2 = (Right(Fieldname1, 1) = 0)

hth
 
Claes,

If fieldname1 is numeric, then the right way to check this is to use the
modulo function on it, like:

If (Fieldname1 Mod 10) = 0 Then
etc.

if, on the other hand, it is a text field, then the condition would be:

If Right(Fieldname1, 1) = "0" Then
etc.

HTH,
Nikos
 
Thank you for your help.
It works just fine now.

This was only a part of the If-statement.
It covers other things also, but since this part was the trouble, I only
wrote that part.
Thanks anyway.

/Claes D

"tina" skrev:
 
The "right way" ? There is always more than one "right" way:
If Right(Cstr(fieldname1),1) = "0" then fieldname2 = True
or
fieldname2 = Right(Cstr(fieldname1),1) = "0"

Data type does not matter
 
Hi Klatuu,

I know it works, but I've never felt comfortable with string operations
on numbers. Actually, if this was the result of a calculation, I would
take an extra step, such as using a condition like:
If (Fieldname1 Mod 10) < 0.001
or something... I've seen some strange things resulting from finite
calculation accuracy, and on top of that you sometimes have to go to the
15th decimal to see your nice round number is not so round after all, so
it's not apparent. I'm sure you've seen this kind of thing too!

Regards,
Nikos
 
I understand your concern, Nikos, but, depending on data type it may or may
not be an issue. If the number is an interger or a long, then it is not a
concern. If you have decimal places, the game changes. What really matters
is the business rule.
As I said before, it can seldom be said that any technique is the absolute
right way to do someting. There are, on the other hand, some really wrong
ways to do things. Don't we see them every day?

I think the suggestion you made in a previous post was very valid. I don't
think string operations on numbers are a bad thing provided you know what
data you expect and what results you want. You must agree, there are times
when string operations on numbers are required.
 
Klatuu,
I understand your concern, Nikos, but, depending on data type it may or may
not be an issue. If the number is an interger or a long, then it is not a
concern. If you have decimal places, the game changes.
Correct, but Claes didn't specify the data type; Access's default
default numeric type (no pun intended) being Double, there's a good
chance people reading this would try to apply it on a double.
This is a lesson learnt the hard way!

As I said before, it can seldom be said that any technique is the absolute
right way to do someting.
Agreed. I was just trying to be as general as possible, but there will
always be exceptions.

There are, on the other hand, some really wrong ways to do things. Don't we
see them every day?
Not only see them, in all fairness I've had my fair share in the doing!
I occasionally open some older app of mine, and feel ashamed at what I
see - or happy I've come a long way, depends on how one looks at it.

I think the suggestion you made in a previous post was very valid. I don't
think string operations on numbers are a bad thing provided you know what
data you expect and what results you want. You must agree, there are times
when string operations on numbers are required.
OK, I admit I've done it myself, in separating digits for check digit
calculation in EAN numbers (the European equivalent of UPC numbers); I
had that as a text field though. I suppose there may be cases where this
practice could apply on pure numeric data, I just haven't had one yet.

Regards,
Nikos
 
Great, we seem to be in agreement, for the most part. Our only difference
seems to be in string operations on numeric data. I will agree one should
avoid it and be very careful when it is necessary.

Yes, going back to code we wrote years ago is like looking at our haircuts
and clothes of the same era. It can be embarressing or laughable.

As a side note, I was told by my mentor when I first began programming in
1977 that a very seasoned programmer's code would look very similar to a
beginner's code. He said that as we learn, we start using cute tricks we
learn just because we can, even when they are not necessary. After a few
years of convoluted obtuse routines, we figure out it is hard to maintain.
We get smarter and write straight forward code that anyone else can
understand and we can understand what we wrote six months ago. I found that
to be true over the years.
 
Just a side note.

The Mod operator always performs integer math. That is it rounds the numbers
involved to integers and then does the modulus operation.
 
Back
Top