# Access VBA Code problem

I don't understand how the value at the end of this code is arrived at. Can someone do a walk through the code and explain the process

Private sub cmdMathOperation_Click()

Dim sngS As Single, sngK As single

sngS = 0

For sngK = 1 to 5                                            ( here the code will add "5" one time to the value of sngK??)
If sngK / 3 = Int (sngK / 2) Then
sngS = sngS + sngK
End if

Next sngK
lblBox.Caption = sngS

End Sub
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Basis of this logic the SNGS value will be 3 only..

the way the code works is this line...

``````For sngK = 1 to 5
``````

Puts one by one value in sngk which is 1,2,3,4,5

Then this equation of urs..
`````` If sngK / 3 = Int (sngK / 2) Then
``````

Whatever the sngk value is which will match only in this scneario when the sngk value is 3

it adds that value in the sngs value since you start sngs with zero..

And then in the final result which is this...

``````lblBox.Caption = sngS
``````

It gives you 3 as an answer...
Computer Info. Sys. StudentAuthor Commented:
Sorry but I am not seeing it.

Are you saying this:

If sngK (1) / 3 = Int (sngK (1) / 2) Then

If sngK (2) / 3 = Int (sngK (2) / 2) Then

If sngK (3) / 3 = Int (sngK (3) / 2) Then

If sngK (4) / 3 = Int (sngK (4) / 2) Then

If sngK (5) / 3 = Int (sngK (5) / 2) Then

This does not seem to work out but it seems like this what you are saying
Commented:
Yeah you want see that because when macro will change the values on its own what basically it will do...

if 1/3 = int(1/3).... Sngk value is 1
if 2/3 = int(2/3).... Sngk value is 2
if 3/3 = int(3/3).... Sngk value is 3
if 4/3 = int(4/3).... Sngk value is 4
if 5/3 = int(5/3).... Sngk value is 5

This will increase sngk value every single time compare the equation and then will do the next steps accordingly..
Computer Info. Sys. StudentAuthor Commented:
I am still having trouble seeing this. I seems to me that 3/3 is equal to 1

Sorry but I am new at this.
Commented:
yeah and int(3/2) is also equal to 1..The int statement which i posted the denominator is 2 not 3 which i posted incorrectly so you see when 3/3 =1 and int(3/2) =1 the if condition is meet and it populate the value for sngs which is zero to start with and since the condition is only met when value is 3 so you see at the end sngs value as 3 only...
Computer Info. Sys. StudentAuthor Commented:
3/2 is not equal to 1. Now I a getting confused

3/3 =  1 * ( 2 / 3)

Is the above what you are saying? It still does not equal 3
Commented:
you reading the statement wrong..statement is..

if 3/3 = Int(3/2) then

Now Int(3/2) is 1.5.. However the int function means integer which will just extract the integer part from the answer which is in this case is 1

so 3/3=1 and int(3/2) =1

so your if condition is satisfied...

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Computer Info. Sys. StudentAuthor Commented:
Thank you. This really made it clear to me now.
Commented:
For sngK = 1 to 5
If sngK / 3 = Int (sngK / 2) Then
sngS = sngS + sngK
End if
Next sngK

Everything between "For" and "Next" is a loop that happens 5 times.  Each time the loop executes, a different value is stored in sngK.  The first time it executes, 1 is stored.  The next time, 2 is stored and so on until the last time which is when 5 is stored into sngK.

The int() function takes whatever number you give it and changes it into an integer (whole number).  For example:

int(2.456) = 2

int(2.9999999) = 2

It does not round.  It just strips off the integer from the number.

Now that you have this, can you read the lines of code to understand what it is doing?
Computer Info. Sys. StudentAuthor Commented:
Thank you very much for giving me this info without my being able to give you points. You guys on Experts Exchange make the small amount of money I spend on membership worth every digital copper penny. Thanks again.
Commented:
Yw.. Happy  to Help..In additional i just want to bring couple of things to your notice we as an expert don't make any money through this..we do that purely on a voluntary basis to share our leanings or expertise that we have...However the only perk we guys enjoy is the free membership to the site..Apart from that rest all remains the same...

Saurabh...
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.